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 4023664
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:37:34+00:00 2026-05-20T10:37:34+00:00

I’m writing a Javascript class to handle keystrokes as well as keystroke combinations. For

  • 0

I’m writing a Javascript class to handle keystrokes as well as keystroke combinations. For example, the following would add a callback for the SHIFT key.

MYAPP.Keyboard.instance().observe(
    MYAPP.Keyboard.type.KEYDOWN,
    MYAPP.Keyboard.key.SHIFT,
    function() {
        $$('body').first().addClassName('keyboardHintShow');
    }
);

An example for SHIFT+F would be:

MYAPP.Keyboard.instance().observe(
    MYAPP.Keyboard.type.KEYUP,
    [MYAPP.Keyboard.key.F, MYAPP.Keyboard.key.SHIFT],
    function() {
        MYAPP.Broadcast.instance().signal('file');
    }
);  

This is working great in Firefox so far, but not when I attempt to use ALT as a modifier. Two problems arise. 1) Event propogation does not stop, so Firefox’s File menu pops up. 2) After releasing ALT and F, only F‘s onKeyUp fires, so the state of ALT is wrong. What makes ALT so different from SHIFT?

Here’s the complete code:

MYAPP.Keyboard = Class.create({

/*******************************************************************************
PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUB
*******************************************************************************/

    /**
     * @return pointer
     */
    initialize: function() {
        this.downKeys = new Hash();
        this.observers = new Hash();

        document.observe(
            'keydown',
            this.onKeyDown.bind(this)
        );

        document.observe(
            'keyup',
            this.onKeyUp.bind(this)
        );
    },

    /**
     * @param MYAPP.Keyboard.type type
     * @param MYAPP.Keyboard.key | array of MYAPP.Keyboard.key keys
     * @param void function() callback
     * @return void
     */
    observe: function(type, keys, callback) {
        var main;
        var modifiers;

        if (typeof(keys) === 'number') {
            main = keys;
            modifiers = [];
        } else {
            main = keys.first();
            modifiers = keys.slice(1);
        }

        if (this.observers.get(type) === undefined) {
            this.observers.set(type, new Hash());
        }

        this.observers.get(type).set(
            main, {
                modifiers: modifiers,
                callback: callback
            }
        );
    },

    /**
     * @param MYAPP.Keyboard.type type
     * @param MYAPP.Keyboard.key
     * @return void
     */
    stopObserving: function(type, key) {
        this.observers.get(type).unset(key);
    },

/*******************************************************************************
PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE 
*******************************************************************************/

    /**
     * @param Event event
     * @return void
     */
    onKeyDown: function(event) {
        if (this.downKeys.get(event.keyCode) === true) {
            return;
        }

        this.downKeys.set(event.keyCode, true);

        var downObservers = this.observers.get(MYAPP.Keyboard.type.KEYDOWN);
        if (downObservers !== undefined) {
            this.runCallback(downObservers, event);
        }
        return false;
    },

    /**
     * @param Event event
     * @return void
     */
    onKeyUp: function(event) {
        this.downKeys.set(event.keyCode, false);

        var downObservers = this.observers.get(MYAPP.Keyboard.type.KEYUP);
        if (downObservers !== undefined) {
            this.runCallback(downObservers, event);
        }
        return false;
    },

    /**
     * @param Hash observers
     * @param Event event
     * @return void
     */
    runCallback: function(observers, event) {
        var overrideBrowser = false;
        var order = observers.get(event.keyCode);
        if (order !== undefined) {
            if (order.modifiers.size() === 0) {
                order.callback();
                Event.stop(event);
            } else {
                for (var i = 0; i < order.modifiers.size(); i++) {
                    var modifierStatus = this.downKeys.get(order.modifiers[i]);
                    if (modifierStatus === undefined || modifierStatus === false) {
                        return;
                    }
                    order.callback();
                    Event.stop(event);
                }
            }
        }
    }
});

/*******************************************************************************
STATIC STATIC STATIC STATIC STATIC STATIC STATIC STATIC STATIC STATIC STATIC STA
*******************************************************************************/

/**
 * @return Keyboard instance
 */
MYAPP.Keyboard.instance = function() {
    if (typeof(MYAPP.keyboard) === 'undefined') {
        MYAPP.keyboard = new MYAPP.Keyboard();
    }
    return MYAPP.keyboard;
};

/**
 * Event type
 */
MYAPP.Keyboard.type = {
    KEYUP: 0,
    KEYDOWN: 1 
};

/**
 * Keycodes for various keys.
 */
MYAPP.Keyboard.key = {
    SHIFT: 16,
    CTRL: 17,
    ALT: 18,
    A: 65,
    B: 66,
    C: 67,
    D: 68,
    E: 69,
    F: 70,
    G: 71,
    H: 72,
    I: 73,
    J: 74,
    K: 75,
    L: 76,
    M: 77,
    N: 78,
    O: 79,
    P: 80,
    Q: 81,
    R: 82,
    S: 83,
    T: 84,
    U: 85,
    V: 86,
    W: 87,
    X: 88,
    Y: 89,
    Z: 90
};
  • 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-20T10:37:35+00:00Added an answer on May 20, 2026 at 10:37 am

    This may help as an example. At the bottom of the page the author claims that his script overrides all modifiers – Ctrl, Alt, Shift, Meta. Hope this help, somehow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.