jquery最好的方法来最小化命令脚本

时间:2012-12-02 15:24:43

标签: jquery plugins command simplify

我有这个关键命令脚本,每当我希望用户能够使用键盘做快捷方式时我都会包含这些命令。但是我想知道是否有办法将其最小化或简化,因为在同一个脚本中它一遍又一遍地基本相同。也许是数组或什么?

 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;
    }

 });

1 个答案:

答案 0 :(得分:2)

您可以声明您的功能:

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

或将它们分配给已定义的函数:

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

然后使用

调用相关函数
 if (typeof keyCommand[e.keyCode] == 'function') 
    keyCommand[e.keyCode]();
相关问题