Detecting Key Presses
I haven’t blogged in quite a while, but recently I was inspired by this PowerShell.com PowerTip that gives us a method to detect key presses using low-level Windows API methods. I took this just a bit further and created a function that allows us to specify exactly which key we’d like to test for and will even check for multiple simultaneous keys (a chord).
How to Use Test-KeyPress
Now I can run Test-KeyPress -Keys Ctrl,Shift
and when the function executes if both CTRL and SHIFT are pressed it will return true. If either or both of those keys are not pressed it will return false. Obivously this is not intended to be used interactively - if you’re running commands at the console prompt then you generally won’t have any keys pressed other than ENTER at the time of execution.
So how can this be used you say? I use Test-KeyPress
to make decisions when either the PowerShell console or the ISE are launched. For example, sometimes I want to launch the PowerShell ISE with the ISE Steroids add-on ready to go, but other times I want to load just the base ISE. To do that, I added this to my profile script:
Now when I launch the ISE, if I hold down the control key ISE Steroids will be loaded with no interaction from me. Alternatively you could use Test-KeyPress
to execute just about any code in your profile so if you would like certain modules, variables, etc. to be conditionally available you can make that happen just by holding down a key or combination of keys.
If you have any other ideas for how to use Test-KeyPress
, I’d love to hear them! Hit me up on Twitter to discuss.