Search code examples
javascriptjqueryhtmlkeydownkeyup

Alt key behaviour with Javascript on Chrome/Windows 7?


I'm trying to emulate a behaviour of a windows menu where you could navigate menus using an combination like: alt + KEY + KEY ... as a simple test I did this:

$(document).on('keydown', function(e) {
  $(".dropdown>a").css("text-decoration","underline");
}).on('keyup', function(e) {
        $(".dropdown>a").css("text-decoration","")
});

Which should add or remove an underline for all my links under .dropdown whenever I press any key on the website, it works fine for all , but the Alt key, with this key the system appears to be awaiting another key to be pressed and will only show the underline if I press alt twice.

I'm wondering if this is something that can be worked around or is something regarding the operating system. Why does is happen?


Solution

  • It can be related to how your browser handles the alt key, might be it activates its own menus. Your code works fine for all keys including alt on my ubuntu+chrome.

    You can also check if e.preventDefault() solves your problem.