flopoloco's picture

One time key input

Hello, I 'm a bit struggling with a problem here... I have this piece of code which basically alters a boolean variable amongst the two available values.

if (Keyboard.[Key.Space])
				this.alertState = !this.alertState;

The trick I want to achieve here, is not to check constantly for a key press but give it a shot only once!

That means that the above piece of code behaves like this, because my human timing cannot sync with the software update timing. ;-)
Updates: 1 2 3 4 n
Input: X X X X n

And I would like to achieve this:
Updates: 1 2 3 4 n
Input: X - - - n

Sorry for asking C# based (not OpenTK) questions.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
objarni's picture

If you are under Windows.Forms, you could use KeyDown events instead of the Keyboard device.

If you are under GameWindow, I think you'll have to roll your own "space pressed event" logic:

class Game {
   bool spaceDown = false;
   void SomeMethod() {
     if(Keyboard[Key.Space] && !spaceDown) {
       spaceDown = true;
       alertState = !alertState;
     if(!Keyboard[Key.Space] && spaceDown)
       spaceDown = false;

This way "alertState" only changes when the "Space key pressed" event occur.

(the word "pressed" is a little ambigous to me - "click" is more to-the-point but is used for the mouse input device in everyday language)

the Fiddler's picture

Or, you can use the KeyDown event of the keyboard device, just like Windows.Forms. :)

objarni's picture

Ah.. Nice :)

flopoloco's picture

Oops, I 've just seen one example in OpenTK (one that makes the user select Fullscreen or Window mode). Thanks anyway. ;-)