如何区分“右键单击鼠标”和“上下文菜单键按下物理键盘”
如何区分使用鼠标右键单击和物理键盘上的上下文菜单键?
How do I differentiate between right click using mouse and context menu key press on physical keyboard?
使用此代码我尝试在控制台中打印事件
Using this code I tried printing event in console
$('#'+inputId).bind('contextmenu', function(e) {
console.log(e);
});
我抓住了上面代码的一些输出 -
I grabbed some output for above code-
右键单击鼠标是 -
- 按钮:2
- originalEvent:MouseEvent
- 类型:contextmenu
- 其中:3
- button: 2
- originalEvent: MouseEvent
- type: "contextmenu"
- which: 3
对于键盘上下文键按键,它是 -
- 按钮:2
- originalEvent:MouseEvent
- 类型:contextmenu
- 其中:3
- button: 2
- originalEvent: MouseEvent
- type: "contextmenu"
- which: 3
我想只在物理键盘上按下contextmenu键时才执行某些操作。我如何实现这一目标?
I want to perform some action only when 'contextmenu key' is pressed on physical keyboard. How do I achieve that?
提前致谢!
Hiya那里这会帮到你捕捉差异:工作演示 http://jsfiddle.net/pPnME/1/
Hiya there This will help you to capture the difference: Working Demo http://jsfiddle.net/pPnME/1/
我在Alienware上测试了这个 - Chrome,当你点击右键时,你会看到键盘上的其他明智的警告,你会看到keyborad警报。
I have tested this on Alienware - Chrome, when you will right click you will see the right click alert other wise on keyboard you will see keyborad alert.
请注意:您可以根据确定点击次数
属性: http://api.jquery.com/event.which/
Please note: you can identify the click based on which
property: http://api.jquery.com/event.which/
对于键或鼠标事件,此属性指示已按下的特定键或
按钮。
For key or mouse events, this property indicates the specific key or button that was pressed.
:)
另请注意,有几个插件可用于获取快捷方式,但我建议坚持使用基本和使用我给出的演示,如果它只是单独捕获两个事件休息演示就是你所有的玩法:)
Also note there are few plugins available to get the shortcut but I would recommend stick to the basic and use the demo I have given if its only to capture both event separately rest demo is all your to play :)
代码
/*
1 = Left Mousebutton
2 = Centre Mousebutton
3 = Right Mousebutton
*/
$('input').mousedown(function(e) {
if (e.which === 3) {
alert('rightclick'); /* Right Mousebutton was clicked! */
}
});
$('input').bind('contextmenu', function(e) {
alert('keyboard yeah');
//console.log(e);
});