Trapping Keyboard Events

The following example demonstrates how to use Akemi Spy Library to trap keyboard events on either a system-wide basis or just within a given thread.

To create this example from scratch, create a standard exe project with one form.  Open zip file

The form has the following controls (not including the various labels):

txtmEverything TextBox.  Used to display everything you type in other applications.  The Locked property is set to True so that you cannot type directly into the TextBox.
btnmStartNotepad CommandButton.  Starts Notepad.
chkmBlockW CheckBox.  Indicates whether the W key is to be blocked from all applications.
optmMonitorAllThreads OptionButton.  When selected, the application will monitor keyboard events from all threads.
optmOnlyMonitorNotepad OptionButton.  When selected, the application will search for an instance of Notepad, and then monitor keyboard events from that instance of Notepad.

The code for this form is below:

Option Explicit

'ShellExecute API is used to launch Notepad.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'Constant used in call to ShellExecute
Private Const SW_SHOWNORMAL = 1

'Object to receive keyboard events must be declared at
'class or form level.

Private WithEvents objmKeyboardEvents As KeyboardEvents

Private Sub btnmStartNotepad_Click()
    If ShellExecute(Me.hwnd, "Open", "Notepad.exe", "", "", SW_SHOWNORMAL) <= 32 Then
        MsgBox "Unable to start Notepad."
    End If
End Sub


Private Sub Form_Load()
    'Default option is to trap keyboard events from all threads.
    Set objmKeyboardEvents = GetAllThreads.KeyboardEvents
End Sub

Private Sub objmKeyboardEvents_KeyDown(Window As AkemiSpyLibrary.Window, ByVal KeyCode As Integer, ByVal AutoRepeat As Boolean, ByVal Shift As Boolean, ByVal Control As Boolean, ByVal Alt As Boolean, Cancel As Boolean)
    'Add the key that was pressed to the text box.
    txtmEverything.Text = txtmEverything.Text & Chr$(KeyCode)

    'Ensure that the last piece of text added to the text box
    'is visible by setting the selection to start at the end of the text.

    txtmEverything.SelStart = Len(txtmEverything.Text)

    'If the W key is being blocked, then cancel the keystroke
    'so that other applications don't receive it.
    '(This technique is shown here for educational purposes only.
    'Use of this program to play practical jokes on newly elected
    'heads of state is not in any way condoned by Akemi).

    If chkmBlockW.Value And UCase$(Chr$(KeyCode)) = "W" Then
        Cancel = True
    End If

End Sub

Private Sub optmMonitorAllThreads_Click()
    Set objmKeyboardEvents = GetAllThreads.KeyboardEvents
End Sub

Private Sub
optmOnlyMonitorNotepad_Click()
    Dim objNotepadWindow As Window

    'Find the first instance of Notepad to come to hand.
    'This example does not account for the possibility of multiple
    'instances of Notepad being open - only the first one to be
    'found will be monitored.
   
Set objNotepadWindow = GetTopLevelWindows.FindWindow("* Notepad", "Notepad")

    'Check whether an instance of Notepad was found, and
    'display a message if Notepad is not already open.
   
If objNotepadWindow Is Nothing Then
        MsgBox "No instances of Notepad currently open."
        optmMonitorAllThreads.Value = True
    Else
        'What we have found is a Window. The Keyboard events we
        'are going to trap, belong to the Thread of that window.
       
Set objmKeyboardEvents = objNotepadWindow.Thread.KeyboardEvents
    End If
End Sub


 

See also:  KeyboardEvents Property, KeyboardEvents Object

 

Home Copyright and Disclaimer