How to trigger events in Lightning Components when keys are pressed

Updated: Jun 9

We are creating a fun project to show the community (more on that later). One of the issues we came up against during our Proof of Concept build was using the keyboard to trigger events (things to happen). Most Lightning Components use buttons to trigger events from user mouse clicks (onClick), not by using a keyboard.


We need to create a 'listener' which is fairly straight forward in javascript, but we were having problems calling it. Turns out, the js function has to be called from the <aura:handler> when the page loads. Once the page loads, the handler calls the listener method, and the listener method uses the addEventListener method out of the box. No more configuration necessary to get these events to trigger. Obviously, you'll want the different keys to do different things but the example below is a working example of how to get a listener up and running and fire when a key is pressed.


To get started with Cloud Pacific, go here to complete a simple form. A Cloud Pacific Account Manager will collaborate with you to clarify your needs and goals, and customize a service package and roadmap that will help your business grow and thrive on Salesforce. Reach out to us here!

131 views0 comments