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.

Newcomers DBPro Corner / Inkey$() functionality

Author
Message
kawaguy
21
Years of Service
User Offline
Joined: 9th Aug 2003
Location: United States
Posted: 10th Aug 2003 23:11
Hi,

Here is a question for a pro. I am beginning to use DB for my future game projects.

I understand inkey$() function returns a character WHENEVER a key is pressed. Unlike other languages, the key is not recorded AFTER it was released.

Because of this functionality, the user must be told to press and HOLD the key until the function gets it. The snippet is shown below.

Is there any other command that would "remember" the key being pressed or a better algorithm?


print "Press & hold Q to quit";
repeat
print " .";
wait 1000
until inkey$() = "q" or inkey$() = "Q"
` wait until key is released
while inkey$() = "q" or inkey$() = "Q"
endwhile
end
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 10th Aug 2003 23:25 Edited at: 10th Aug 2003 23:28
All key presses are stored in the keyboard buffer and the buffer string can be called using entry$(). However simply checking to see if entry$()="q" wouldn't be enough incase multiple keys have been pressed, so you would have to use a checking routine to see if one of the keypresses has been "q". Also clearing the keyboard buffer on each loop would help with the speed of the checking routine.



This is using DB classic, I don't know if it is the same in Pro.

Also you can use lower$() to convert the string to lower case so you don't have to check for both cases .

cuRant PRogekt: a three-de map editer
Why the hell'd you ask me for crying out loud!?!
Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 10th Aug 2003 23:27
you just have to write your code a little differently and use the entry buffer, the entry buffer remembers the keys that where last pressed on the keyboard, you just need to strip the last character off with left$ and use that, heres an example of the entry buffer.

clear entry buffer
print "press a few keys"
wait 10000
print entry$()
wait 10000

even though the code is doing nothing more than waiting, the entry buffer still gets filled with the last few keypresses, when you have finished with the data in the entry buffer you can clear it and then check for any changes, hope thats some help to you, cheers.

Mentor.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Aug 2003 23:30
Is this what you are after?

Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 10th Aug 2003 23:34
The problem with that code is that it will freeze the program if there needs to be anything else running IanM.

cuRant PRogekt: a three-de map editer
Why the hell'd you ask me for crying out loud!?!
Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 10th Aug 2003 23:58
you could just set a flag to remember the key that was pressed, if your codes that slow it can`t detect a keypress then it must be doing less then 30fps and needs a spot of optimiser number 5 , most people would press a key for one thirtieth of a second and not notice anything amiss (unless they where trying to type or something that required rapid repeated response), and most db code should run at well over 30fps, especialy if it was DB Pro we are talking about, this language is mainly intended for realtime use, cheers.

Mentor.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Aug 2003 02:05
*Shrug* That's what his original code did.

I have more complex code in a library that does the 'trigger' on key release, but that's far too much to post for someone who registered yesterday.
kawaguy
21
Years of Service
User Offline
Joined: 9th Aug 2003
Location: United States
Posted: 11th Aug 2003 02:25
Replies from you all are much appreciated, indeed.

I am following Hamish's suggestion. Here is the modified and better code:

print "Press Q key to quit";
clear entry buffer
repeat
print " .";
wait 1000
lastch$ = right$(entry$(), 1)
until lower$(lastch$) = "q"
end

The last character in entry buffer is expected a "Q" whenever user last presses the key, not in the middle somewhere. This is my understanding.

Anyway, it works like a charm but there is some strange happenings that make me puzzled. Please see my next topic.

Thank you.

Login to post a reply

Server time is: 2024-09-20 19:50:40
Your offset time is: 2024-09-20 19:50:40