I recently came across a requirement to close a Modal on Esc key press (508 compliance). I then realised that the onKeyDown handler I wrote on my react component wasn't working as expected. The event wasn't getting fired on clicking Escape key from my macbook pro's touch bar. Has anyone gone through similar issue ? If yes, is there a workaround ?
Asked
Active
Viewed 546 times
0
-
could you share the code for more clarification? – Jineesh 619 Jan 25 '22 at 11:45
-
https://stackoverflow.com/questions/53345866/apple-touch-bar-keys-not-firing-keypress-event You can use keydown or keyup instead of keypress – hasanqqsp Jan 25 '22 at 12:01
2 Answers
0
You should write onKeyDown={(e) => yourFunctionName(e)}
on the Modal element and your function should look like something like this
const yourFunctionName = (e) => { if (e.key === "ENTER") { closeModal() } }

Liuron
- 11
- 3
-1
Make sure Esc key-code is 27
func((e) => e.keyCode === '27'){
//your code }

Tony Corsair
- 7
- 1
-
[keyCode](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode) is deprecated – pilchard Jan 25 '22 at 12:14