views:

222

answers:

0

I've hit a wall trying to solve this problem and have found no resources that address it specifically. Some are close, but nothing complete.

The default behavior of the DOWN arrow-key on the web page is to scroll the page down.

I'm looking for a way to detect a quick successive double-tap of the DOWN arrow-key, That accounts for:

  1. Will not confuse with periodic taps of the Down key
  2. Will not be confused with a continuous key press
  3. Not jquery - Sorry, but I'm dependent on Prototype for this

Super Extra Bonus Points if... - The default scroll-down behavior of the browser window is prevented until it knows for sure whether its a single tap or double-tap of the DOWN key. - Incorporates detection of single & double keypress with the SHIFT key

Here is some rough code I originally worked with - which you are not obliged to work with - its just an idea of where I was going and to show I'm trying to tackle this myself... it presumes Prototype.js and workes in Firefox (for the most-part) but failed y confusing continuous keypress with double in Safari, Chrome etc...

Example = {
    run: function() {
        this.lastKey   = 0;      // Last key pressed
        this.lastPress = 0;

        this.dblDelay  = 250;    // Delay in ms
        this.dblTap    = false;

        Event.observe(window, 'keydown', this.shortcuts.bind(this));
    },

    shortcuts: function(e) {
        var instance   = this;
        var key        = e.keyCode;
        var now        = (new Date()).getTime();
        this.dblTap    = (this.lastKey == key && this.lastPress > (now - this.dblDelay)) ? true : false;

        function delayedExecute(fn) {
            if (!instance.dblTap) {
                fn();
            }
        }

        if (key == 40) {
        //  Down Arrow
            if (this.dblTap) {
            //  Double Down
                this.stopDefault(e);
                this.doWhateverForDouble();
            } else {
            //  Single Down
                window.setTimeout(function(){
                    delayedExecute(function(){
                        console.log('Single Down. Default behavior not blocked');
                    });
                }, 350);
            }
        }

        this.lastKey   = (this.dblTap) ? 0 : key;
        this.lastPress = now;
    },

    stopDefault: function(e) {
        if (e) {
            if (typeof e.preventDefault!= 'undefined') {
                e.preventDefault();    // W3C
            } else {
                e.returnValue = false; // IE
            }
        }
        return false;
    },

    doWhateverForDouble: function() {
        console.log('Double Tap detected and acted on');
    }
};

Example.run();