如下代码,建议用这个,e.keyCode 已经过时,后面都是用 e.key:string.
onMounted(() => {
window.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 's') { // 检查是否按下了 Ctrl + S
e.preventDefault(); // 阻止默认行为(保存网页)
console.log(editDocumentVisible.value);
if (editDocumentVisible.value) {
saveDocument();
}
}
});
});
这段代码演示了在Vue应用中监听键盘事件,特别是Ctrl+S组合键的处理。它阻止了默认的页面保存行为,并在editDocumentVisible为true时调用saveDocument函数。e.keyCode已被弃用,推荐使用e.key。

被折叠的 条评论
为什么被折叠?



