OscarGodson / jKey

Key shortcuts made simple
http://oscargodson.github.com/jKey/
185 stars 40 forks source link

alert()s and confirms() break jKey if they are invoked with it #13

Open ossiangrr opened 12 years ago

ossiangrr commented 12 years ago

In the simplified example below, if I press a or b separately, nothing happens (expected). If I press a and b and the same time, then the event fires (expected). But after that point, the event fires every time I press a or b.

Behavior is reproducible in Chrome 13 and Firefox 6 and IE9 Am I doing something wrong?

Thanks!

<!doctype html>  
<html>
<head>
   <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
   <script type="text/javascript" src="jquery.jkey-1.2.js"></script>

  <script type="text/javascript">
  $(function() {
  $(document).jkey('a+b',function() { alert('a and b pressed together!');});
  });
  </script>
</head>
<body>
</body>

ossiangrr commented 12 years ago

Addendum: It only seems to be an issue if alert/confirm (maybe some other things?) are in the handler function. If there is no alert(), and I do something else instead, the event only fires when expected. Which means I can work around it, but I'd still consider it a bug.

OscarGodson commented 12 years ago

Yeah, that's a known issue and thats why in my examples I have jkey.console which is an internal method in the docs. I was already suspecting this before I got this 2nd message and did a quick test case:

http://jsbin.com/acuqop/edit#javascript,html

As you know it's just the alert tho.

The bug is tough, and im not sure how to fix it yet. The reason is that alert take the focus off your current window and jKey then sort of gets lost. I'm storing all the current keys you're pressing in an array and when you fire an alert it never has a chance to wipe out that array because the alert cancelled all my scripts so in jKey's mind you never yet let go of your keys.

If you have any suggestions or come up with a bug fix let me know.

ossiangrr commented 12 years ago

I'm no expert, but I suppose you could at least add a manual "eject" function to force jkey to drop any currently lingering keys, and tell users to call jkey.eject('keycode list') after launching any alerts?