Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
Created August 22, 2016 02:23
Show Gist options
  • Save mattmcnabb/93033227b4629cf696ea53af4b1ac3ed to your computer and use it in GitHub Desktop.
Save mattmcnabb/93033227b4629cf696ea53af4b1ac3ed to your computer and use it in GitHub Desktop.
Blog_Customize-The-ISE-With-Hotkeys
#requires -Version 3
function Test-KeyPress
{
<#
.SYNOPSIS
Checks to see if a key or keys are currently pressed.
.DESCRIPTION
Checks to see if a key or keys are currently pressed. If all specified keys are pressed then will return true, but if
any of the specified keys are not pressed, false will be returned.
.PARAMETER Keys
Specifies the key(s) to check for. These must be of type "System.Windows.Forms.Keys"
.EXAMPLE
Test-KeyPress -Keys ControlKey
Check to see if the Ctrl key is pressed
.EXAMPLE
Test-KeyPress -Keys ControlKey,Shift
Test if Ctrl and Shift are pressed simultaneously (a chord)
.LINK
Uses the Windows API method GetAsyncKeyState to test for keypresses
http://www.pinvoke.net/default.aspx/user32.GetAsyncKeyState
The above method accepts values of type "system.windows.forms.keys"
https://msdn.microsoft.com/en-us/library/system.windows.forms.keys(v=vs.110).aspx
.NOTES
Original idea found here at http://powershell.com/cs/blogs/tips/archive/2015/12/08/detecting-key-presses-across-applications.aspx
.INPUTS
None - Test-KeyPress does not accept pipeline input.
.OUTPUTS
System.Boolean
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[System.Windows.Forms.Keys[]]
$Keys
)
# use the User32 API to define a keypress datatype
$Signature = @'
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern short GetAsyncKeyState(int virtualKeyCode);
'@
$API = Add-Type -MemberDefinition $Signature -Name 'Keypress' -Namespace Keytest -PassThru
# test if each key in the collection is pressed
$Result = foreach ($Key in $Keys)
{
[bool]($API::GetAsyncKeyState($Key) -eq -32767)
}
# if all are pressed, return true, if any are not pressed, return false
$Result -notcontains $false
}
if (Test-KeyPress -Keys ControlKey) { Start-Steroids }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment