New Feature: Perspective Key Events

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 the document 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 interface keydown/keyup events from running that have been bound deeper in the DOM than the document level.

image

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:

image

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.

8 Likes

Oooo! Like!

2 Likes

I wish we have key event stroke for individual page or view not the whole session.

We have a lot of case in a single project that each page should behave differently for arrow key events.

1 Like

As part of the event data, you’ve got access to the pageId and path, so you’ll be able to create different interactions based on the page being displayed. The session is there for you as well (in case you need it).

Good. But if we have this in view config section it will capsulated every thing in single view file and easy to manage.
Now configuration is in differnet part and hard to manage after sometime.
when you want to export a page to other project it may overwrite the new project event script as well. So I have to copy and paste my event code by hand to prevention overwrite other page event script.

Can we use the key event to conbine a keydown and a mouse click. For example a control keyDown and right click of a mouse open a pdf.

regards

You don't need this feature for that. Mouse events already have a ctrlKey property; you would process every mouse event on your component, then check if ctrlKey is set to drive your additional logic:
https://docs.inductiveautomation.com/display/DOC81/Perspective+Event+Types+Reference#PerspectiveEventTypesReference-MouseEvents

hi @PGriffith,

thanks for your reply. I will test that out.

Regards