SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot stikkybubble's Avatar
    Join Date
    Sep 2006
    Location
    Pluto
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question stop auto-repeat onkeydown?

    hi,
    I am trying to write a js program which counts keyboard clicks.
    The problem I have is stopping auto-repeat, i.e. if the key is held down for a long time it counts this as lots of clicks instead of just one.
    So far I have only tested in Safari on OS x and IE on Windows.
    It is IE that is giving me the trouble.
    I am a bit of a noobie, and starting to wonder if what I want to do is possible- maybe I should try some other way?
    I added in what I could find that is supposed to stop default behaviour:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
    <html>

    <head>
    <title></title>
    <script type="text/javascript" src="soundmanager.js">
    </script>
    <script type="text/javascript">
    <!--
    /* timed beeps at 10sec intervals for 1 min */
    var foo = 0;
    function beep(){
    if (foo < 7) {
    foo++;
    if (foo > 1) {
    soundManager.play('on');
    }
    setTimeout("beep()",10000);
    }
    else {
    alert('session over');
    }
    }
    /*counting key presses: this is the problem area, where I have added everything I could find to stop the auto-repeat*/
    var totalKeyStrokes = 0;
    function CountKeyStrokes() {
    if (foo % 2 == 0) {
    totalKeyStrokes = ++totalKeyStrokes;
    if (evt.preventDefault) {
    evt.preventDefault();
    }
    else {
    evt.returnValue = false;
    return false;
    }
    }
    }
    document.onkeydown = CountKeyStrokes;
    //-->
    </script>
    </head>
    <body onload="beep()";>
    <div>
    </div>
    <script>
    soundManagerInit();
    </script>
    some text here
    <form name="go">
    <input type="button" value="Start" onClick="alert(totalKeyStrokes)">
    </form>
    </body>
    </html>

  2. #2
    SitePoint Zealot stikkybubble's Avatar
    Join Date
    Sep 2006
    Location
    Pluto
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok this turned out to be impossible i suppose: i am now using onkeyup, as solutions to the above do not work across all browsers.


Bookmarks

Posting Permissions

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