【问题标题】:How to detect ctrl + z and ctrl + y in vuejs?如何在 vuejs 中检测 ctrl + z 和 ctrl + y?
【发布时间】:2020-08-13 12:12:12
【问题描述】:
【问题讨论】:
标签:
vue.js
keyboard
keyboard-shortcuts
keyboard-events
【解决方案1】:
您可以为整个页面设置一个keyup 处理程序。
如果您想在输入之外撤消/重做数据,我认为您必须将每个更改保存在某处,然后在 keyup 处理程序中撤消/重做它。
<div>{{ output }}</div>
data () {
return {
changes: [],
output: ''
}
},
mounted () {
document.addEventListener('keyup', this.keyupHandler)
},
destroyed () {
document.removeEventListener('keyup', this.keyupHandler)
},
methods: {
logChange (string) {
this.changes.push(string)
}
keyupHandler (event) {
if (event.ctrlKey && event.code === 'KeyZ') {
this.undoHandler()
}
else if (event.ctrlKey && event.code === 'KeyY') {
this.redoHandler()
}
},
undoHandler () {
// Get the data from "this.changes" and set the output
this.output = ...
},
redoHandler () {
// Get the data from "this.changes" and set the output
this.output = ...
}
}