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

  • SEARCH
  • Home
  • 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 6621103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:11:45+00:00 2026-05-25T21:11:45+00:00

It looks like mouse events will add listeners to canvas elements fine, but keyboard

  • 0

It looks like mouse events will add listeners to canvas elements fine, but keyboard events don’t seem to be working for canvas elements.

Example:
http://jsfiddle.net/H8Ese/1/

Browsers:
Chrome 14.0
FF 5.0.1

I know I can use the document level listeners, but I’m trying to get the Canvas element first (so that your keyboard works everywhere else on the page).

Any ideas on how to get key event listening working on canvas elements?

  • 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-05-25T21:11:46+00:00Added an answer on May 25, 2026 at 9:11 pm

    I don’t think you can add keyboard event listener directly to the canvas. If you don’t want to register event handler on window level then I think you can wrap the canvas inside a div and register keyboard events on the div.

    <div id="canvasWrapper" style="border:1px solid;   width:600px; height:400px;">
            <canvas id="canvas" width="600" height="400" >
                Could not create Canvas!
            </canvas>
    </div>
    
    jQuery("#canvasWrapper").keypress(function(e){
    keys[e.keyCode] = true;
    alert("key pressed!");
    });
    

    Another interesting way is to use tabIndex on the canvas tag and bind keypress on the canvas. I have updated the code at jsfiddle, pasting here too for future references.

    <canvas id="my-canvas" tabindex="1"></canvas>
    
    
    $("#my-canvas").bind({
    keydown: function(e) {
        var key = e.keyCode;
       var elem=document.getElementById("my-canvas");
    
        var context=elem.getContext("2d");
        context.font = "bold 20px sans-serif";
        context.clearRect(0,0,300,200);
        context.fillText("key pressed " + key, 10,29);
    
    },
    
    focusin: function(e) {
        $(e.currentTarget).addClass("selected");
    },
    
    focusout: function(e) {
        $(e.currentTarget).removeClass("selected");
    }
    });
    $("#my-canvas").focus();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It looks like a label normally, once you move the mouse over it, or
Looks like the options are Propel, Doctrine, and ActiveRecord, others?, but I am only
I need to implement mouse drag events which look something like this: class MouseDragEvent
This looks like it should work to me, but clearly I've done something wrong.
Looks like here in StackOveflow there is a group of F# enthusiasts. I'd like
Looks like my only option is to create a mutatant child of the tree
Looks like while( condition ) { //do stuff } is completely equivalent to for(
Looks like ExpressionTrees compiler should be near with the C# spec in many behaviors,
Looks like there is no Load event for usercontrol on the CF. I'm used
Looks like XSD.exe is not delivered as a part of Visual Studio 2010. what

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.