Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:11:08+00:00 2026-06-06T19:11:08+00:00

Here’s a very simple thing I’d like to do: when the pointer is within

  • 0

Here’s a very simple thing I’d like to do: when the pointer is within a canvas element and I press, say, the key ‘A’ I want to draw a circle centered at the given location. Alas, I can’t seem to be able to achieve this. I know how to add a listener to capture mouse click events:

var canvas = goog.dom.getElement('my_canvas');
goog.events.listen(canvas, [goog.events.EventType.CLICK], 
   function(e) {console.log(e);});

Easy es pie and works beautifully: the event has the coordinates of where I clicked so I can use it to draw a circle. But I wanted to use the keyboard for this (because I’m planning on having other actions as well). First, I tried the trivial:

goog.events.listen(canvas, [goog.events.EventType.KEYDOWN], 
   function(e) {console.log(e);});

Nothing. I tried, using KEYUP, KEYPRESS, or all of them together: not a beep. Then, I came across goog.events.KeyHandler and tried that. Here’s what I did:

var canvas = goog.dom.getElement('my_canvas');
var keyHandler = new goog.events.KeyHandler(canvas);
goog.events.listen(keyHandler, goog.events.KeyHandler.EventType.KEY, 
   function(e) {console.log(e);});

Still nothing. Now, I got angry, so I said “Let’s see if we can capture any keyboard events at all!” and did the following:

var keyHandler = new goog.events.KeyHandler(document);
goog.events.listen(keyHandler, goog.events.KeyHandler.EventType.KEY, 
   function(e) {console.log(e);});

Lo and behold, it worked: now I was capturing all keyboard events. Unfortunately, when I looked at the events in the javascript console, the keyboard events didn’t have any coordinates or even target elements in them. What? I mean, that’s not very useful, right?

Anyhow, I’m stuck. It seemed like a trivial thing to do but I can’t seem to be able to do it.

I’m using Chrome Version 19.0.1084.53 on Mac OS X Version 10.7.4.

I’m not sure about the version of Closure I’m using (how do you even find it out?) because I’m using Plovr which comes bundled with its Closure version, but I believe it’s pretty recent (Feb 2012).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:11:10+00:00Added an answer on June 6, 2026 at 7:11 pm

    OK, as pointed out by cpeisert in the comment, the problem was that canvas, by default, cannot get the input focus. I had to add ‘tabindex=”1″‘ to make it happen. Next, I had to surround canvas with a div and attach my event handler to the div.

    Related Question (although not using Closure)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a coding problem for those that like this kind of thing. Let's see
Here's a very simple repro: Start up VS2010 or VS2008, new a WPF project
Here is a simple timepicker to jQuery UI's datepicker <script type=text/javascript> /* <![CDATA[ */
Here's a piece of code I copied from http://www.schillmania.com/content/projects/javascript-animation-1/demo/ Very simple JS animation: function
Here's what I'm doing. I want to upload multipart file via Ajax to my
Here's what my table TheTable looks like ColA | ColB | ColC ------+-------+------ abc
Here is my code...I have two dimensional matrices A,B. I want to develop the
here i want to understand the architecture of bluez (Bluetooth Stack Protocol). I understood
Here an example of my checkbox list http://jsfiddle.net/YnM2f/ Let's say I check on G
Here is my code (Say we have a single button on the page that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.