检查“单击”上的 Ctrl / Shift / Alt 键事件
我如何识别在以下代码中按下了Ctrl / Shift / Alt键?
$("#my_id").click(function() {
if (<left control key is pressed>) { alert("Left Ctrl"); }
if (<right shift and left alt keys are pressed>) { alert("Right Shift + Left Alt"); }
});
How could I identify which Ctrl / Shift / Alt keys are pressed in the following code ?
$("#my_id").click(function() {
if (<left control key is pressed>) { alert("Left Ctrl"); }
if (<right shift and left alt keys are pressed>) { alert("Right Shift + Left Alt"); }
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
嗯,这不适用于所有浏览器,只有 IE 8。微软实现了确定按下哪个(右/左)键的功能。这是一个链接 http://msdn.microsoft.com/ en-us/library/ms534630(VS.85).aspx
我还发现了这篇关于浏览器中的 keypress、keyup、keydown 事件的精彩文章。
http://unixpapa.com/js/key.html
Well you this wont work in all browsers just IE 8. Microsoft implemented the ability to determine which (right/left) key was pressed. Here is a link http://msdn.microsoft.com/en-us/library/ms534630(VS.85).aspx
I also found this wonder article about keypress, keyup, keydown event in browsers.
http://unixpapa.com/js/key.html
我不知道是否可以在单击事件中检查左/右键,但我认为这是不可能的。
I don't know if it's possible to check for left/right keys within a click event, but I don't think it's possible.
e.originalEvent.location
对于左键返回 1,对于右键返回 2。因此,您可以检测按下了哪个modifier
键,如下所示。希望这会对您有所帮助。e.originalEvent.location
returns 1 for left key and 2 for right key. Therefore you can detect whichmodifier
key is pressed like following. Hope this will help you.在大多数情况下,ALT、CTRL 和 SHIFT 键布尔值将用于查看是否按下了这些键。例如:
当被调用时,它将返回 true 或 false。有关更多信息,请访问 https://developer.mozilla.org /en-US/docs/Web/API/MouseEvent/altKey
为了将来参考,还有一个名为
metaKey
(仅限 NS/firefox)的工具,它在按下元键时起作用。In most instances the ALT, CTRL,and SHIFT key booleans will work to see if those keys were pressed. For example:
When called upon, it will return true or false. For more info, go to https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/altKey
For future reference, there is also one called
metaKey
(NS/firefox only) which works when the meta key is pressed.只是想我会添加一个适合 2020 年的答案。
您现在也可以使用
MouseEvent.getModifierState()
来实现此目的 - 它是 截至撰写本文时大多数浏览器都支持。注意事项:
shift
、ctrl
和alt
以外的修饰符。然而,由于固有的平台差异,不同操作系统之间的具体行为有些不稳定。在使用它们之前,请先检查此处。Just thought I would add an answer appropriate for 2020.
You can now also use
MouseEvent.getModifierState()
for this - it's supported by most browsers as of time of writing.Caveats:
shift
,ctrl
, andalt
. However the specific behaviour is somewhat erratic across different OSes due to innate platform differences. Check here before you use them.根据我的评论,这是可能的解决方案。
要检查按下了哪个特定修饰键,您可以使用 KeyboardEvent Location (查看表格支持)
为了支持 IE8,幸运的是你可以使用 已发布解决方案。
现在的解决方法是设置一个全局对象,其中包含与持有哪些修饰键相关的属性。当然,不使用全局对象的其他方法也是可能的。
在这里,我使用相关的 javascript 监听器方法捕获事件(jQuery 不支持捕获阶段)。我们捕获事件来处理
keydown/keyup
事件传播由于某种原因被正在使用的代码停止的情况。旁注:
)
Shift-Right 键),将导致仅处理第一个
(似乎是默认浏览器行为,所以无论如何,似乎是正确的!)
Following my comment, this is possible solution.
To check which specific modifier key is pressed, you can use KeyboardEvent Location (see table support)
To support IE8, fortunately you could use already posted solution.
Now the workaround is to set a global object with relevant properties regarding which modifier keys are held. Other ways without using global object would be possible of course.
Here, i capture event using relevant javascript listener method (jQuery doesn't support capturing phase). We capture event to handle case where
keydown/keyup
events propagation would be stopped for some reason by already in-use code.As side notes:
keys
Shift-Rigth keys), would result in only first one to be handled
(seems like default browser behaviour, so anyway, seems right!)
使用 js-hotkeys。它是一个 jQuery 插件。
这是一个测试,旨在显示您正在寻找什么。它还向您展示了如何捕获标准的左、右、上、下键和数字键盘(带有数字 2、4、6、8 的键)!
http://afro.systems.googlepages.com/test-static-08。 html
Use js-hotkeys. It is a jQuery plugin.
This is a test to show what you are looking for. It also shows you how to capture left, right, up, down keys standard and those from numeric key pad (the one with numbers 2,4,6,8)!
http://afro.systems.googlepages.com/test-static-08.html
比任何事情都简单:您使用 keydown 事件来检查它是否是 Ctrl (17) 或 Shift (16),然后使用 keyup 事件来检查它是否是 Enter< /kbd> (13) 和 Ctrl 或 Shift 之前按下(按下键时)
取消任何按键上的 Ctrl 或 Shift,但 Enter 除外
Easier than anything: you use keydown event to check if it's Ctrl (17) or Shift (16), you then use keyup event to check if it's Enter (13) and Ctrl or Shift hit before (on key down)
cancel Ctrl or Shift on any keyup but Enter
效果就像一个魅力! Chrome、Firefox、IE 和 Edge 上也支持 ;) https://jsfiddle.net/55g5utsk/2/< /a>
Works like a charm! and on Chrome, Firefox, IE, and Edge too ;) https://jsfiddle.net/55g5utsk/2/
有一些原因导致左右 CTRL、SHIFT 和ALT 键无法区分,因为
1.键码相同
2.许多笔记本电脑键盘可能没有两个控制键
参考:
如何判断事件是否到来从右 Ctrl 键?
There are some reasons that right and left CTRL,SHIFT & ALT keys are not distinguishable because
1. keyCodes are same
2. Many laptop keyboards may not have two control keys
Taken a Reference :
How can I tell if an event comes from right Ctrl key?