0

I have this key commands script that I include every time I want user to be able to use their keyboard to do shortcuts. But I would like to know if there was a way to minimize it or simplify, because it is basically the same thing over and over again in the same script. Maybe an array or something?

 var isCtrl = false;

 $(document).keyup(function (e) {
    if(e.which == 17 || e.which == 91) isCtrl=false;
 }).keydown(function (e) {

    // Check if CTRL || CMND is hold down
    if(e.which == 17 || e.which == 91) isCtrl=true;

    // CTRL+S || CMND+S
    if(e.which == 83 && isCtrl == true) {
        ctrl_s_command();
        return false;
    }

    /** HOTKEYS **/

    // ESC Key
    if (e.keyCode == 27) {
        esc_key_command();
        return false;
    }

    // Enter Key
    if (e.keyCode == 13) {
        enter_key_command();
        return false;
    }

    // Space Bar
    if (e.keyCode == 32) {
        space_bar_command();
        return false;
    }

    // Alt Key
    if (e.keyCode == 18) {
        alt_key_command();
        return false;
    }

    // Tab key
    if (e.keyCode == 9) {
        tab_key_command();
        return false;
    }

    // Shift key
    if (e.keyCode == 16) {
        shift_key_command();
        return false;
    }

    // Caps Lock
    if (e.keyCode == 20) {
        caps_lock_command();
        return false;
    }

    // Backspace key
    if (e.keyCode == 8) {
        backspace_key_command();
        return false;
    }

    // Home key
    if (e.keyCode == 36) {
        home_key_command();
        return false;
    }

    // End key
    if (e.keyCode == 35) {
        end_key_command();
        return false;
    }

    // Delete key
    if (e.keyCode == 46) {
        delete_key_command();
        return false;
    }

    // Insert key
    if (e.keyCode == 45) {
        insert_key_command();
        return false;
    }

    // Page Up key
    if (e.keyCode == 33) {
        page_up_key_command();
        return false;
    }

    // Page Down key
    if (e.keyCode == 34) {
        page_down_key_command();
        return false;
    }

    // Numlock key
    if (e.keyCode == 144) {
        numlock_key_command();
        return false;
    }

    // Scroll Lock key
    if (e.keyCode == 145) {
        scroll_lock_command();
        return false;
    }

    // Pause Break key
    if (e.keyCode == 19) {
        pause_break_command();
        return false;
    }

    /** ARROWS **/

    // Left Arrow Key
    if (e.keyCode == 37) {
        left_arrow_command();
        return false;
    }

    // Up Arrow Key
    if (e.keyCode == 38) {
        up_arrow_command();
        return false;
    }

    // Right Arrow Key
    if (e.keyCode == 39) {
        right_arrow_command();
        return false;
    }

    // Down Arrow Key
    if (e.keyCode == 40) {
        down_arrow_command();
        return false;
    }

    /** F Keys **/

    // F1 Key
    if (e.keyCode == 112) {
        f1_key_command();
        return false;
    }

    // F2 Key
    if (e.keyCode == 113) {
        f2_key_command();
        return false;
    }

    // F3 Key
    if (e.keyCode == 114) {
        f3_key_command();
        return false;
    }

    // F4 Key
    if (e.keyCode == 115) {
        f4_key_command();
        return false;
    }

    // F5 Key
    if (e.keyCode == 116) {
        f5_key_command();
        return false;
    }

    // F6 Key
    if (e.keyCode == 117) {
        f6_key_command();
        return false;
    }

    // F7 Key
    if (e.keyCode == 118) {
        f7_key_command();
        return false;
    }

    // F8 Key
    if (e.keyCode == 119) {
        f8_key_command();
        return false;
    }

    // F9 Key
    if (e.keyCode == 120) {
        f9_key_command();
        return false;
    }

    // F10 Key
    if (e.keyCode == 121) {
        f10_key_command();
        return false;
    }

    // F11 Key
    if (e.keyCode == 122) {
        f11_key_command();
        return false;
    }

    // F12 Key
    if (e.keyCode == 123) {
        f12_key_command();
        return false;
    }

 });
4

1 に答える 1

2

You can declare your functions like so:

var keyCommand = {};
    // ESC Command
    keyCommand[27] = function() { alert("Escape"); };
    // Enter Command
    keyCommand[13] = function() { alert("Enter"); };
    // Tab key Command
    keyCommand[9] = function() {  alert("Tab"); };
    // etc etc

or assign them to already defined functions:

var keyCommand = {};
    // ESC Command
    keyCommand[27] = esc_key_command;
    // Enter Command
    keyCommand[13] = enter_key_command;
    // Tab key Command
    keyCommand[9] = tab_key_command;
    // etc etc

and then call the relevant function using

 if (typeof keyCommand[e.keyCode] == 'function') 
    keyCommand[e.keyCode]();
于 2012-12-02T19:56:39.623 に答える