0

I have keyboard shortcut that executes some jQuery:

$('body').on('keyup', function(e) {
  if (e.keyCode == 70) {
    $('html').addClass('example');
    $('#example').focus();
    return false;
  }
});

When the user presses f, it'll execute the jQuery.

How can I change the keyCode so instead of it being f, I'd like it to be when the user presses alt + f?

This is what I tried:

$('body').on('keyup', function(e) {
  if (e.keyCode == 18 && 70) {
    $('html').addClass('example');
    $('#example').focus();
    return false;
  }
});

But it didn't work.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
user2203362
  • 259
  • 1
  • 7
  • 11
  • possible duplicate of [How can i detect keyboard modifier(ctrl or Shift) through javascript](http://stackoverflow.com/questions/13539493/how-can-i-detect-keyboard-modifierctrl-or-shift-through-javascript) – James Montagne Apr 09 '13 at 17:05

2 Answers2

1

Keyboard events have attributes for the modifier keys:

if (e.keyCode == 18 && e.altKey) {

}
Wogan
  • 70,277
  • 5
  • 35
  • 35
0

$('#example') selects the ID example. Use $('.example') instead. Also you should use e.keyCode == 70 && e.altKey.

PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58