# 快捷键
# 1. 说明
document.addEventListener('keydown', (e) => {
// I will add logic later
});
Here, I am listening to keydown event.
There is another event called keyup.
You can also use this event.
But sometimes keyup event creates conflict with the default shortcuts.
It is safe to use keydown event.
# 2. 示例
document.addEventListener('keydown', (e) => {
if (e.key.toLowerCase() === 's' && e.ctrlKey) {
e.preventDefault();
// Add your code here
shortcutForCrtlS();
} else if (e.key.toLowerCase() === 'p' && e.shiftKey) {
// Add your code here
shortcutForShiftP();
} else if (e.key.toLowerCase() === 'i' && e.altKey) {
// Add your code here
shortcutForAltI();
}
});
# 3. 参考
上一篇: 下一篇:
本章目录