Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    H3X
    H3X is offline
    New Coder
    Join Date
    Nov 2008
    Posts
    90
    Thanks
    30
    Thanked 0 Times in 0 Posts

    Detecting double-press of a key

    I want to detect the double-press of the space key. However, I want a single press of a space key to have a different function. The double-tap of space should only count if the taps of space are no more than, say, a half second apart.

    For example:
    The user hits space once, and javascript begins performing one action. 1/3rd of a second later, they hit the space bar again, and since that is within our threshold of 1/2 of a second, the action that began because the user hit the spacebar once should be cancelled, or undone (which I can write later) and the action when the user hits the spacebar twice should begin.

    Anyone have an idea how to do this? Thanks!

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    You need to use onkeydown. If I'm understanding you correctly you want javascript to do something if the spacebar is pressed and then stop the action if the spacebar is pressed again within 1/3 of a second?

  • #3
    H3X
    H3X is offline
    New Coder
    Join Date
    Nov 2008
    Posts
    90
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by elitis View Post
    You need to use onkeydown. If I'm understanding you correctly you want javascript to do something if the spacebar is pressed and then stop the action if the spacebar is pressed again within 1/3 of a second?
    I know I need to use that, but I'm just not sure how I need to link everything together.

    And, I want javascript to do something if the spacebar is pressed and then stop the action and start another one if the spacebar is pressed again within 1/2 of a second.

  • #4
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by H3X View Post
    I know I need to use that, but I'm just not sure how I need to link everything together.

    And, I want javascript to do something if the spacebar is pressed and then stop the action and start another one if the spacebar is pressed again within 1/2 of a second.
    Lets try getting the first part of that to work before moving on to the second part.
    Code:
    <head>
    <script type="text/javascript">
    function checkspace(event){if (window.event.keyCode == 32) alert('Stop that!');}
    </script>
    </head>
    <body>
    <BODY onKeydown="checkspace()">
    Replace alert with whatever it is you want it to do.

  • #5
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then you could do a counter function that increments by 100 every 100ms, and do an if
    var keypresses = 0;
    var counter = 0;
    startcouting();
    if(event.keyCode == 32){
    keypresses += 1;
    }
    if(keypresses == 2 && counter <= 500){
    do something;
    reset the counter;
    }

    That is coming from a not so good at JS guy, pardon me if there is a lot of failing.

  • #6
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    HTML5 canvas and javascript game code optimization help.

    Edited
    Last edited by amando96; 12-31-2010 at 07:40 PM.

  • #7
    H3X
    H3X is offline
    New Coder
    Join Date
    Nov 2008
    Posts
    90
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Thanks guys, got it working on my own after tinkering for a while.

  • #8
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Arrow

    Quote Originally Posted by H3X View Post
    Thanks guys, got it working on my own after tinkering for a while.
    Now that you have peaked our interest,
    how about showing what you did to solve the problem
    in case others might want to use the technique!


  •  

    Posting Permissions

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