keyhit/keydown
Blitz3D Forums/Blitz3D Beginners Area/keyhit/keydown
| ||
I am trying to make it so if you press <Enter> on a menu it will go to the next and stuff, but if I press <Enter> once, it counts for all of the If KeyHit(28) then....code.. For example on a menu: "Press <Enter> to continue." then it goes to another menu: "Press enter again to continue." But it skips through all of the menu's if you press <Enter> once. Is there any way around this? |
| ||
If I understand you correctly you need to flush the keystroke buffer between menus. Just call FlushKeys() between your menus and that should take care of it. |
| ||
What CGV said. Perhaps you need also a waitkey() if you want to halt the program until a key is pressed. Or you can continue your program flow only when enter is pressed. In this case, a while..wend loop could be a solution: this snip will loop until <enter> is pressed while not keydown(28) wend and this will wait until the <enter> key is depressed: while keydown(28) wend ;let's flush also the keyboard buffer flushkeys() Using the first one in conjunction with the second, you should have the requested behaviour. Put both in a function, and call it when you need that your program waits until the user presses (and releases) the enter key. If you make the key code as a parameter for the function, you can then use the same algorithm to wait for any key pressed (and released): const K_SPACE = 57 ;if I recall good... const K_ENTER = 28 ;ENTER ;wait until space bar is pressed and released wait_key(K_SPACE) ;wait until the ENTER key is pressed and released wait_key(K_ENTER) . . function wait_key(key) ;wait for key being pressed while not keydown(key) wend ;wait for key being released while keydown(key) wend ;flush the keyboard buffer flushkeys() end function All this theory is valid in case of a static menu, when no screen refresh is requested when displaying the menu itself. If, instead, you need to refresh the screen in order to show some animation, then you may have to slightly change the code, but basically the concept is the same. You can use a flag to indicate when a certain key has been pressed, and use that flag to change the status of your menu. Hope this has sense for you, Sergio. |
| ||
But it skips through all of the menu's if you press <Enter> once. Is there any way around this? Once, you check for keyhits with the keyhit() command, the buffer for that key is cleared, no matter how many keyhits (of that key) were made. There shouldn't be any reason to use the Flushkeys() command. Also, you should be careful using the Flushkeys() command since it will erase all other key buffers as well, which might be of importance to you depending on the application. I doubt that what CGV and semar stated to try is necessary. You should post your code. The problem probably lies in there. |
| ||
Yup, it works now, I used FlushKeys(), like this:If KeyHit(28) :FlushKeys():Exit:EndIf |
| ||
Agreeing with WolRon here. FlushKeys is not needed. I am glad to see you have it working, but the intial problem is with the program's logic. You really ought to post some code just so one of the heavenly gurus floating about might show you an alternative method. I have coded Very similar menu setups and never needed to do what you are doing now. There is a better way. Trust us. Put down the gun. Nobody is going to hurt. You are just going to have to trust me. There is a better way.. ;^) Seriously, 16 years ago or more I would have and probally did have 'bugs' such as what you you described. But as a programmer I learned a little, learned a little. I *think* I know what you are doing logic wise, but I would like to see some code. Actually I just want to steal your code. Bill Gates sent me here to assimilate you. You see, in the future, you are Bill Gates rival and the last nail in the coffin that is MicroSoft. By sending me back in time, he can change his fate. One bit of advice: If you meet a funny accented, muscle bounded beast of a man, RUN ! |
| ||
I'm going to agree with WolRon as well. Sorry about my first post po but I was having the EXACT same problem with my menu system but I'm using the mouse to enter menu selections, not the keyboard, so using FlushMouse was the solution to my problem. In your case though, since you're using KeyHit, you should not be having the problem you're describing. The cause of your problem is definately with your program logic. Post your menu code so smarter guys than me can help you. |
| ||
OOhhhhhh.... You guys were right :) Before you guys suggested FlushKeys(), I had If keyDown(28), not: If KeyHit(28). I took out the FlushKey()'s and it works fine with just KeyHit. -Thanks |
| ||
;^) |