www.webdeveloper.com
Results 1 to 2 of 2

Thread: How do I detect keypresses for Canvas?

  1. #1
    Join Date
    Apr 2012
    Posts
    1

    How do I detect keypresses for Canvas?

    I'm trying not to use third party scripts such as jQuery, because I am learning javascript itself.
    Basically, I want to detect when a key has been pressed so that I can move a circle around. I successfully achieved this by adding into the body element:
    <body onkeydown='keyboard.keydown(event)'></body>
    Problem is, a keypress events can happen from all elements. This isn't really the way I want to do it because I would like to have input forms on the same document.

    Well, I've tried onkey events inside of the canvas element but nothing happens! The only thing that seems to get through is onclick and I don't know why. I've googled and the only thing that I find is jQuery stuff. Any ideas from the Javascript guru's?

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    It is a sort of misunderstanding of the javascript events. The keys' events (onkeydown, onkeyup, etc.) can fire a javascript function only if the element gains a focus. See also this article:

    http://www.dbp-consulting.com/tutori...KeyEvents.html

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles