Intercepting keystrokes, whitout having the focus

Posted on 1998-10-14
Last Modified: 2011-10-03
Perhaps soemeone would be able to help:

I'd like to be able to monitor keystrokes in Windows, so when a certain combination is pressed, my application which did not have the focus would be activated.

Question by:mmahdi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Geffl earned 100 total points
ID: 1439850
I had the same problem with my Printscreen utility!
Try the API call GetAsyncKeyState.
Public Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Public Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Type KeyboardBytes
    kbByte(0 To 255) As Byte
End Type

Public kbArray As KeyboardBytes
Public Const VK_SNAPSHOT = &H2C
Public Const VK_ALT = &H12

        If (GetAsyncKeyState(VK_SNAPSHOT)) Then
            If (GetAsyncKeyState(VK_ALT)) Then
                Call PrintDefault(1)
                Call PrintDefault(0)
            End If
            kbArray.kbByte(VK_SNAPSHOT) = 0
            kbArray.kbByte(VK_ALT) = 0
            SetKeyboardState kbArray
        End If
        Sleep 25
This is the code of my application - you have to modify the code.
But don´t forget the DoEvents and Sleep isn´t a bad idea.

Author Comment

ID: 1439851

It worked great!! Thanks for your help.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question