简体   繁体   English

如何检测是否按下了左 Alt + 字母

[英]How can I detect if left Alt + letter was pressed

I need to perform some action only if the left ALT key was pressed with the letter s .仅当左ALT键与字母s一起按下时,我才需要执行某些操作。 I can find whether some Alt + s pressed using the keydown event, when oEvent.altKey === true and String.fromCharCode(oEvent.keyCode) === 'S' .oEvent.altKey === trueString.fromCharCode(oEvent.keyCode) === 'S'时,我可以找到是否使用keydown事件按下了某些Alt + s I can also find whether the left or right ALT was pressed by:我还可以找到是否按下了左侧或右侧ALT

oEvent.originalEvent.location === KeyboardEvent.DOM_KEY_LOCATION_LEFT 

or或者

oEvent.originalEvent.location === KeyboardEvent.DOM_KEY_LOCATION_RIGHT

But what I could not find is the way to combine the two.但我找不到的是将两者结合起来的方法。

For make this you have to register two events, keyUp and keyDown and using a single variable can do the trick,为此,您必须注册两个事件keyUpkeyDown并使用单个变量可以解决问题,

 isleftAltPressed : false,

keyUp: function(e)
{
  var keyCode = e.which ? e.which : e.keyCode;
  if(keyCode == 18)
      isleftAltPressed = false;
},

keyDown: function(e)
{
  var keyCode = e.which ? e.which : e.keyCode;

  if(keyCode == 18 && e.originalEvent.location === KeyboardEvent.DOM_KEY_LOCATION_LEFT)
      isleftAltPressed = true;

    if(e.altKey && isleftAltPressed && keyCode == 83)
      alert("hi");

},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM