Sunday, October 30, 2016

Changing Keyboard Shortcut Behavior in PowerShell Studio

I'm a big fan of SAPIEN's PowerShell Studio. The editor is fast and PrimalSense (their equivalent of intellisense) is rock solid, unlike intellisense in the PowerShell ISE, which mostly works...sometimes...if you're lucky.

One thing that I don't like is how the behavior of the F5 shortcut key differs from the ISE - F5 runs the script in their special sandbox session rather than within the shell itself. At the time of this writing, there is no way to change this behavior within the program itself.

Enter AutoHotkey. AutoHotkey is open-source macro-creation and automation software. In this instance, I'm using its macro abilities to send Ctrl+F8 (run script in shell) whenever F5 is pressed, but only when the PowerShell Studio window is active.

I also set up Scroll Lock to send Ctrl+Shift+A (expand aliases to Cmdlets) and Ctrl+Shift+J (format script). If you're wondering about whether or not pressing Scroll Lock for this macro causes it to turn on the light on your keyboard, it doesn't.

First, download and install AutoHotkey. After that, navigate to the directory where you want to store your script and create a new AutoHotkey Script via the right-click menu.

Next, open your newly-created ahk file in your favorite text editor, paste the following code into it and save.

#IfWinActive SAPIEN PowerShell Studio 20
SetTitleMatchMode, 1 ; Title must start with the specified text to be a match

f5:: ; Run script in console
 send, ^{f8}}

Scrolllock:: ; Expand aliases to Cmdlets and format script
 send, ^+a
 send, ^+j

Double-click the file and it will run. If you want to reload the script after making changes, or exit AutoHotkey completely, this can be done by right-clicking the green tray icon.

Once you're satisfied that everything is working as desired, place a shortcut to your ahk file in the startup directory and you're done.

No comments:

Post a Comment