Web browser is not able to capture Windows key + D, why?

1.5k Views Asked by At

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?

2

There are 2 best solutions below

0
On

Windows+D is captured by the OS and is not forwarded to the browser. A possible workaround is to use Ctrl+Windows+D to simulate Windows+D.

0
On

Not an ideal answer but you might need to edit the Windows Registry for this to happen.

  1. Disable Windows+D in the registry. See this answer.
  2. Capture the keys using the code below. Source.

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>