如何区分“右键单击鼠标”和“上下文菜单键按下物理键盘” [英] How to differentiate between 'right click using mouse' AND 'context menu key press on physical keyboard'

查看:163
本文介绍了如何区分“右键单击鼠标”和“上下文菜单键按下物理键盘”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何区分使用鼠标右键单击和物理键盘上的上下文菜单键?

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-

右键单击鼠标是 -


  1. 按钮:2

  2. originalEvent:MouseEvent

  3. 类型:contextmenu

  4. 其中:3

  1. button: 2
  2. originalEvent: MouseEvent
  3. type: "contextmenu"
  4. which: 3

对于键盘上下文键按键,它是 -


  1. 按钮:2

  2. originalEvent:MouseEvent

  3. 类型:contextmenu

  4. 其中:3

  1. button: 2
  2. originalEvent: MouseEvent
  3. type: "contextmenu"
  4. 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);
});​

这篇关于如何区分“右键单击鼠标”和“上下文菜单键按下物理键盘”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆