Home | History | Annotate | Download | only in front-end
      1 /*
      2  * Copyright (C) 2009 Apple Inc. All rights reserved.
      3  * Copyright (C) 2009 Google Inc. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      9  * 1.  Redistributions of source code must retain the above copyright
     10  *     notice, this list of conditions and the following disclaimer.
     11  * 2.  Redistributions in binary form must reproduce the above copyright
     12  *     notice, this list of conditions and the following disclaimer in the
     13  *     documentation and/or other materials provided with the distribution.
     14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     15  *     its contributors may be used to endorse or promote products derived
     16  *     from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 WebInspector.KeyboardShortcut = function()
     31 {
     32 };
     33 
     34 /**
     35  * Constants for encoding modifier key set as a bit mask.
     36  * @see #_makeKeyFromCodeAndModifiers
     37  */
     38 WebInspector.KeyboardShortcut.Modifiers = {
     39     None: 0,   // Constant for empty modifiers set.
     40     Shift: 1,
     41     Ctrl: 2,
     42     Alt: 4,
     43     Meta: 8    // Command key on Mac, Win key on other platforms.
     44 };
     45 
     46 WebInspector.KeyboardShortcut.KeyCodes = {
     47     Backspace: 8,
     48     Tab: 9,
     49     Esc: 27,
     50     Space: 32,
     51     PageUp: 33,      // also NUM_NORTH_EAST
     52     PageDown: 34,    // also NUM_SOUTH_EAST
     53     End: 35,         // also NUM_SOUTH_WEST
     54     Home: 36,        // also NUM_NORTH_WEST
     55     Left: 37,        // also NUM_WEST
     56     Up: 38,          // also NUM_NORTH
     57     Right: 39,       // also NUM_EAST
     58     Down: 40,        // also NUM_SOUTH
     59     Delete: 46,
     60     Zero: 48,
     61     F1: 112,
     62     F2: 113,
     63     F3: 114,
     64     F4: 115,
     65     F5: 116,
     66     F6: 117,
     67     F7: 118,
     68     F8: 119,
     69     F9: 120,
     70     F10: 121,
     71     F11: 122,
     72     F12: 123,
     73     Semicolon: 186,    // ;
     74     Plus: 187,         // +
     75     Comma: 188,        // ,
     76     Minus: 189,        // -
     77     Period: 190,       // .
     78     Slash: 191,        // /
     79     Apostrophe: 192,   // `
     80     SingleQuote: 222   // '
     81 };
     82 
     83 /**
     84  * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in the higher 8 bits.
     85  * It is useful for matching pressed keys.
     86  * keyCode is the Code of the key, or a character "a-z" which is converted to a keyCode value.
     87  * optModifiers is an Optional list of modifiers passed as additional paramerters.
     88  */
     89 WebInspector.KeyboardShortcut.makeKey = function(keyCode, optModifiers)
     90 {
     91     if (typeof keyCode === "string")
     92         keyCode = keyCode.charCodeAt(0) - 32;
     93     var modifiers = WebInspector.KeyboardShortcut.Modifiers.None;
     94     for (var i = 1; i < arguments.length; i++)
     95         modifiers |= arguments[i];
     96     return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, modifiers);
     97 };
     98 
     99 WebInspector.KeyboardShortcut.makeKeyFromEvent = function(keyboardEvent)
    100 {
    101     var modifiers = WebInspector.KeyboardShortcut.Modifiers.None;
    102     if (keyboardEvent.shiftKey)
    103         modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift;
    104     if (keyboardEvent.ctrlKey)
    105         modifiers |= WebInspector.KeyboardShortcut.Modifiers.Ctrl;
    106     if (keyboardEvent.altKey)
    107         modifiers |= WebInspector.KeyboardShortcut.Modifiers.Alt;
    108     if (keyboardEvent.metaKey)
    109         modifiers |= WebInspector.KeyboardShortcut.Modifiers.Meta;
    110     return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyboardEvent.keyCode, modifiers);
    111 };
    112 
    113 WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers = function(keyCode, modifiers)
    114 {
    115     return (keyCode & 255) | (modifiers << 8);
    116 };
    117