dmauro / Keypress

A keyboard input capturing utility in which any key can be a modifier key.
http://dmauro.github.io/Keypress/
Apache License 2.0
3.18k stars 313 forks source link

How to prevent default handling #121

Open drubb opened 8 years ago

drubb commented 8 years ago

I'm not sure how to prevent the default handling for key pressures, at least it doesn't work for me. I've tried to figure this out from the docs, and ended up with the following code (example):

var scope = document.getElementById("page");
var options = {
    prevent_default  : true  
};
var listener = new window.keypress.Listener(scope, options);
listener.simple_combo("ctrl o", function() {
   alert("You pressed ctrl and o");
});

This will display the message, however the browser's default behaviour (open a file dialog) is triggered, too. Same for e.g. ctrl+f (opens find dialog).

Thanks a lot, Boris

drubb commented 8 years ago

Just found out that this doesn't work in conjunction with alert. If I replace this by console.log, everything is fine, the default event won't be triggered. The default code without parameters is ok, too:

var listener = new window.keypress.Listener();
listener.simple_combo("ctrl o", function() {
   console.log("You pressed ctrl and o");
});

So maybe this is a special case.

dmauro commented 8 years ago

So all combos will prevent the default behaviors by default (the prevent_default is for all constituent keys). It looks like the browser is doing some magic and doesn't like that the JS is throwing an alert maybe? What browser is this in?

drubb commented 8 years ago

Firefox 47.0.1 (Windows)