KeyHit versus KeyDown
BlitzPlus Forums/BlitzPlus Beginners Area/KeyHit versus KeyDown
| ||
What is the difference between these comands, they both use scancodes? Does The While wend loop keep on cycling until a key has been pressed. The keyhit comand seems to be used always with keyhit? |
| ||
There is one major difference: keydown returns the scancode of a key that is currently actively pressed down. keyhit will tell you if a key was pressed and released since the last time you checked. Generally you want to use keydown for things where you need continuous input, e.g. the cursor keys in a game to move sprites around or something. Keyhit on the other hand will be good to see if something happened since the last time you checked. If you have a time-consuming loop somewhere, a keyhit() can tell you if escape was pressed during any time the program was doing other things, and you can tell it to cancel. If you used keydown(), you would have to be actively pressing the key at the exact time the keydown() check happened or it wouldn't register. |
| ||
Also Flushkeys will clear out the keyboard buffer and thus KeyHit will not return anything even if it was pressed earlier. Flushkeys is very useful. |
| ||
Keyhit/down looses focus sometimes if you alt-tab orso, or do ctrl-esc. I dunno if that's been fixed already, but GetASyncKeyState is more safe .. (tho you may need an hour to make your a handy system for it) |
| ||
Thanks to all of you, you've all been very helpful! |
| ||
@xlsior: Are you sure KeyDown() returns a scancode ?? I think it works just like KeyHit() for this aspect, i.e. they both return 1 or 0 whether the key you want to check (whose scancode is passed as argument) is pressed or not. From docs: KeyDown (scancode) Parameters scancode = corresponding key scancode This command is used to detect if a key is being held down. This command returns a 0 if the key is not held down, a 1 if the key is held down. |
| ||
Yeah. KeyDown(scancode) returns 0 or 1 depending if actual keys is down now. KeyHit(scancode) returns 0 or 1 depending if key is in the keyboard buffer, also it doesn't have to have been released to register! |
| ||
KeyHit(scancode) returns 0 or 1 ACTUALLY, it returns ANY number depending on how many times the key was pressed since the last check of the key. |
| ||
oh OK, cool, I wonder what the max number is 1 byte = 255 or more? |
| ||
Don't know, but in a quick test I made, it counted 329 keyhits I made. |
| ||
more than a byte then, hmm. More than most people will ever need! |
| ||
@xlsior: Are you sure KeyDown() returns a scancode ?? I think it works just like KeyHit() for this aspect, i.e. they both return 1 or 0 whether the key you want to check (whose scancode is passed as argument) is pressed or not. No, my bad. It's the # of hits. Mentioning scancode in the original question threw off my answer. The rest still stands, of course. |
| ||
It is strange that these examples in the blitzdocs about keyhit and keydown doesn't work. Probably they've never checked it. |
| ||
I think the examples may have been written for Blitz Basic, OR you need to be in a graphics window for them to work. The KeyHit example always returns 0! And the Keydown example never prints "Enter is being pressed!" lol |