简体   繁体   中英

shortcut key not working ctrl + shift + numpad1

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;
    }
  }
}

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.

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.

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

这有效

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM