我正在开发一个应用程序,当用户按下特定文本框上的CMD+时,我需要做一些后处理。我要做这个后浏览器的默认功能(即在将插入符号置于当前物理行的第一个位置之后)。
问题是keyup
是不只要cmd键被按下,左键(或该事项的任何键)就会被触发。
我用Shift键尝试了这一点,并发现对辅助键来说,keyup是按预期触发的。因此,如果执行Shift+左,然后释放Shift,将得到4个事件,2个按键和2个按键。然而,对于CMD键,我们得到了2个按键,但是只有一个键事件(当我们在最后释放它时,CMD键本身的一个键事件)。
实质上:
$('#mytextbox')
// this gets correctly triggered for the meta key as well as the secondary key
// when you press CMD and LEFT in sequence, you get two lines in the console one for
// the CMD key and one for the LEFT key
.keydown(function(_e) {
console.log('Keydown: ' + _e.keyCode);
})
// however, if I release the LEFT key (while keeping the CMD key down)
// this does NOT get triggered for the LEFT key
.keyup(function(_e) {
console.log('Keyup: ' + _e.keyCode);
});
发布于 2018-05-25 11:36:22
可以考虑自己实现默认的浏览器行为,然后实现所需的自定义行为,然后用_e.preventDefault();
发布于 2018-05-25 11:48:10
当我做cmdspace时,我甚至没有得到空间的按键事件,因为聚光灯窗口出现了。
在提到ctrl键时:因为我在ctrllist或ctrlright时设置了“space”,所以不会触发左或右按键事件,但是ctrlup或ctrldown至少会触发它们的按键事件。
https://stackoverflow.com/questions/-100008582
复制相似问题