KeyHit versus KeyDown

BlitzPlus Forums/BlitzPlus Beginners Area/KeyHit versus KeyDown

Zooker(Posted 2005) [#1]
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?


xlsior(Posted 2005) [#2]
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.


Grey Alien(Posted 2005) [#3]
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.


CS_TBL(Posted 2005) [#4]
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)


Zooker(Posted 2005) [#5]
Thanks to all of you, you've all been very helpful!


Seldon(Posted 2005) [#6]
@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. 



Grey Alien(Posted 2005) [#7]
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!


WolRon(Posted 2005) [#8]
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.


Grey Alien(Posted 2005) [#9]
oh OK, cool, I wonder what the max number is 1 byte = 255 or more?


WolRon(Posted 2005) [#10]
Don't know, but in a quick test I made, it counted 329 keyhits I made.


Grey Alien(Posted 2005) [#11]
more than a byte then, hmm. More than most people will ever need!


xlsior(Posted 2005) [#12]
@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.


Gregor(Posted 2005) [#13]
It is strange that these examples in the blitzdocs about keyhit and keydown doesn't work. Probably they've never checked it.


Grey Alien(Posted 2005) [#14]
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