0

I need to implement a simple functionality on keyboard shortcut but combination with 'shift' key not work :( .

 window.onkeydown = (event) => {
  if (event.ctrlKey && event.shiftKey) {
    switch (event.key) {
      case '1':
        // something
        break;
    }
  }
}
Filip Laurentiu
  • 854
  • 3
  • 9
  • 29

3 Answers3

2

The key code switches to an exclamation point because SHIFT is being held. Changing the case in your switch to '!' from '1' is a possible solution. However you may be using a numpad. I would recommend avoiding the key in this scenario and just getting the event.code.

window.onkeydown = (event) => {
    if (event.ctrlKey && event.shiftKey) {
        switch (event.code) {
            case 'Digit1':
                alert()
            break;
        }
    }
}

I hope this helps.

Jesse
  • 2,790
  • 1
  • 20
  • 36
0

When you are pressing Shift, the event.key will be the character that was pressed. In your case when you press Shift + 1 it will be !.

One possible solution would be to use event.code property. For numpad it will give Digit1, Digit2, etc.

0
 if (event.ctrlKey && event.code === 'Numpad1')

this works

Filip Laurentiu
  • 854
  • 3
  • 9
  • 29