1

Web browsers fail to capture windows key shortcut. For instance, Windows key + D displays the desktop.

However, in the browser, JS could only detect the keydown event of the Windows key, but is not able to capture the keyboard event of D or the keyup event of the windows key.

What's the deep reason behind it? Is there any document explaining it?

4

2 回答 2

0

这不是一个理想的答案,但您可能需要编辑 Windows 注册表才能做到这一点。

  1. 在注册表中禁用 Windows+D。看到这个答案
  2. 使用下面的代码捕获密钥。来源

var log = $('#log')[0],
    pressedKeys = [];

var prevKey;

$(document.body).keydown(function (evt) {
    var li = pressedKeys[evt.keyCode];
    if (!li) {
        li = log.appendChild(document.createElement('li'));
        pressedKeys[evt.keyCode] = li;
    }
    $(li).text('Down: ' + evt.keyCode);
    $(li).removeClass('key-up');
    
    if(prevKey){
        if(prevKey === 91 && evt.keyCode === 68){
            evt.stopPropagation();
        }
    }
    
});

$(document.body).keyup(function (evt) {
    var li = pressedKeys[evt.keyCode];
    if (!li) {
       li = log.appendChild(document.createElement('li'));
    }
    $(li).text('Up: ' + evt.keyCode);
    $(li).addClass('key-up');
});
.key-up {
    opacity: 0.5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="log">
    <li>List of keys:</li>
</ul>

于 2015-02-09T07:33:37.083 回答
0

Windows+D 被操作系统捕获,不会转发到浏览器。一种可能的解决方法是使用 Ctrl+Windows+D 来模拟 Windows+D。

于 2015-07-28T05:44:44.923 回答