Detect key release?

BlitzMax Forums/BlitzMax Beginners Area/Detect key release?

Lillpeter(Posted 2010) [#1]
How do i go about to find out if a key on the keyboard has been released? I have a function i want to execute only one time on release (when input stops).

So far the only solution i can come up with is to add to a counter or something while a key is pressed, and then check for any increment since the last frame, then if there is none i execute my function and reset the counter.

But i cant help thinking there´s a better way?

/Peter


Jesse(Posted 2010) [#2]
you can use keyhit or GetChar()


ziggy(Posted 2010) [#3]
Depending on what you're doing it's usually as simple as:
Local KeyWasPressed:Int = False

'Main Loop:
While Not KeyHit(Key_Escape)
    Local KeyIsPressed:int 
    KeyIsPressed = KeyDown(Key_X)
    If (KeyIsPressed = False) And (KeyWasPressed = True) Then
        'Do Stuff here, key has been released
    EndIf

    Flip() 'etc...

    KeyWasPressed = KeyIsPressed
Wend

(haven't tested, just take it as pseudo-code).

Last edited 2010


TomToad(Posted 2010) [#4]
You can also use events
SuperStrict

Graphics 640,480

Local Text:String = "No key pressed yet"

While Not KeyHit(KEY_ESCAPE)
	If PollEvent()
		If CurrentEvent.ID = EVENT_KEYDOWN
			Text = "Key "+CurrentEvent.Data+" Pressed"
		End If
		If CurrentEvent.ID = EVENT_KEYUP
			Text = "Key "+CurrentEvent.Data+ " Released"
		End If
	End If
	
	Cls
	DrawText text,10,10
	Flip
Wend



B(Posted 2011) [#5]
oops

Last edited 2011