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 30WebInspector.KeyboardShortcut = function() 31{ 32}; 33 34/** 35 * Constants for encoding modifier key set as a bit mask. 36 * @see #_makeKeyFromCodeAndModifiers 37 */ 38WebInspector.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 get CtrlOrMeta() 45 { 46 // "default" command/ctrl key for platform, Command on Mac, Ctrl on other platforms 47 return WebInspector.isMac() ? this.Meta : this.Ctrl; 48 } 49}; 50 51WebInspector.KeyboardShortcut.Keys = { 52 Backspace: { code: 8, name: "\u21a4" }, 53 Tab: { code: 9, name: { mac: "\u21e5", other: "<Tab>" } }, 54 Enter: { code: 13, name: { mac: "\u21a9", other: "<Enter>" } }, 55 Esc: { code: 27, name: { mac: "\u238b", other: "<Esc>" } }, 56 Space: { code: 32, name: "<Space>" }, 57 PageUp: { code: 33, name: { mac: "\u21de", other: "<PageUp>" } }, // also NUM_NORTH_EAST 58 PageDown: { code: 34, name: { mac: "\u21df", other: "<PageDown>" } }, // also NUM_SOUTH_EAST 59 End: { code: 35, name: { mac: "\u2197", other: "<End>" } }, // also NUM_SOUTH_WEST 60 Home: { code: 36, name: { mac: "\u2196", other: "<Home>" } }, // also NUM_NORTH_WEST 61 Left: { code: 37, name: "\u2190" }, // also NUM_WEST 62 Up: { code: 38, name: "\u2191" }, // also NUM_NORTH 63 Right: { code: 39, name: "\u2192" }, // also NUM_EAST 64 Down: { code: 40, name: "\u2193" }, // also NUM_SOUTH 65 Delete: { code: 46, name: "<Del>" }, 66 Zero: { code: 48, name: "0" }, 67 F1: { code: 112, name: "F1" }, 68 F2: { code: 113, name: "F2" }, 69 F3: { code: 114, name: "F3" }, 70 F4: { code: 115, name: "F4" }, 71 F5: { code: 116, name: "F5" }, 72 F6: { code: 117, name: "F6" }, 73 F7: { code: 118, name: "F7" }, 74 F8: { code: 119, name: "F8" }, 75 F9: { code: 120, name: "F9" }, 76 F10: { code: 121, name: "F10" }, 77 F11: { code: 122, name: "F11" }, 78 F12: { code: 123, name: "F12" }, 79 Semicolon: { code: 186, name: ";" }, 80 Plus: { code: 187, name: "+" }, 81 Comma: { code: 188, name: "," }, 82 Minus: { code: 189, name: "-" }, 83 Period: { code: 190, name: "." }, 84 Slash: { code: 191, name: "/" }, 85 Apostrophe: { code: 192, name: "`" }, 86 SingleQuote: { code: 222, name: "\'" } 87}; 88 89/** 90 * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in the higher 8 bits. 91 * It is useful for matching pressed keys. 92 * keyCode is the Code of the key, or a character "a-z" which is converted to a keyCode value. 93 * optModifiers is an Optional list of modifiers passed as additional paramerters. 94 */ 95WebInspector.KeyboardShortcut.makeKey = function(keyCode, optModifiers) 96{ 97 if (typeof keyCode === "string") 98 keyCode = keyCode.charCodeAt(0) - 32; 99 var modifiers = WebInspector.KeyboardShortcut.Modifiers.None; 100 for (var i = 1; i < arguments.length; i++) 101 modifiers |= arguments[i]; 102 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, modifiers); 103}; 104 105WebInspector.KeyboardShortcut.makeKeyFromEvent = function(keyboardEvent) 106{ 107 var modifiers = WebInspector.KeyboardShortcut.Modifiers.None; 108 if (keyboardEvent.shiftKey) 109 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift; 110 if (keyboardEvent.ctrlKey) 111 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Ctrl; 112 if (keyboardEvent.altKey) 113 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Alt; 114 if (keyboardEvent.metaKey) 115 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Meta; 116 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyboardEvent.keyCode, modifiers); 117}; 118 119WebInspector.KeyboardShortcut.makeDescriptor = function(key, optModifiers) 120{ 121 var modifiers = 0; 122 for (var i = 1; i < arguments.length; i++) 123 modifiers |= arguments[i]; 124 125 return { 126 key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key : key.code, modifiers), 127 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers) 128 }; 129} 130 131WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers) 132{ 133 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInspector.KeyboardShortcut._keyName(key); 134} 135 136WebInspector.KeyboardShortcut._keyName = function(key) 137{ 138 if (typeof key === "string") 139 return key.toUpperCase(); 140 if (typeof key.name === "string") 141 return key.name; 142 return key.name[WebInspector.platform] || key.name.other; 143} 144 145WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers = function(keyCode, modifiers) 146{ 147 return (keyCode & 255) | (modifiers << 8); 148}; 149 150WebInspector.KeyboardShortcut._modifiersToString = function(modifiers) 151{ 152 const cmdKey = "\u2318"; 153 const optKey = "\u2325"; 154 const shiftKey = "\u21e7"; 155 const ctrlKey = "\u2303"; 156 157 var isMac = WebInspector.isMac(); 158 var res = ""; 159 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Ctrl) 160 res += isMac ? ctrlKey : "<Ctrl> + "; 161 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Alt) 162 res += isMac ? optKey : "<Alt> + "; 163 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift) 164 res += isMac ? shiftKey : "<Shift> + "; 165 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta) 166 res += isMac ? cmdKey : "<Win> + "; 167 168 return res; 169}; 170