keyhit() is not 100%

Blitz3D Forums/Blitz3D Beginners Area/keyhit() is not 100%

jigga619(Posted 2003) [#1]
I wrote a program that was about 300 lines of code. I used the keyhit statement often. I wrote another program that was about 3000 lines of code and noticed that keyhit would not work anymore. I would have to use keydown() istead of keyhit. Have any of you ever had this problem? Is there a limit to how many keyhit statements you can have in your program?


keyboard(Posted 2003) [#2]
The skys the limit as far as I know :)

Are you checking the Keyboard presses all in the one place, like in my example below, or at different locations throughout your program? If all the keyboard checks are in the same place in your code, there is less chance of them getting lost...


Graphics 640,480,16,2
SetBuffer BackBuffer()
Global MyString$= "press an arrow key"
Global Program = True
While Program
	Cls
	Text 100,100,MyString$
	Flip
	CheckTheKeyboard()
Wend
End
Function CheckTheKeyboard()

	If KeyHit(1) Then Program = False
	If KeyHit(203) Then MyString = "left arrow pressed"
	If KeyHit(205) Then MyString = "right arrow pressed"
	If KeyHit(200) Then MyString = "Up  arrow pressed"
	If KeyHit(208) Then MyString = "down arrow pressed"


End Function



hope this helps


(tu) sinu(Posted 2003) [#3]
have you got flushkeys in there somewhere?
I noticed it depened on when i called flushkeys for all my keyhits to work.


Rottbott(Posted 2003) [#4]
Sounds likely you are checking the same key twice or more in one loop like keyboard said.


jigga619(Posted 2003) [#5]
Actually I do test for the same key often during the entire program. For example, I check for keyhit(28) at least 20 different times in the same program.

For example, I may have 4 variables in my program.
The names of the variables are

a#
b#
c#
d#
I would then write

if a#=0 and keyhit(28)
value=true
endif

if a#=1 and keyhit(28)
value=false
endif

and so on. Is this method of programming incorrect?


Binary_Moon(Posted 2003) [#6]
Is this method of programming incorrect?


Yup. Call the keyhit function once and it resets the current keyhit value for the key you are checking against.

To do your example above you could use a selectr case statement or more if's

[code]
if keyhit(28_


Binary_Moon(Posted 2003) [#7]
Is this method of programming incorrect?


Yup. Call the keyhit function once and it resets the current keyhit value for the key you are checking against.

To do your example above you could use a selectr case statement or more if's

[code]
if keyhit(28)


Binary_Moon(Posted 2003) [#8]
Is this method of programming incorrect?


Yup. Call the keyhit function once and it resets the current keyhit value for the key you are checking against.

To do your example above you could use a select case statement or more if's

if keyhit(28)
   if a=0 value=true
   if a=1 value=false
endif


Both methods will be faster (less key checking) and the code will be cleaner/ easier to change. For example if you want to change the key you use for this task you would have to go through a lot of commands replacing the vlaue. In the example above you would just have to do it once.


ashmantle(Posted 2003) [#9]

a#
b#
c#
d#
I would then write

if a#=0 and keyhit(28)
value=true
endif

if a#=1 and keyhit(28)
value=false
endif


also I don't think its good practice to compare a float# variable to an integer% value :) would 'almost' never match, but I guess you just made up a quick example and didn't check :)


WolRon(Posted 2003) [#10]
Also, as opposed to this method:
if keyhit(28)
   if a=0 value=true
   if a=1 value=false
endif

You could store the value in a variable and check against it like you were.
if keyhit(28)
 enterkey = true
else
 enterkey = false
endif

if a=0 and enterkey 
 value=true 
endif 

if a=1 and enterkey 
 value=false 
endif 



(tu) sinu(Posted 2003) [#11]
i think wolron's method is best.

Function CheckForKeyHit(key%)

if keyhit(key)
return true
else return false

endif


end function

if CheckForKeyHit(28) = true and a=0

value=true

endif


Neo Genesis10(Posted 2003) [#12]
I've always preferred the following method (that way, you can use it multiple times).
upkey = KeyHit(200)
downkey = KeyHit(208)

If control = player
	If upkey DoBlah()
	if downkey DoBlahBlah()
Else
	If upkey DoBlah_2()
	if downkey DoBlahBlah_2()
EndIf



Al Mackey(Posted 2003) [#13]
Neo's system can also be used if you like to detect multiple keys that do the same thing. For example, this will detect key down on both the arrow keys and the WASD keys for your lefthanded users. Also, since KeyDown and the Or factors out into a 0 or 1, you can do more shorthand converting the keys to velocity, below:

keyU = KeyDown(17) Or KeyDown(200)
keyL = KeyDown(30) Or KeyDown(203)
KeyD = KeyDown(31) Or KeyDown(208)
KeyR = KeyDown(32) Or KeyDown(205)

VelocityX = KeyR - KeyL
VelocityY = KeyU - KeyD