Custom C# scripts in 232key Pro

232key Pro Data Flow

232key Pro Data Flow

232key Pro works with data strings or “lines of data”. This string is created when the Terminator character is found in the stream of bytes received from your device (or when no new data has been received for the duration of the Timeout).

It then attempts to match a regular expression (regex) in each line of data.

Variables with captured data

The results of this match are made available to your C# script in the following variables:

  • 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. Example with four lines of data sent from a scale:
    content of first capturing group highlighted in event log
    Many simple scripts will only require the value string.
  • 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 by 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

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

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.

Constants

  • char ESC: Escape character (ASCII 27)

Functions

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

Format captured data as number with a comma as decimal separator and return it to 232key Pro for output:
return NumberFormatter.Format(value, true, NumberStyles.Number);

Constants

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

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

Output from your script

Your script has to return a string to 232key Pro which will then be converted to keyboard events.

Default Output

A keyboard event is created for each character in the string returned from your script and then passed to the event queue (input buffer). 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.

Example script

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

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

Example output:

中間值:       8.738

GIDEI Output

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.

Typing single 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 Key Name .

Script example to press the DEL key ahead of the content of the value variable:

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

Key combinations

232key currently supports the Combine command only.

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 [ , Key Name 1 ] [ , Key Name 2 ] [, Key Name 3 ] [ , etc] .

Script example to press Ctrl + F and then type the content of the value variable:

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

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

GIDEI Key NameDescription
altALT key
backspace
bspace
BACKSPACE key
control
ctrl
Left CONTROL key
enterENTER key
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