Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with KeyDown
#4
GetAsyncKeyState + GetActiveWindow is my current favorite.

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.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)