Custom C# scripts in 232key Pro

232key Pro data flow

232key Pro Data Flow

232key Pro works with data strings or “lines of data”. A line is created every time the Terminator character is found in the stream of bytes received from your device (or when no additional bytes have been received for the duration of the Timeout, if set).

Next, 232key Pro attempts to match a regular expression (regex) in each line. The results of this match are made available to your C# script in the variables described below.

Your script runs every time a match is found. It can transform this data in any way you want using the C# language. At he end of your script, return a string that 232key should type, e.g:

return value+'\n';

This is the default script in 232keyPro. It simply takes the value variable (see below) and adds a new line character (which will be transformed into the ENTER key).

Note: The Encoding set under Settings > General Settings is used when the bytes received from your device are first converted to a string.

Variables with captured data

  • String value: The string matched by the first capturing group of the regex (or an empty string if nothing was captured). This string is highlighted in blue in the Event Log. Most tasks can be solved by using the value string.
  • Example with four lines of data sent from a scale when the “Generic measuring instrument” device profile has been selected in the Input tab:
    content of first capturing group highlighted in event logThe regex associated with this device profile captures the first number it encounters in each line (in this case, the weight, e.g.     8.738). Your script would run once for every line and the value variable would contain the following values:
    1.     8.738
    2.     8.736
    3.     8.736
    4.     8.738
  • Match firstMatch: The first match of the regex (or null if no match was found). Useful to access multiple capturing groups defined in a custom regular expression: firstMatch.Groups[1].Value corresponds to value (but can be null), firstMatch.Groups[2].Value is the string matched by the second capturing group, etc.
  • MatchCollection allMatches: All matches of the regular expression (or null if no matches were found). Use this when the regex matches more than once (and you want to access all matches).

Tip: If you’d rather parse all data in your script instead of dealing with regular expressions, you can set the Device in the Input tab to “Generic text device“. This will use a regex which matches all characters except for line feed and carriage return. If you want those as well, change the regex to “([\s\S]+)” (without the quotation marks). All data sent from the connected device will then be available in the value string.

Using the ([\s\S]+) regex, the same input as in the example above would look like this in the Event Log, showing that all data sent by the scale has been matched:
matching all data sent from a scale

Functions

Other than using regular C# code, you can call the following functions from your script to simplify common tasks.

NumberFormatter.Format – formats a string as a number

string Format(string capturedString, bool useComma, NumberStyles numberStyles)

This function trims white spaces and removes leading zeros. The decimal separator can be set to a dot or comma.

Parameters

  • capturedString String: The string to format as a number (usually the value variable).
  • useComma bool: If true, use a comma as the decimal separator. If false, use a decimal point.
  • numberStyles enum System.Globalization.NumberStyles: Style to use for formatting (you’ll usually want to use NumberStyles.Number)

Returns

String: Formatted string or original string if number conversion fails.

Script example

return NumberFormatter.Format(value, true, NumberStyles.Number);

Formats captured data as number with a comma as the decimal separator and returns it to 232key Pro for output.

Constants

For your convenience, the following constants are available to your script:

  • ESC: The escape character (‘\u001b’). Meant to be used with escape sequences (see examples below).

User variables

You can define any variable you want in your script*, but the variable will be re-initialized every time the script runs, which is every time a line of data is processed. There’s currently no way to avoid if we also want to use extremely fast compiled scripts.

To maintain state between script executions, the following arrays with 10 elements are available:

  • bool[10] Bools
  • int[10] Ints
  • double[10] Doubles
  • string[10] Strings
  • decimal[10] Decimals

The variables are maintained even when the script is stopped and restarted. However, they are lost when the program is closed.

*Note: You cannot use reserved keywords or the following names for your own (temporary) variables: allMatches, Bools, Decimals, Doubles, ESC, firstMatch, Ints, match (alias for value), Strings, value.

Script example

Ints[0]++; //Increase by one
return $"{Ints[0]}: {value}\n";

Note: The dollar sign ($) is not typed, it means that the string literal following it may contain {interpolated expressions}. Instead, you could also write:

return Ints[0]+": " +value+'\n';

Example output from a weighing scale:

1:       8.234
2:       2.686
3:      26.740

Tip: Wondering where the blank spaces in the output are coming from? Except for the one we’ve explicitly added, they were already present in the value variable as the regex defined for this device had captured them.

Adding characters

There are currently two ways to add characters which 232key Pro will then convert to keyboard events.

Using the default output settings

A keyboard event is created for each character in the string returned from your script. The Unicode flag is set for all characters with the exception of tab (\t) and line feed (\n, translated to the ENTER key), which means that you don’t have to worry about keyboard layouts and modifiers.

Script example

return $"中間值: {value}\n";

Types “中間值: ” (average) in front of the content of the value variable and then presses the enter key.

Example output from a weighing scale

中間值:       8.738

Using GIDEI

To type non-alphanumeric keys and key combinations, set the Output Method in the Settings tab to GIDEI and use one of the GIDEI escape sequences described below.

Alphanumeric keys A-Z and the non-alphanumeric keys listed below are currently supported.

Note: Outside of GIDEI escape sequences, all characters are supported just like in the default output mode!

Detailed information on GIDEI (also implemented as Windows SerialKeys up to XP) can be found in this document.

GIDEI support in 232key is currently incomplete and has to be considered experimental.

Individual keys

Start the escape sequence with the ESC character (ASCII 27), followed by one key name and a period (ASCII 46). White spaces are optional:
ESC Keyname .

GIDEI script example with multiple individual keys

return $"{ESC}delete.{value}{ESC}tab.{ESC}tab.{ESC}enter.";

Presses the DEL key ahead of the content of the value variable, then TAB twice and finally ENTER. Each GIDEI escape sequence is shown in a different color.

Key combinations

232key currently supports the Combine command to create key combinations.

A key combination consists of the escape character (ASCII 27), the combine command and the key names, separated by commas. It ends with a period (ASCII 46):
ESC , combine [ , Keyname1 ] [ , Keyname2 ] [, Keyname3 ] [ , etc] .

GIDEI script example with key combinations

return $"{ESC}, combine, control, f.{value}";

Presses Ctrl + F and then types the content of the value variable.

Non-alphanumeric keys (modifiers, navigation, editing, …)

GIDEI Key NameDescription
altALT key
backspace
bspace
BACKSPACE key
control
ctrl
Left CONTROL key
enterENTER key
f1…f12F1…F12 function keys
deleteDEL key
downDOWN ARROW key
escapeESC key
lcontrol
lctrl
Left CONTROL key
(same as control)
leftLEFT ARROW key
lshiftLeft SHIFT key
menuALT key
(same as alt)
rcontrol
rctrl
Right CONTROL key
returnENTER key
(same as enter)
rightRIGHT ARROW key
rshiftRight SHIFT key
tabTAB key