keyhit() is not 100%
Blitz3D Forums/Blitz3D Beginners Area/keyhit() is not 100%
| ||
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? |
| ||
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 |
| ||
have you got flushkeys in there somewhere? I noticed it depened on when i called flushkeys for all my keyhits to work. |
| ||
Sounds likely you are checking the same key twice or more in one loop like keyboard said. |
| ||
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? |
| ||
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_ |
| ||
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) |
| ||
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. |
| ||
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 :) |
| ||
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 |
| ||
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 |
| ||
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 |
| ||
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 |