Reputation: 938
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;
}
}
}
Upvotes: 1
Views: 783
Reputation: 2830
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.
Upvotes: 2
Reputation: 2883
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.
Upvotes: 0