Can Powershell listen for and capture key presses?
Is it possible to write a PowerShell script that, like AutoHotkey, sits in tray and waits until you press a predefined keyboard key to start execution? And possibly not return but fire every time you press said key?
What I would like to achieve is - perform a predefined scripted action at the press of a button only AFTER starting the script, so putting it on the desktop and defining a shortcut key doesn't work.
For example:
I'd like the text "TEST" typed 3 times every time I press the "x" key but I would like this to happen only if the script that does this is running. So when the script is not running - pressing "x" would do nothing.
Basically, AutoHotkey can do exactly that but I'd like to do it in PowerShell, if possible, without writing huge amounts of C# code, because then I'd just write a tiny C# tray application for that.
You can Capture Keystrokes with Powershell with the help of a Third Part coding Script (Autohotkey)
You only need in Powershell to read this Windows registry Key.
And if you then run, together these Two AHk Scripts (KeypressValueToREG + ShowKeypressValue) then it is indirect Possible.
Note - the ShowKeypressValue.ahk is only to show visually, all your mouse button clicks and all your Keyboard keystrokes (it is not nessesary to use this Script)
You can run only the KeypressValueToREG.Ahk in the Background and then you are ready to go. (you can Capture all your keystrokes values into only one Variable $val)
KeypressValueToREG.ahk
ShowKeypressValue.ahk
Not in PowerShell directly maybe, but as you can run pretty much any C# code, here is a basic working example based on an excellent solution by Peter Hinchley:
Version 2
(using a callback):