Check that no keys are being pressed

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Check that no keys are being pressed

Post by Dude »

For Windows, what's the best way to ensure that the user isn't pressing down any keys at any given time, no matter which app has the focus? Looping all virtual key code states with GetAsyncKeyState and checking for 0 status isn't efficient nor working reliably, so I'm looking for an alternative solution. Thanks.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Check that no keys are being pressed

Post by mk-soft »

Remove Keyboard :mrgreen:

No.
Search forum to KeyboardHook :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Check that no keys are being pressed

Post by firace »

A very clean and simple method is the GetLastInputInfo API (but be aware that this includes both mouse and keyboard input):

Code: Select all

LII.LASTINPUTINFO
LII\cbSize = SizeOf(LASTINPUTINFO)

Repeat 
  GetLastInputInfo_(@LII)
  Debug LII\dwTime
  Delay(1300)
ForEver
highend
Enthusiast
Enthusiast
Posts: 123
Joined: Tue Jun 17, 2014 4:49 pm

Re: Check that no keys are being pressed

Post by highend »

@firace

LASTINPUTINFO is a predefined structure, right?

Is there a compilation for PureBasic of these structures,
that you don't need to define yourself?
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Check that no keys are being pressed

Post by firace »

highend wrote:@firace

LASTINPUTINFO is a predefined structure, right?

Is there a compilation for PureBasic of these structures,
that you don't need to define yourself?
Try the Structure Viewer IDE tool (Alt+S) for a full list (and double-click a structure to display its members).
highend
Enthusiast
Enthusiast
Posts: 123
Joined: Tue Jun 17, 2014 4:49 pm

Re: Check that no keys are being pressed

Post by highend »

Thanks, firace!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Check that no keys are being pressed

Post by Dude »

Thanks firace, I did a GetLastInputInfo to wait for no input for 50 milliseconds and it seems to be working fine. Great idea! :D
Post Reply