-
I'm wondering how i can make something to tell if a key or combination of keys are pressed while a program is running. The form does not have to be the top window. Like if you are on you computer, where ever you are, with your program running, and you hit CTRL + ESC, then do something. Or any specific keys. How Would You Do This?? Please Help.
-
Use the GetAsyncKeyState API. Add the following to a Form with a Timer.
Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 50
End Sub
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyControl) And GetAsyncKeyState(vbKeyEscape) Then MsgBox "CTRL+ESC was pressed"
End Sub
-
Place a timer on the form paste this code:
Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Const VK_CONTROL = &H11
Private Const VK_ESCAPE = &H1B
Private Sub Timer1_Timer()
If GetAsyncKeyState(VK_CONTROL) And GetAsyncKeyState(VK_ESCAPE) Then MsgBox "CTRL + ESC PRESSED"
End Sub
-
The constants are already internally defined in VB, so you can omit them.