jQuery 触发 keyCode Ctrl+Shift+z +z +在所见即所得文本区域中按 Ctrl+z
我想知道如何触发由 Ctrl+z
组成的事件 keyCode 和由 Ctrl+Shift+z
组成的事件 keyCode ?
i was wondering how do i trigger the event keyCode composed by Ctrl+z
and the event keycode composed by Ctrl+Shift+z
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用已通过 jquery 跨浏览器标准化的
e.which
。Use
e.which
which has been normalized cross browser by jquery.如果你想触发该事件,那么它应该是这样的:
HTML
JavaScript
查看 JSBIN 链接
但似乎不起作用。我没有更多的时间花在这上面,但是,这是一个安全问题。我会在 MSDN 上看到这些文档, W3C和 MDN 看看是否有真正的方法可以做到这一点。
If you want to trigger the event then it should be something like this:
HTML
JavaScript
LOOK AT JSBIN LINK
But it seems it doesn't works. I don't have more time to spend on this, but yeah this is kind of a security issue. I would see these docs at MSDN, W3C and MDN to see if there is a real way to do this.
Ctrl 和 Shift 键包含在按键事件中,但按键代码会引用您按下的键。 Ctrl 和 Shift 是控制键,它们在按键事件中拥有自己的键。
例如,如果您按
Ctrl+Shift+Z
,那么 keydown 事件将是这样的:如您所见,有两个键分别用于
Ctrl
和Shift
键为 true,因为这些键是在按下Z
时按下的。因此,您可以像这样检测此事件:
注意:您应该监听
keydown
以获得多个键盘快捷键。keyup
不起作用。Ctrl and Shift keys are included in key events but keycode is refereeing to which key you press. Ctrl and Shift are control keys and they have their own keys in key events.
For example if you press
Ctrl+Shift+Z
then keydown event would be this:As you can see there is two key for
Ctrl
andShift
keys that are true because those keys were pressed while pressingZ
.So you can detect this event like this:
Note: You should listen to
keydown
for multiple key keyboard shortcuts.keyup
wouldn't work.上面的答案很好并且有效,但是对于更动态的解决方案会有所帮助的情况,我有一个使用的替代解决方案。认识到我有点晚了,这是我的解决方案:
一个用于查看效果的 HTML 元素:
以及脚本:
这是一个高度缩写的版本,当我知道我是唯一一个会使用它的人时,我会使用它需要通读剧本。如果您认为其他人需要阅读和理解它,我建议使用稍微不同的脚本版本:
以下是这种方法的优点:
1)处理您的代码的其他人不需要知道任何你写的 JavaScript 可以工作。您可以让他们在需要时添加和删除命令。
2)可以动态添加命令(可能取决于用户输入)。
3 ) 非常复杂的按键命令、重载命令和其他更复杂的操作对于其他解决方案可能会更困难。
下面测试一下:
The answers above are good and work, but for situations where a more dynamic solution would be helpful I have an alternate solution that I use. Recognizing that I'm a little late to the game, this is my solution:
An HTML element to view the effects in:
And the script:
This is a highly abbreviated version that I use when I know that I am the only one who will need to read through the script. If you think someone else will need to read and understand it, I would suggest using a slightly different version of the script:
Here are the advantages of this approach:
1 ) Someone else working on your code does not need to know how any of the JavaScript you wrote works. You can just have them add and remove commands whenever they need to.
2 ) Commands can be added dynamically (possibly depending on user input).
3 ) Very complex key commands, overloaded commands and other more complex operations might be more difficult with other solutions.
Test it out below: