0

I'm trying to handle key events to a canvas. I've added a tabindex which allows the canvas to receive events. My problem is that keydown events aren't registering. They are still going to the document. I can get letter key events but not the standard arrow keys (or at least in everything but FF).

So I guess my question is how do I handle arrow key events in IE, Safari and Chrome? I get "sort of handled" in all browsers with letters and numbers. Just not arrow keys.

Thanks a bunch.

$('#tetris-canvas').keypress(function(e){
  e.preventDefault();
  alert('sort of handled');
  tetris.keyHandler(e);
});

My page is at http://tetris.townsendwebdd.com/ if you want to see the whole code

so changed it to $(document).keypress(yada); safari still doesn't handle arrows

Gambai
  • 651
  • 1
  • 7
  • 25

1 Answers1

1

Try this:

$(#tetris-canvas).keydown(function(e) {
    e.preventDefault();
    e.stopPropagation();
    // your code...
});​
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • this works. thank you why would it work with keydown and not keypress though – Gambai Dec 02 '12 at 18:21
  • This might help... http://stackoverflow.com/questions/1367700/whats-the-difference-between-keydown-and-keypress-in-net – palaѕн Dec 03 '12 at 07:49