为什么 Google Chrome 使用 ASCII 代码 26 和 25 而不是 Ctrl+90/122 和 Ctrl+89/121 来响应 Undo/Redo?
Why is Google Chrome responding to Undo/Redo with ASCII codes 26 and 25 instead of Ctrl+90/122 and Ctrl+89/121?
我正在尝试编写一个带有事件侦听器的 jQuery 文件,以响应多个键盘事件,包括 Ctrl+Z、Ctrl+Y、Ctrl+X 等。目前,我已经开始研究撤消和重做功能,并且在 Firefox 49 中都可以正常工作,但在 Chrome 53 中它们似乎都不正确(但可以预测)。
Firefox 49 可以完美地正确处理撤消和重做,但 Chrome53 似乎监听与 Firefox 不同的 ASCII 键码。最重要的是,Chrome 只决定在键入 Ctrl+Z 两次后处理该 ASCII 代码,因为它似乎完全忽略了第一次尝试。
下面是我的代码片段:
$(document).on('keypress', ':input', function(event) {
if(event.ctrlKey && (event.which === 88 || event.which === 120)){ // cut handled in another event
// don't transmit Ctrl+x or Ctrl+X
return;
}
else if(event.ctrlKey && (event.which === 89 || event.which === 121)){
// todo handle Ctrl+y or Ctrl+Y (redo)
}
// Google Chrome
else if(event.which === 25){ // Chrome seems to want me to use ASCII code 25
// todo handle Ctrl+y or Ctrl+Y (redo)
}
// undo done similar to redo
}
我的两个问题如下:
为什么 Chrome 不想使用与 Firefox 相同的 ASCII 代码?两者都是发布时的最新版本,但两者似乎都想以不同的方式处理基本操作。
如何让 Chrome 在第一次使用快捷键时执行 undo/redo,而不是必须输入两次,这显然不是预期的。
提前致谢。
如评论中所述,使用 keyup 而不是 keypress,您将收到 'correct' 代码。
In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.
你可以检查这个small script
的区别
$(document).on('keyup', ':input', function(event) {
// switching to keyup will return the same keycode
// in all browers
console.log(e.which);
});
我正在尝试编写一个带有事件侦听器的 jQuery 文件,以响应多个键盘事件,包括 Ctrl+Z、Ctrl+Y、Ctrl+X 等。目前,我已经开始研究撤消和重做功能,并且在 Firefox 49 中都可以正常工作,但在 Chrome 53 中它们似乎都不正确(但可以预测)。
Firefox 49 可以完美地正确处理撤消和重做,但 Chrome53 似乎监听与 Firefox 不同的 ASCII 键码。最重要的是,Chrome 只决定在键入 Ctrl+Z 两次后处理该 ASCII 代码,因为它似乎完全忽略了第一次尝试。
下面是我的代码片段:
$(document).on('keypress', ':input', function(event) {
if(event.ctrlKey && (event.which === 88 || event.which === 120)){ // cut handled in another event
// don't transmit Ctrl+x or Ctrl+X
return;
}
else if(event.ctrlKey && (event.which === 89 || event.which === 121)){
// todo handle Ctrl+y or Ctrl+Y (redo)
}
// Google Chrome
else if(event.which === 25){ // Chrome seems to want me to use ASCII code 25
// todo handle Ctrl+y or Ctrl+Y (redo)
}
// undo done similar to redo
}
我的两个问题如下:
为什么 Chrome 不想使用与 Firefox 相同的 ASCII 代码?两者都是发布时的最新版本,但两者似乎都想以不同的方式处理基本操作。
如何让 Chrome 在第一次使用快捷键时执行 undo/redo,而不是必须输入两次,这显然不是预期的。
提前致谢。
如评论中所述,使用 keyup 而不是 keypress,您将收到 'correct' 代码。
In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.
你可以检查这个small script
的区别$(document).on('keyup', ':input', function(event) {
// switching to keyup will return the same keycode
// in all browers
console.log(e.which);
});