I'm kind of new to JS, I have searched the internet and I haven't got what I'm looking for, I want to run a function if the keys Shift and Enter were pressed, like a shortcut, I have tried this but I think I killed JS with this code
document.addEventListener('keypress', function (e) {
if (e.key === 'Enter' 'Shift') {
console.log("test");
}
});
anything would be helpful, thanks.
CodePudding user response:
You can use e.shiftKey
to see if shift is being pressed.
document.addEventListener('keypress', function(e) {
if (e.key === 'Enter' && e.shiftKey) {
console.log("test");
}
});
CodePudding user response:
This is a pretty expandable solution because you can create shortcuts for many different keys.
const keysDown = {};
document.addEventListener('keydown', ({ key }) => {
keysDown[key] = true;
if (keysDown.Shift && keysDown.Enter) console.log("test");
});
document.addEventListener('keyup', ({ key }) => {
keysDown[key] = false;
});