Keydown, and lost window focus

Blitz3D Forums/Blitz3D Programming/Keydown, and lost window focus

RifRaf(Posted 2009) [#1]
Im running into an odd thing. The player can use a joystick or keys to move a tank.. In windowed mode if the player is holding a direction .. say the up arrow key to move forward, and at the same time clicks off of the window to the desktop or another window. The game believes the user is still holding the arrow key, in fact even when you click back onto the games window the application is confused and nomatter what other keys you press it still thinks the up arrow is also pressed. The only way to fix it is to tap the up arrow again, sometimes more than once.

I have tried detecting when the window looses focus and flushing the keys, but that doesnt work. Ive tried flushing the keys on focus return as well but it doesnt do anything either(even if it did, that wouldnt fix the problem of the tank moving while the window is not in focus)


Anyone else ran into this, and if so did you find a solution. Im at a point where I may just remove windowed mode as an option.


GIB3D(Posted 2009) [#2]
I wouldn't worry about it too much. It happens in commercial games too. Like Half-Life 2, and um... other games I can't remember atm. I'd include Garry's Mod but that uses the Half-Life 2 engine so.. yea..


Warner(Posted 2009) [#3]
You could use GetASyncKeyState instead. It will also detect keyup/down when the window has no focus. Maybe this archive entry helps:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1319


RifRaf(Posted 2009) [#4]
Thanks warner, ill check that out


Warner(Posted 2009) [#5]
The reason that this occurs is that the window never receives a KeyUp Event for that specific key. In order for it to get a KeyUp message, you'll need to press the key again when the window has focus.
I suppose that internally, it works somewhat like this:
Dim keydown[255]

Function OnKeyUp(key:Int)
   keydown[int] = True
End Function

Function OnKeyDown(key:Int)
   keydown[int] = False
End Function