none
Help detecting which keyboard keys are pressed RRS feed

  • Question

  • I am new to PowerShell and I would like to write a script that monitors the keyboard and when predefined keys are pressed I want to perform a action. My work doesn't allow the application autohotkeys, but they would allow PowerShell so I wanted to try and create something to mimic that application.

    I have been searching around and haven't found any good articles to help with this. Any help would be great.

    Sunday, October 13, 2013 12:29 AM

Answers

All replies

  • Sorry but that is not possible.  You can only intercept the keyboard with a driver or an application hook.  Both must be installed.  PowerShell cannot do that.

    You can define keyboard shortcuts that will launch an application and most applications support hot keys.

    Autohotkey is not a Microsoft product so you would need to post in other forums for help with that.


    ¯\_(ツ)_/¯

    Sunday, October 13, 2013 12:57 AM
  • IF you are asking how to read the keyboard in PowerShell then that can be done with the following:

    $host.ui.RawUI.ReadKey()

    This will give you back the full driver response of the key pressed on the keyboard.

    Here is what it looks like for the escape key:

    PS C:\scripts> $host.ui.RawUI.ReadKey()

                   VirtualKeyCode                     Character               ControlKeyState                       KeyDown
                   --------------                     ---------               ---------------                       -------
                               27                             ←                     NumLockOn                          True


    ¯\_(ツ)_/¯

    Sunday, October 13, 2013 1:03 AM
  • It is possible but not with built-in cmdlets. A good starting point would be the script linked below, which acts as a key logger but can be modified to perform actions based on specific key strokes:

    https://github.com/mattifestation/PowerSploit/blob/master/Exfiltration/Get-Keystrokes.ps1


    • Edited by Dirk_74 Sunday, October 13, 2013 7:03 AM
    • Marked as answer by Zach Shupp Sunday, October 13, 2013 11:45 AM
    Sunday, October 13, 2013 7:03 AM
  • Thanks Dirk, This is what I was looking for. I will have to modify it but it will help me with what I wanted to do.
    Sunday, October 13, 2013 11:45 AM