Web browsers are notorious in how they handle key presses. Every browser handles them differently. I am not going to go into the details here about the differences between the browsers, as it is covered in lots of other places on the web. There is a particularly good comparison here by The Pothoven Post which shows how the browsers differ in their interpretation of key presses. Rather I am going to deal with how it is handled in Adobe Air, and Webkit which air is built on. mybloggingplanet.com/2014/09/how-to-make-blog-in-free.html

The three events linked to capturing keys are the keydown, keyup and keypress events. And it is not all that difficult to listen to events using Javascript. The only code you need to listen to the keydown even is this:

keyDownEvent = function(e) {
   alert(e.keyCode);
}

document.getElementById('obj_name').addEventListener('keyup', keyUpEvent, false);

In Webkit, this event gets fired when you press a key, and e.keyCode contains the code for the key that was pressed.

What I was trying to do was to capture the ctrl-x, ctrl-c, ctrl-v shortcut keys.

When you press these key combinations, the keydown event should get fired. Rather more specifically, a keydown event will get fired for the ctrl key as well as for the c, x or v key, so you will get, with the code above, a popup giving a keycode of 17 when you press the ctrl key, and then, while holding down ctrl, if you press c, it will pop up with a key code of 67, which is the same code as if you had only been pressing c by itself.

So, what might my problem be, you may ask?

Well, everything works well, except for the fact that when you press ctrl-v, no event gets triggered, thus making it impossible to check for that key combination. You can detect ctrl-x and ctrl-c but not ctrl-v. I even tried many other keys, and all others I have tried have worked, and so it is solely the ctrl-v key combo which refuses to work.

Safari 4 is also built on top of Webkit, and thus should give similar results to Adobe Air, so I ran my code in there to test it. The code all worked fine in Safari 4, and the ctrl-v key combo was being correctly trapped.

This seems to indicate to me that there is a slight problem in Adobe Air itself as to why it won’t see this key, and I have yet to find a way in which to overcome this problem.

Share