Keyboard Hook


Capture global keyboard events in PowerShell ISE.
(warning, this is effectively a key logger for PowerShell).

Why run AutoHotKey and have to learn yet another automation language, when you can trigger PowerShell commands or functions directly from any keyboard key combination.

After spending a lot of time on Goole looking for solutions to capture keyboard events in ISE and finding that all of the solutions only worked in the PowerShell command console, not ISE, I turned to GitHub for a head start in building something that would allow ISE to detect any keyboard event.

It is a .Net class that supports events in PowerShell

Download the DLL into your PowerShell directory using this link!ApGpqMMpRLhijfNq4VZM-E-H5WiIEw

Remember to unblock the file after saving it.

Use it in ISE like this…

add-type -Path '.\documents\WindowsPowerShell\VISE_WinKeyboardHook.dll'
$KeyboardInterceptor = new-object VISE.WinKeyboardHook.KeyboardInterceptor
function HandleKeyDown($keyId)
    write-host $keyID.KeyCode
    if($keyID.KeyCode -eq "Escape"){
Unregister-Event -SourceIdentifier KeyDown -ErrorAction SilentlyContinue
$keyevent = Register-ObjectEvent -InputObject $KeyboardInterceptor -EventName KeyDown -SourceIdentifier KeyDown -Action {HandleKeyDown($event.sourceEventArgs)}

The $event.sourceEventArgs returns several properties that can be accesses in the HandleKeyDown function:

  • $keyID.KeyChar
  • $keyID.KeyCode
  • $keyID.KeyData
  • $keyID.KeyValue
  • $keyID.Modifiers
  • $keyID.Shift
  • $keyID.Alt
  • $keyID.Control

The one I use most is KeyData as this returns a string with all the current keys that are down. The following shows the output for all properties as a d keyboard sequence is pressed..

KeyCode LControlKey
KeyData LControlKey
KeyValue 162
Modifiers None
Shift False
Alt False
Control False
KeyCode LMenu
KeyData LMenu, Control
KeyValue 164
Modifiers Control
Shift False
Alt False
Control True
KeyCode D
KeyData D, Control, Alt
KeyValue 68
Modifiers Control, Alt
Shift False
Alt True
Control True

There is also a KeyUp event so you can wait until a key is released for things like volume settings or PTZ camera movements.

It is particularly useful for keyboards that have extra function keys that you can use to automate anything. I’ll have some sample for using this MX Light shortly.

Code for this dll was largely copied from a codeplex project and includes this license


3 thoughts on “Keyboard Hook

  1. I tried adding this in a powershell script, but I got this error:

    Add-Type : Could not load file or assembly ‘file:///E:\WindowsPowerShell\VISE_W
    inKeyboardHook.dll’ or one of its dependencies. This assembly is built by a run
    time newer than the currently loaded runtime and cannot be loaded.

    I’ve updated my .net framework to 4.7, but the error is still persistent.

    Any kind help will be highly appreciated. 🙂

    1. Hi,
      You have to right click on the DLL and check that it is not blocked (File properties dialog, bottom of the window, look for Un-block tick box or button).
      I did have a debug version accidentally released at one stage so check you have the latest version from!ApGpqMMpRLhijfNpfp52okmo8YdOBA
      I will test this on a clean machine that doesn’t have Visual Studio installed later tonight and confirm the version of .Net required.

      What OS version are you using?

  2. I have just tested on a Windows 10 PC (64bit) running in ISE 32 and 64 Bit that only has .net 4.7 installed.
    Also tested on Windows 7 32Bit OS that has .Net 4.5.2 as well as 3.5 SP1, 3.0 SP2 etc.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s