I am trying to implement redo and undo in a browser. I have a solution that works, but it sometimes misinterprets the redo sequence as undo, whereas this never happens, for example, in vscode. My code is as follows:
document.addEventListener(
'keydown',
async function(evt) {
evt.stopImmediatePropagation()
if (
evt.code === 'KeyZ' &&
(evt.ctrlKey || evt.metaKey) &&
evt.shiftKey
) {
// handle redo action
} else if (evt.code === 'KeyZ' && (evt.ctrlKey || evt.metaKey)) {
// handle undo action
}
})
I am aware of other questions similar to this one, but they all offer solutions similar to mine. I am specifically looking for a solution that will never misinterpret an undo/redo, so that, for example, a sequence of 50 actions can be undone and redone with perfect precision (as is the case in vscode, and probably countless other browser-based tools).