05-04-2007, 05:27 PM
GetAsyncKeyState + GetActiveWindow is my current favorite.
GetKeyState returns non-zero if the key has been pressed, ie:
GetActiveWindow returns 0 if the current window is not selected. So you can add this to the above example before it:
Then you only get keypresses from when your project is active. Not sure how it works with multiple forms, but it works in MDI forms, I know that much. I'm pretty sure it'll work fine, though. :wink:
You have to use this with a timer of some sort since you have to constantly check for the key presses, they're not event based like the KeyDown and KeyPress events on the form.
Code:
Public Declare Function GetKeyState Lib "User32" (ByVal nVirtKey As Long) As Integer
Public Declare Function GetActiveWindow Lib "User32" () As Long
GetKeyState returns non-zero if the key has been pressed, ie:
Code:
If GetAsyncKeyState(vbKeyLeft) Then Msgbox ":)" else msgbox ":("
GetActiveWindow returns 0 if the current window is not selected. So you can add this to the above example before it:
Code:
If GetActiveWindow = 0 Then Exit Sub
Then you only get keypresses from when your project is active. Not sure how it works with multiple forms, but it works in MDI forms, I know that much. I'm pretty sure it'll work fine, though. :wink:
You have to use this with a timer of some sort since you have to constantly check for the key presses, they're not event based like the KeyDown and KeyPress events on the form.