0

I can prevent Alt key in Javascript with these codes:

$(window).keydown(function(event) 
{
    if(event.altKey == true)
        {
            event.preventDefault();
        }
});

But if I want to prevent Win key from taking any default actions, can I?

Lan Mai
  • 365
  • 1
  • 5
  • 18

1 Answers1

3

No you can't block it since the Win Key is applied outside the scope of the browser, on a lower level. The same goes for combinations like Ctrl+Alt+Del and Ctrl+Shift+Esc.

However, if you just want to know if it was clicked and make it trigger an action you can do it like this:

$(window).keydown(function(event) {
  if (event.key == "Meta") {
    // event.preventDefault(); //Won't work
    console.log("Win Key was clicked");
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
unrealapex
  • 578
  • 9
  • 23
Z-Bone
  • 1,534
  • 1
  • 10
  • 14