Similar in functionality to Vision’s Keystroke Scripts, we have introduced Key Events into Perspective’s Session Events, which can be found in the latest nightly build. These events can be found under the Project > Session Events menu item:
This functionality wraps a KeyboardEvent that is generated by the browser and provides the ability to match either a single key (with or without modifiers), or a regex pattern (series of keys) and execute a script once the match has been detected. The match for a single key can be made either when the key has been pressed (keyDown) or when the key has been released (keyUp). A regex match is always made when the key has been released.
Single Key
For single keys entered, the match can be a physical key typed, or the code generated by the key. Modifiers are additional keys that can be held down during a KeyboardEvent
and are detectable in the event data. These include alt, control, shift, and meta (command). In addition, the resulting KeyboardEvent
that is used for match detection has the following advance features that can be utilized:
-
Capture Phase - Lets the execution of the event happen during “event capturing”, which will allow the resulting script to execute before other interface events that may be in place. By default, events execute during an “event bubbling” phase which is dictated by the position of the event binding in the Document Object Model (DOM). In the case of Key Events, these are bound to the
document
layer of the DOM and will execute after other interface events if this option is unchecked. -
Prevent Default - Prevents any default browser functionality that would be associated with the event binding. This can be toggled on if there is any functionality that should not be run when a
keydown
/keyup
event has been fired at thedocument
level when a key match is made. -
Stop Propagation - Prevents the event from going any further beyond the point in the DOM where it is bound (the
document
level). This can be especially useful if combined with the Capture Phase option as it will prevent any interfacekeydown
/keyup
events from running that have been bound deeper in the DOM than thedocument
level.
Regex Pattern
For a regular expression (regex) pattern match, the match will be made based upon a supplied regex pattern and a series of keys that have been stored in a match buffer. While there are a variety of use cases for this type of key event detection, the primary use case that we targeted with this feature was scanning barcodes from an HID barcode scanner.
To create a regex pattern to match against, an online tool like Regexr can be very helpful to test for pattern validity. Once a pattern is entered, the match buffer will need to be set. This buffer will always be the most recently typed characters and should be the length of whatever a “correct match” would be based on the supplied regex pattern to minimize the amount of processing that is done during key entry in a Perspective session.
Example Barcode Pattern Match
The Pattern field in example below contains a regex that matches a #
character, then 12 numeric characters, and then a @
character. Additionally, the match is made at the end of the match buffer, as denoted by the $
character used as an anchor. The total number of characters that this regex pattern is meant to match is 14
, so that is used as the value for the match buffer:
Note: Key event detection is meant to work inside of a Perspective Session and is not active inside of the Designer.
Also to Note: The device being used to trigger the key events must generate a KeyboardEvent
when a key is pressed. HID barcode scanners will generate a KeyboardEvent
as they mimic a user typing keys on a physical keyboard. Scanners and other input devices that do not create a KeyboardEvent
will not trigger this logic.