devxlogo

Capturing Key States from a Non-active Window

Capturing Key States from a Non-active Window

Sometimes you need to determine whether a particular key is pressed or not?even though the application is not active. “GetAsyncKeyState” comes to the rescue. This function takes a virtual key code as a parameter and returns non-zero if the function succeeds.

Here’s a sample app for capturing the ‘F5’ key. In this example, I call GetAsyncKeyState in a timer, so that it can capture the key press event.

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As IntegerPrivate Const VK_F5 = &H74Private Sub Timer1_Timer()    If GetAsyncKeyState(VK_F5) <> 0 Then        Text1.Text = "F5 Pressed..."    End IfEnd Sub

devx-admin

Share the Post: