js_solutions 0 Posted July 11, 2020 Report Share Posted July 11, 2020 Hi, Trying to detect altKey combinations in a text area, but it's not working. In the following example, the code detects ctrl-a, but not alt-a. Why? <!DOCTYPE html> <html> <head> <script> function myFunction(e) { if(e.altKey && e.key=="a") // option-a is not detected console.log("alt-a"); else if(e.ctrlKey && e.key=="a") console.log("ctrl-a"); // ctrl-a is not detected } </script> </head> <body> <p>Press ctrl-a inside the text area. Then, press option-a (alt-a) inside the text area.</p> <textarea id="demo" onkeydown="myFunction(event);"></textarea> </body> </html> Quote Link to post Share on other sites
JMRKER 10 Posted July 20, 2020 Report Share Posted July 20, 2020 Ctrl-A is a defined event by the browser. Similar to using Ctrl-X and Ctrl-V to cut-and-paste. I don't think your "myFunction(e)" is checking either keypress as written. Quote Link to post Share on other sites
shaili_shah 1 Posted July 31, 2020 Report Share Posted July 31, 2020 Hii, Good morning. This code works with both keys. Thanks! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.