X-keys

Download the PowerShell DLL for X-keys support from…

This is version 1.2 and only supports x86 PowerShell and the Key-down event.
Create a shortcut to ISE (x86) on 64 bit systems by using this link…
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell_ise.exe
It requires Microsoft .net 4.5

To use X-keys in a PwerShell script:

add-type -path 'C:\Users\imorrish\Source\Workspaces\VISE_Xkeys\VISE_Xkeys\bin\Debug\VISE_Xkeys.dll'
$PIE = new-object VISE_Xkeys.xkeys
$units = $PIE.GetDevices()
# Note the Device ID's from this list and use below - Unit ID is not yet populating,
#get this from monitoring keystrokes in the event handler loop
$units 

$pie.NewDevice(1) # May change if multiple X-keys plugged in

#To set a UnitID number for DeviceID so you can handle multiple device key detection (the number that prefixes the Key-xx message)
#$pie.SetUnitID(1,10)

#Example writing LED status
$pie.SendData(1,1,1) # DeviceID, Key (*2 for Red), Status 0=off, 1=on, 2=blink
Start-Sleep -Seconds 2
$pie.SendData(1,1,0)

#Event handler for X-keys, 10- is the Unit ID, not to be confused with the DeviceID.
function HandleXkey($key)
{write-host $key
    switch ($key)
    {
    10-Key-1{write-host"Key 1 pressed"}
    10-Key-2{write-host"Key 2 pressed"}
    }
}

Unregister-Event -SourceIdentifier KeyPressed
$MyEvent = Register-ObjectEvent -InputObject $Pie -EventName KeyPressed -SourceIdentifier KeyPressed -Action {HandleXkey($event.sourceEventArgs.KeyPressed)}

sourceEventArgs also provides the key pressed and unit ID as Integers. See my blog post on how to use these in version 1.2.
Do not put any blocking script code in the switch statements as this will prevent new key events from being detected. You can start a PSJob or Runspace if you want to start a long running transaction.

Pressing many keys simultaneously may cause the events to get permanently behind the current keystroke. I hope to address this in a future version along with the key-up event so you can reliably detect a “shift” or “control” action.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s