Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / getting input using dbGetEntry

Author
Message
american eagle
17
Years of Service
User Offline
Joined: 2nd Aug 2007
Location:
Posted: 7th Nov 2009 03:58
i'm using dbGetEntry to get some string input but I want that if i press backspace i want to delete my last character from the string any ideas? here is my code



thx
bloodmage2
15
Years of Service
User Offline
Joined: 14th Jun 2009
Location:
Posted: 10th Nov 2009 21:41
what i do for my text inputs is this:
a variable for how many characters you have
a char array for your input string
a variable that is set every time you make a new character
when backspace is hit, it deletes the most recent character, and sets the number back one. when a non backspace key is hit, it puts the character in the array at the index of the number of characters, then increases the number of characters.

hope this helps. if you want, i can try to give you my text IO class header file.

-to the optimist, the glass is half full. to the pessimist, it is half empty, to the engineer, it is twice as big as it needs to be.
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 10th Nov 2009 22:18
Someone wrote this function (Not me):



It should do the job.
american eagle
17
Years of Service
User Offline
Joined: 2nd Aug 2007
Location:
Posted: 11th Nov 2009 00:04
thx for all I did figure out a way to do the job it looks like the code puppy of kosh suggested and it works fine
Marsh0
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 15th Nov 2009 22:42 Edited at: 16th Nov 2009 05:26
Thanks a ton!. It works great
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 16th Nov 2009 22:19 Edited at: 16th Nov 2009 23:42
It works but it creates a memory leak. Every time when you use a Dark GDK function which returns a char pointer, it creates a new string in memory. If you don't delete it, the strings will accumulate. This function calls dbGetEntry three times every function call, that's three strings per loop. Here is an improved version, with memory leak removed.



(Edited because I left out the first dbClearEntryBuffer and the closing bracket. But the main part was OK.)
Marsh0
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 17th Nov 2009 01:30
thanks for the updated version. Anyone know the name of the original author?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Nov 2009 01:57 Edited at: 17th Nov 2009 01:58
That code can be improved a lot; Why is the second 'dbClearEntryBuffer();' not done right after 'NewEntry = dbGetEntry();'? Why is the input buffer 100 chars long yet the limit when adding to it is set to 256? Why is the first entry char value checked against NULL(not like you can type it, and typing nothing results in 'dbGetEntry()' returning NULL)? Why does it check if you press backspace before it decides to remove a character? The backspace (0x8) character will get inserted if you press it so it should check that instead.

american eagle
17
Years of Service
User Offline
Joined: 2nd Aug 2007
Location:
Posted: 17th Nov 2009 02:43
dark coder your right that code could improve a lot and should be shorter too. I did wrote a good code I will try posting it later but I think I found the best way to do the job ... and another setback of this code is that it waits for returnkey it stops everything until the writing is done. It shouldnt be this way for this purpose u could simply use dbInput() function the code I made runs with the program and doesn't stop it.. will try to post it soon so u all could take a look
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 17th Nov 2009 07:51 Edited at: 17th Nov 2009 07:53
Quote: "Why is the input buffer 100 chars long yet the limit when adding to it is set to 256?"


Ooops I didn't spot that.

Quote: "
Why is the first entry char value checked against NULL(not like you can type it, and typing nothing results in 'dbGetEntry()' returning NULL)?
"


That however, is not exactly true. In my experience dbGetEntry returns null only before hitting the very first character. After that, the returned pointer is never null even if the returned string is empty, so checking for an empty string is not unnecessary.

Otherwise you are right that the code could still be improved. Yesterday I just concentrated on the memory issue.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 17th Nov 2009 21:38 Edited at: 8th Jan 2010 21:20
Here is a further improved version of the function. This eliminates one more problem that existed with the original code: If you type very fast, then dbGetEntry can return more than one character at the same time, but the character count was only increased by one. This makes the character count incorrect sometimes, with the result that when Backspace is pressed the next time, more than one character is deleted back. What's worse, the code could overwrite the end of the array with an incorrect character count. In this version, I added the characters from the input string one by one, to avoid this issue.

I tested this function together with a 3D object which was moved up/down with the keyboard, to make sure that the function does not disturb detection of the keystates later in the loop.

This function does not watch the return key. It depends on how the program is organized, the return key can be watched from the main program (the code that calls the function) or maybe you can add code to the function to return a true/false flag to indicate when return has been pressed.



Calling the function:



Feedback is welcome.

(EDITED because I accidentally left one surplus line in the function and because the space for the null terminator was not taken into account in the previous version.)
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 20th Nov 2009 20:18 Edited at: 21st Nov 2009 10:13
Nevermind that post, my program build was stuck on the old function, it works perfectly.

Login to post a reply

Server time is: 2024-10-01 18:33:07
Your offset time is: 2024-10-01 18:33:07