# Perspective Key Event Regex New Line

**URL:** <https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [October 2, 2021, 6:01am UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553 "2021-10-02T06:01:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![deon.korb](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@deon.korb](https://forum.inductiveautomation.com/u/deon.korb)\
**Post date:** [October 2, 2021, 6:01am UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/1 "2021-10-02T06:01:37Z")

</div>

Hi,

I’m trying to catch an RFID read using the Perspective key events to go straight to the idP login.

The RFID badges are an 8 - 10 character hex code followed by a line feed. While I can catch the code, as soon as I try include the line feed, nothing works.

This is the regex: `[A-Fa-f0-9]{8,10}[\n?\r]` and it works perfectly in regex testers, such as [regexr.com](http://regexr.com).

Any ideas?

Regards,  
Deon

---

<div class="post-metadata">

**Author:** ![deon.korb](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@deon.korb](https://forum.inductiveautomation.com/u/deon.korb)\
**Post date:** [October 5, 2021, 3:27pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/2 "2021-10-05T15:27:33Z")

</div>

Has anyone else successfully matched a string with a line feed?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 5, 2021, 4:30pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/3 "2021-10-05T16:30:07Z")

</div>

> [@deon.korb](#):
>
> `[A-Fa-f0-9]{8,10}[\n?\r]`

That doesn't look like the correct pattern. If you're looking for an OS-agnostic linefeed, you would want:  
`[A-Fa-f0-9]{8,10}\r?\n` - Windows uses CRLF, so `\r\n`, Unix uses `\n` alone. You explicitly _don't_ want a character set `[]` around that, since the square brackets change the escaping logic such that your pattern is asking for a hex string followed by any one character `\n`, `\r`, or `?` - not an optional newline.

---

<div class="post-metadata">

**Author:** ![kcollins](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kcollins/32/19646_2.png) [@kcollins](https://forum.inductiveautomation.com/u/kcollins)\
**Post date:** [October 5, 2021, 4:43pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/4 "2021-10-05T16:43:42Z")

</div>

Building on that, if you want to still only match the code but retain the condition where it is followed by a new line, you can leverage a lookahead assertion, e.g. `[A-Fa-f0-9]{8,10}(?=\r?\n)`.

---

<div class="post-metadata">

**Author:** ![deon.korb](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@deon.korb](https://forum.inductiveautomation.com/u/deon.korb)\
**Post date:** [October 6, 2021, 6:33am UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/5 "2021-10-06T06:33:55Z")

</div>

I’ll be honest, I only use regex in self defence.

It still does not seem to pick up the new line. I tried `[A-Fa-f0-9]{8,10}\r?\n` and `[A-Fa-f0-9]{8,10}(?=\r?\n)`

If I change it to key down event on Enter, it does recognise the enter key as seen below:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/6/d6d6230cd6d93b868c8425a0c4d3f6b979540d86.png)

`system.util.getLogger('RFID').info('RFID key: ' + str(event.key))`

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/0/c0db5bc2a37cc662f51108c036855d65f115545d.png)

Is the enter key perhaps different to a new line and needs to be captured in another way?

For what it’s worth, I’m using the RFIdeas PCProx USB keystroking reader to output the card serial number.

---

<div class="post-metadata">

**Author:** ![deon.korb](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@deon.korb](https://forum.inductiveautomation.com/u/deon.korb)\
**Post date:** [October 7, 2021, 3:29pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/6 "2021-10-07T15:29:37Z")

</div>

I’m convinced there is something strange here.

If I change my regex pattern to `[A-Fa-f0-9]{8,10}\s` it should match on any whitespace, including line feeds, tabs etc.

It doesn’t seem related to the RFID reader; I can’t get it to work on a keyboard either.

If I type ‘123456789’ followed by a space, I get a match. I type ‘123456789’ followed by either enter or tab, I don’t get a match.

Simplifying it even further, I can’t get `.\s` to match a character followed by enter, only on space.

Any help matching the pattern with an enter at the back would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 7, 2021, 4:18pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/7 "2021-10-07T16:18:30Z")

</div>

Ugh, you’re right, there _is_ something strange here. It seems that the regex is matching against some string representation of the typed key, not the actual underlying character. A regex like this works when you press enter:  
`.+(Enter)`  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/4/f496dc49ae86ea07d23a53fe2e0ec3193462c408.png)

---

<div class="post-metadata">

**Author:** ![deon.korb](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@deon.korb](https://forum.inductiveautomation.com/u/deon.korb)\
**Post date:** [October 8, 2021, 7:29am UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/8 "2021-10-08T07:29:27Z")

</div>

Your workaround did the trick. Is this a bug or just a strange browser effect?

I have decided to implement the regex as `([0-9a-fA-F]{8,10})(?=Enter|\r?\n)`, so that it will match the line feed if this behaviour changes in future.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 8, 2021, 3:27pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/9 "2021-10-08T15:27:04Z")

</div>

I’m of the opinion this is a bug (and filed a ticket to that effect) but there may be some underlying browser…strangeness, I’ll say, at work here that I’m not aware of.

---

<div class="post-metadata">

**Author:** ![jball](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jball/32/11128_2.png) [@jball](https://forum.inductiveautomation.com/u/jball)\
**Post date:** [October 8, 2021, 4:49pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/10 "2021-10-08T16:49:58Z")

</div>

This is expected behavior, all. At the core of the regex match is the data that we receive from a [JavaScript KeyboardEvent](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent) because this executes in the scope of the browser. We’re assembling a character sequence to match against from an input source (your barcode scanner) that creates KeyboardEvents. A “new line” character shows up as a key press for the “Enter” key.

[https://keycode.info/](https://keycode.info/) is a pretty decent way to test what KeyboardEvent data your sequence would produce. It tests a key at a time, but knowing what your sequence looks like, you could type it out on your keyboard to generate the KeyboardEvent and observe the `event.key` value in the tool.

[Check out the docs site for more info](https://docs.inductiveautomation.com/display/DOC81/Perspective+Session+Event+Scripts#PerspectiveSessionEventScripts-KeyEvents).

---

<div class="post-metadata">

**Author:** ![SBL\_MC](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sbl_mc/32/56813_2.png) [@SBL\_MC](https://forum.inductiveautomation.com/u/SBL_MC)\
**Post date:** [February 28, 2022, 2:31pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/11 "2022-02-28T14:31:32Z")

</div>

Hi Deon,

I have a question regarding your scanning solution. Do you always select a input field before you scan the RFID tag, or are you able to trigger the Key event without?

Thanks.

---

<div class="post-metadata">

**Author:** ![deon.korb](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@deon.korb](https://forum.inductiveautomation.com/u/deon.korb)\
**Post date:** [February 28, 2022, 4:55pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/12 "2022-02-28T16:55:32Z")

</div>

It continuously matches and calls `system.perspective.login` on matching 8-10 hexadecimal characters followed by enter.

It would also match and call the login if a user were to type this, we deemed that unlikely enough. The convenience of just tapping twice is more than worth it.

I would have preferred an existing session to be logged out if another user taps, but that is not the behaviour as described in this thread:

> [@Ignition idP Force Auth Behaviour](https://forum.inductiveautomation.com/t/ignition-idp-force-auth-behaviour/51554/3):
>
> Thanks. The onus is on the currently logged on user to log out when he is done anyway.

---

<div class="post-metadata">

**Author:** ![imt.sebastian.sanche](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@imt.sebastian.sanche](https://forum.inductiveautomation.com/u/imt.sebastian.sanche)\
**Post date:** [October 19, 2023, 10:29pm UTC](https://forum.inductiveautomation.com/t/perspective-key-event-regex-new-line/51553/13 "2023-10-19T22:29:08Z")

</div>

Hi, I've tried a couple of places but where do I configure the keystroke? I have a similar deployment as the original post but with an actual barcode scanner. The scanner is set to scan then send and send an Enter keystroke.

My regex is as follow:` [5|R][0|T]...........\d\d\d\d(?=Enter)`

Then I'm binding the first array of data to a string tag that is on a PLC but when I scann it doesn't write anything to it.
