• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

Detecting a key combination from any other application.

I would like to be able to detect a key combination such as Crtl+G (or a single key for that matter) from my application when the key(s) are pressed in any other application.  Once the key(s) have been detected I would then like to copy some text to the other application (probably using sendkeys or the clipboard).  Any one have any ideas on how to detect the keyboard input?

Thanks in advance

1 Solution
Use the GetAsyncKeyState API.

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Const VK_CONTROL = &H11

You pass in VK_CONTROL and ASC("G") in succession like this:

        If (GetAsyncKeyState(VK_CONTROL)) Then
            If (GetAsyncKeyState(ASC("G"))) Then  
            'do your stuff here
            End If
        Sleep 25    'this app does not even register 1% of processor time on most machines
                    'even though it's checking 40 times a second for keyboard events

Make sure you include some way for breaking out of that loop so that you release the memory when you're done.
iankoshAuthor Commented:
Thanks very much! (Easy when you know the API call!!!)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now