Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

Trap ESC key in a modal

Asked by Anil Mer Jul 13, 2021 606 views 2 answers
Share

About this question

I want to override the behavior of the ESC key in my LWC while a modal is being displayed. Default behavior passes the ESC key down to the calling component and closes both the modal and that component. I can trap the ESC in the modal with this code:

@track openModal = false; connectedCallback() { this.template.addEventListener('keydown', event => { var keycode = event.code; console.log('keycode',keycode); if(keycode == 'Escape'){ event.preventDefault(); event.stopImmediatePropagation(); } }, true); } showModal() { this.openModal = true; } closeModal() { this.openModal = false; }

HTML

<!-- =============== Information Modal =============== --> 
Once I've trapped the ESC key, I want to close the modal. I'm not sure how to make that happen.
How to trap esc keycode in a model?

 

Your answer

2 Answers

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.