简体   繁体   English

如何在不捕获keydown事件的情况下查看修改键是否被按下?

[英]How to see if a modifier key is being held down without catching the keydown event?

I know how to catch keyup and keydown events. 我知道如何捕获keyup和keydown事件。

I want my program to see which modifier keys (eg: meta key, control key, alt key, etc.) are currently being held down even if the program didn't observe the keydown events of those keys. 我希望我的程序看到哪些修饰键(例如:元键,控制键,alt键等)当前被按下,即使程序没有观察到这些键的keydown事件。

I know that click events and other events can tell me which modifier keys were pressed down when they were fired off, but my program can't wait around for one to occur. 我知道点击事件和其他事件可以告诉我哪些修饰键在被触发时被按下,但我的程序不能等待一个发生。

I need my program to check for the modifier keys regularly (say, every 100ms). 我需要我的程序定期检查修改键(比方说,每100毫秒)。 How would I go about doing this? 我该怎么做呢? My program uses jQuery if that helps. 如果有帮助,我的程序使用jQuery。

You can store those values in a variable, and check them whenever you want. 您可以将这些值存储在变量中,并随时检查它们。 An event listener will be necessary of course to update that variable: 当然,事件监听器必须更新该变量:

 // assume no hot key is pressed var object = { ctrlKey: false, altKey: false, shiftKey: false }; // update whenever a keydown or keyup event is fired document.addEventListener("keydown", function(e) { for(var key in object) { if(object.hasOwnProperty(key)) object[key] = e[key]; // update the object from the event e } }); document.addEventListener("keyup", function(e) { for(var key in object) { if(object.hasOwnProperty(key)) object[key] = e[key]; } }); // testing example: function check() { console.log("Checking:"); console.log("Alt key:", object.altKey); console.log("Ctrl key:", object.ctrlKey); console.log("Shift key:", object.shiftKey); } setInterval(check, 1000); // calling check every second without waiting for an event to occur 

Late to the party, but since this shows up on Google, here's a partial solution that's one step closer. 晚会,但由于这出现在谷歌,这是一个更接近的部分解决方案。 From this SO answer : 这个SO回答

document.body.onkeydown = function(e) {
  if (e.which === 18) {
    alt_state.textContent = 'pressed';
  }
};

document.body.onkeyup = function(e) {
  if (e.which === 18) {
    alt_state.textContent = 'released';
  }
};

function detectAlt() {
  if (document.webkitHidden) return;
  window.addEventListener('mousemove', function onMove(e) {
    alt_state.textContent = e.altKey ? 'pressed' : 'released';
    window.removeEventListener('mousemove', onMove, false);
  }, false);
}

document.addEventListener('webkitvisibilitychange', detectAlt, false);
window.addEventListener('load', detectAlt, false);

Essentially, set up a mousemove event listener, which contains the modifier key states when fired, and remove the listener after it's fired the first time. 本质上,设置一个mousemove事件侦听器,它包含触发时的修饰键状态,并在第一次触发后删除侦听器。 Again, still requires an "event" but depending on your application's expected usage, catching the first mousemove event might be sufficient for detecting modifier keys. 同样,仍需要“事件”,但根据应用程序的预期用途,捕获第一个mousemove事件可能足以检测修饰键。

暂无
暂无

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

相关问题 如何检查一个键(在键盘上)是否被按住? - How to check if a key (on the keyboard) is being held down? 防止 JavaScript keydown 事件在按住时被多次处理 - Prevent JavaScript keydown event from being handled multiple times while held down Javascript:限制按下按键事件监听器在按下时可以多快调用一次函数 - Javascript: Limit how quickly a keydown event listener can call a function when held down 按住多个键时未触发JavaScript KeyDown事件 - JavaScript KeyDown event not triggered when multiple keys are held down HTML Canvas:按住键时控制键按下注册表 - HTML Canvas: Controlling keydown registry when key is held down jQuery使用keydown / keyup / keypress事件仅触发一次,而不是按住键时触发多次 - jQuery using keydown/keyup/keypress event to fire only one time instead of multiple times when key held down 使用 JavaScript 每次按下键时,即使它被按住,我如何才能只获取一次 keydown 事件? - How can I get keydown events only once each time the key is pressed, even if it's held down, with JavaScript? 跨浏览器的方式,在按住键时自动重复重复按下事件 - Cross-browser way to get automatically repeating keydown events when key is held down 在没有keydown事件的情况下检测CTRL和SHIFT键? - Detect CTRL and SHIFT key without keydown event? 单击事件多次触发并按住 Enter 键 - Click event fires multiple times with enter key being held
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM