Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to receive keyboard events in a canvas element. Click events are received but key presses aren't.

Here is my code which doesn't handle key events:

var canvasElm = $('canvas');
Event.observe('canvas', 'keypress', function(e) {
    console.log('Key event is received!!');

How can I make it handle those key events?

share|improve this question
Dupe, or at least helpful? – Anko Feb 28 '13 at 23:48
up vote 2 down vote accepted

I suspect the canvas never has the keyboard focus. Keyboard events will be sent to the Window object.

Try adding the events to the Window object. (NB: It might be clearer what's happening if you don't use JQuery)

share|improve this answer
+1 For "don't use jQuery" ;) And for finding the solution, of course! – Laurent Couvidou Mar 1 '13 at 10:07

I'm really sorry. I found the solution after ask the question by trying. As MarkR said, we need give focus on our canvas. Here is the pure javascript (without JQuery);

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.