X-keys editor to create JSON file

I have started working on a JSON schema to support the X-keys options that can be performed in PowerShell for each button. Originally I was thinking of doing this with an XML file, which would have worked fine, but the purpose of exercise is also to learn some new technology.

The JSON file format looks like this:

{
"keys": [
{
"Type": "Script",
"KeyID": 0,
"Command": " ",
"Script": " $me[0].Program=1",
"Caption": "Prog 1",
"Description": " Set Program bus to input 1"
},
{
"Type": "Script",
"KeyID": 1,
"Command": " ",
"Script": " $me[0].Preview=2",
"Caption": "Preview 2",
"Description": "Set Preview bus to input 2"
}
]
}

And the tool that saves you from having to create this in Notepad can be downloaded from here

It is very simple and just a temporary solution until my full WPF based host is ready (long term project so I’m not holding my breath, neither should you).

xkeysjsoneditor

The JSON file can be loaded in PowerShell and converted to a Hashtable with the following code…


$xk60File = ConvertFrom-Json (get-content "C:\Users\imorrish\OneDrive\PowerShell\V-ISE\test.json" -raw)
$xk60Commands = @{}
foreach($key in $xk60File.keys){
$xk60Commands.add($key.KeyID,$key.Script)
}
# this is the function called by the KeyDown event handler
function HandleXkey($key) {
if($xk24Commands.ContainsKey($key))
{
invoke-expression  $xk60Commands.Get_Item($key)
}
}

Note: if you want to store a file path or anything with a \ character, you have to double it up.
If you want to learn more about JSON, check out this article https://channel9.msdn.com/Series/Windows-Store-apps-for-Absolute-Beginners-with-C-/Part-11-Working-with-JSON-Data

Updated X-keys for PowerShell

I have created a new version that provides some better options for event arguments.

You can now key the key number and Unit ID as Integers within the event handler function. The original string value is still there also.

The new values are:

  • $event.sourceEventArgs.KeyID
  • $event.sourceEventArgs.UnitID

This makes it easier to use a Switch statement for the unit ID so you can deal with multiple X-Keys and have different actions on the same key ID’s.

Example script to show the UnitID and KeyID for the KeyPressed event

<br data-mce-bogus="1">

add-type -path 'C:\Users\imorrish\onedrive\powershell\x-keys\VISE_Xkeys.dll'
$PIE = new-object VISE_Xkeys.xkeys
$keys = $PIE.GetDevices()
$keys #Get the Unit ID's from this output and create a NewDevice for each

$pie.NewDevice(0)
$pie.NewDevice(3)

function HandleXkey($key, $device) 
{
    write-host $key $device
}
$MyEvent = Register-ObjectEvent -InputObject $Pie -EventName KeyPressed -SourceIdentifier KeyPressed -Action {HandleXkey($event.sourceEventArgs.KeyID,$event.sourceEventArgs.UnitID)}

If you add another X-keys device once the script is running, just run

$PIE.GetDevices()

and $pie.NewDevices(xxx)

where xxx is the ID of the new device, and it will be included in the existing event notification.