Odd bug with KeyHit() and requesters

Archives Forums/BlitzMax Bug Reports/Odd bug with KeyHit() and requesters

TomToad(Posted 2011) [#1]
Sample code
SuperStrict

Graphics 800,600


While Not KeyHit(KEY_ESCAPE)
	If KeyHit(KEY_C)
		DebugLog "I hit the C key"
		Notify("The C Key has been pressed")
	End If
Wend

When I press the C key, the Notify requester appears. Click ok, then the next time I press the C key, it doesn't register as being pressed. Only by pressing it twice after the requester does it register the keypress.

After some experimenting, I noticed that it only happens when the key is not released before the requester appears. This workaround works.
SuperStrict

Graphics 800,600


While Not KeyHit(KEY_ESCAPE)
	If KeyHit(KEY_C)
		While KeyDown(KEY_C) Wend
		DebugLog "I hit the C key"
		Notify("The C Key has been pressed")
	End If
Wend


Running on Windows 7 Home Premium, AMD Phenom 9550 Quad-core 2.2Ghz, 6 gig memory, NVidia GeForce 9100
BlitzMax version 1.41

Last edited 2011


skidracer(Posted 2011) [#2]
A tidier workaround might be to call FlushKeys before the Notify.


zipplet(Posted 2011) [#3]
I think what happens here is as follows:
- User presses the C key
- Application receives the 'C pressed down' event
- Code runs and Notify() is called
- While the notification dialog is on-screen, the user releases the C key
- Application receives the 'C key released' event, but the event is lost because the notification dialog is still on screen!
- User closes notification dialog

Blitz at this point still thinks C is being held down, so the next time you press C, another keyhit() event is not generated. However, the next time the key is released the key release event is received, and keyhit() is reset so it works after 2 key presses.

I had this exact same bug when writing a game engine in Delphi. As skidracer says a call to flushkeys will work, but a note for the blitz developers: Maybe allow keyhit() to trigger again if a 'key pressed down' event is received again while a key is already registered as pressed?