Reputation: 33
I want to detect when the Alt/Option key and any other key are both pressed down simultaneously in a textarea on Mac. For example, I want to check for Alt/Option + 'h':
HTML
<textarea onkeydown="myFunction(event);"></textarea>
JavaScript
function myFunction(e) {
if (e.altKey && e.key=="h") {
// Do something here
}
}
The function works if I use Control instead of Alt. How can I get this to work with Alt on Mac?
Upvotes: 1
Views: 707
Reputation: 628
You can do this by directly ASCII value e.keyCode === 65
instead of checking by e.key == 'A'
function myFunction(e)
{
if ( e.altKey && e.keyCode === 65) //it check both altKey + 'A' or 'a'
{
//do something
}
}
i just did this for alt + a press. but you can do with any key by getting it's ASCII values
JsFiddle link for demo
Upvotes: 1