I'm making a screen and it requires a user to scan their badge on a RFID reader that acts as a keyboard input. the problem is I don't want the users to be able to see their own badge ID on the screen. ideally, it would be pressing a button to run the script to accept the RFID scan and set it to a custom property.
I was able to make something somewhat functional with setting focus on a text area and leaving the text the same color as the background, but this requires the text area to be on the screen and visible which in my opinion is not a good practice. is there any way to make a text input without a text area?
I was using a hidden text input for scanning QR codes, but the focusing was clunky and error prone, and Claude helped me improve it using Key Events:
AI content
You don't need a text field at all. Use a session Key Event (Project > Session Events > Key Events). We do this for QR scanners that type like a keyboard, and it works the same for a keyboard-wedge RFID reader.
The Key Event
Add a key handler and set it to Regex mode.
Pattern: ^([\s\S]*)Enter$ (assuming your reader ends each read with Enter, as most do).
Match on the key, not the code. The regex buffer is built from event.key names, so Enter shows up as the literal text Enter.
The client keeps a rolling buffer of every key pressed in the session. When a read ends in Enter, the handler fires and the badge is in event.matches[1]:
def onKeyEvent(page, event):
import re
if event.key != 'Enter' or not event.matches or len(event.matches) < 2:
return
# The buffer can also hold whatever someone typed before the read,
# so take the badge off the end and check its format.
m = re.search(r'(\d{8,10})$', event.matches[1]) # adjust to your badge format
if m is None:
return
system.perspective.sendMessage('badge-scanned', payload={'badge': m.group(1)},
scope='page', pageId=page.props.pageId)
On the view, add a message handler for badge-scanned (scope: Page):
Nothing is on screen and there's no focus to manage. The message is page-scoped, so only the page that has the handler acts on a read. Badge reads on any other page are ignored.
If you still want a button first, have the button set self.view.custom.waitingForBadge = True. Then the handler returns early unless the flag is set, and clears it after it stores the badge.
One gotcha: this only works if the reader sends real key presses. Some devices, mostly Android handhelds, "paste" the text instead. Browsers report pasted keys as Unidentified, and the Key Event never matches. A USB keyboard-wedge reader is fine.
I'm confused on why you would not use the barcode scanner input component for this. It has a built-in regex prop that can be configured and comes with an onActionPerformed event.
Honestly, I didn't consider it. I went straight from the hidden text field to Key Events. Looking at it now, the Barcode Scanner Input would have worked for my QR scanning too.
A few reasons I'd still lean toward the Key Event, especially for the badge question:
Nothing on screen. The component is a visible element, and it has a dataStyle for the scanned value, so it looks like it shows what was scanned. Keeping the badge ID off the screen is the whole point of the question. I haven't tested whether it still listens while hidden. If it does, that would work too.
Raw keys in a script. Our Android handhelds (Firefox) slip stray Unidentified key events into scans, even in keyboard mode. The Key Event hands the script the raw buffer, so I strip those before parsing. I don't know how the component's JavaScript regex would deal with them.
One handler for the whole session, instead of a component on each view that takes scans.
So both work. The Key Event is what I'd pick when the scanned value shouldn't show up anywhere.
In my case, keeping the input focused properly was the real issue. I'm sure the Barcode Scanner Input could be made invisible, but it wouldn't work if it didn't maintain focus.
I did not know that. I told Claude, and he's still trying to justify his decision.
AI content
Agreed, it doesn't need focus, so it would have fixed my focus problem just as well. I just didn't know about it when I switched.
Where I still see a difference is what ends up on screen. The component is a visible element with a dataStyle for the scanned data, and the OP's goal is to keep the badge ID off the screen. A Key Event never renders anything. If the component still listens when it's hidden, or doesn't show the value, then it's a fine option for the OP too.