📄 keyeventmac.mm
字号:
/* * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#import "config.h"#import "PlatformKeyboardEvent.h"#import "Logging.h"#import <Carbon/Carbon.h>#import <wtf/ASCIICType.h>using namespace WTF;namespace WebCore {static String keyIdentifierForKeyEvent(NSEvent* event){ if ([event type] == NSFlagsChanged) switch ([event keyCode]) { case 54: // Right Command case 55: // Left Command return "Meta"; case 57: // Capslock return "CapsLock"; case 56: // Left Shift case 60: // Right Shift return "Shift"; case 58: // Left Alt case 61: // Right Alt return "Alt"; case 59: // Left Ctrl case 62: // Right Ctrl return "Control"; default: ASSERT_NOT_REACHED(); return ""; } NSString *s = [event charactersIgnoringModifiers]; if ([s length] != 1) { LOG(Events, "received an unexpected number of characters in key event: %u", [s length]); return "Unidentified"; } unichar c = [s characterAtIndex:0]; switch (c) { // Each identifier listed in the DOM spec is listed here. // Many are simply commented out since they do not appear on standard Macintosh keyboards // or are on a key that doesn't have a corresponding character. // "Accept" // "AllCandidates" // "Alt" case NSMenuFunctionKey: return "Alt"; // "Apps" // "BrowserBack" // "BrowserForward" // "BrowserHome" // "BrowserRefresh" // "BrowserSearch" // "BrowserStop" // "CapsLock" // "Clear" case NSClearLineFunctionKey: return "Clear"; // "CodeInput" // "Compose" // "Control" // "Crsel" // "Convert" // "Copy" // "Cut" // "Down" case NSDownArrowFunctionKey: return "Down"; // "End" case NSEndFunctionKey: return "End"; // "Enter" case 0x3: case 0xA: case 0xD: // Macintosh calls the one on the main keyboard Return, but Windows calls it Enter, so we'll do the same for the DOM return "Enter"; // "EraseEof" // "Execute" case NSExecuteFunctionKey: return "Execute"; // "Exsel" // "F1" case NSF1FunctionKey: return "F1"; // "F2" case NSF2FunctionKey: return "F2"; // "F3" case NSF3FunctionKey: return "F3"; // "F4" case NSF4FunctionKey: return "F4"; // "F5" case NSF5FunctionKey: return "F5"; // "F6" case NSF6FunctionKey: return "F6"; // "F7" case NSF7FunctionKey: return "F7"; // "F8" case NSF8FunctionKey: return "F8"; // "F9" case NSF9FunctionKey: return "F9"; // "F10" case NSF10FunctionKey: return "F10"; // "F11" case NSF11FunctionKey: return "F11"; // "F12" case NSF12FunctionKey: return "F12"; // "F13" case NSF13FunctionKey: return "F13"; // "F14" case NSF14FunctionKey: return "F14"; // "F15" case NSF15FunctionKey: return "F15"; // "F16" case NSF16FunctionKey: return "F16"; // "F17" case NSF17FunctionKey: return "F17"; // "F18" case NSF18FunctionKey: return "F18"; // "F19" case NSF19FunctionKey: return "F19"; // "F20" case NSF20FunctionKey: return "F20"; // "F21" case NSF21FunctionKey: return "F21"; // "F22" case NSF22FunctionKey: return "F22"; // "F23" case NSF23FunctionKey: return "F23"; // "F24" case NSF24FunctionKey: return "F24"; // "FinalMode" // "Find" case NSFindFunctionKey: return "Find"; // "FullWidth" // "HalfWidth" // "HangulMode" // "HanjaMode" // "Help" case NSHelpFunctionKey: return "Help"; // "Hiragana" // "Home" case NSHomeFunctionKey: return "Home"; // "Insert" case NSInsertFunctionKey: return "Insert"; // "JapaneseHiragana" // "JapaneseKatakana" // "JapaneseRomaji" // "JunjaMode" // "KanaMode" // "KanjiMode" // "Katakana" // "LaunchApplication1" // "LaunchApplication2" // "LaunchMail" // "Left" case NSLeftArrowFunctionKey: return "Left"; // "Meta" // "MediaNextTrack" // "MediaPlayPause" // "MediaPreviousTrack" // "MediaStop" // "ModeChange" case NSModeSwitchFunctionKey: return "ModeChange"; // "Nonconvert" // "NumLock" // "PageDown" case NSPageDownFunctionKey: return "PageDown"; // "PageUp" case NSPageUpFunctionKey: return "PageUp"; // "Paste" // "Pause" case NSPauseFunctionKey: return "Pause"; // "Play" // "PreviousCandidate" // "PrintScreen" case NSPrintScreenFunctionKey: return "PrintScreen"; // "Process" // "Props" // "Right" case NSRightArrowFunctionKey: return "Right"; // "RomanCharacters" // "Scroll" case NSScrollLockFunctionKey: return "Scroll"; // "Select" case NSSelectFunctionKey: return "Select"; // "SelectMedia" // "Shift" // "Stop" case NSStopFunctionKey: return "Stop"; // "Up" case NSUpArrowFunctionKey: return "Up"; // "Undo" case NSUndoFunctionKey: return "Undo"; // "VolumeDown" // "VolumeMute" // "VolumeUp" // "Win" // "Zoom" // More function keys, not in the key identifier specification. case NSF25FunctionKey: return "F25"; case NSF26FunctionKey: return "F26"; case NSF27FunctionKey: return "F27"; case NSF28FunctionKey: return "F28"; case NSF29FunctionKey: return "F29"; case NSF30FunctionKey: return "F30"; case NSF31FunctionKey: return "F31"; case NSF32FunctionKey: return "F32"; case NSF33FunctionKey: return "F33"; case NSF34FunctionKey: return "F34"; case NSF35FunctionKey: return "F35"; // Turn 0x7F into 0x08, because backspace needs to always be 0x08. case 0x7F: return "U+0008"; // Standard says that DEL becomes U+007F. case NSDeleteFunctionKey: return "U+007F"; // Always use 0x09 for tab instead of AppKit's backtab character. case NSBackTabCharacter: return "U+0009"; case NSBeginFunctionKey: case NSBreakFunctionKey: case NSClearDisplayFunctionKey: case NSDeleteCharFunctionKey: case NSDeleteLineFunctionKey: case NSInsertCharFunctionKey: case NSInsertLineFunctionKey: case NSNextFunctionKey: case NSPrevFunctionKey: case NSPrintFunctionKey: case NSRedoFunctionKey: case NSResetFunctionKey: case NSSysReqFunctionKey: case NSSystemFunctionKey: case NSUserFunctionKey: // FIXME: We should use something other than the vendor-area Unicode values for the above keys. // For now, just fall through to the default. default: return String::format("U+%04X", toASCIIUpper(c)); }}static bool isKeypadEvent(NSEvent* event){ // Check that this is the type of event that has a keyCode. switch ([event type]) { case NSKeyDown: case NSKeyUp: case NSFlagsChanged: break; default: return false; } switch ([event keyCode]) { case 71: // Clear case 81: // = case 75: // / case 67: // * case 78: // - case 69: // + case 76: // Enter case 65: // . case 82: // 0 case 83: // 1 case 84: // 2 case 85: // 3 case 86: // 4 case 87: // 5 case 88: // 6 case 89: // 7 case 91: // 8 case 92: // 9 return true; } return false;}static int windowsKeyCodeForKeyEvent(NSEvent* event){ switch ([event keyCode]) { // VK_TAB (09) TAB key case 48: return 0x09; // VK_APPS (5D) Right windows/meta key case 54: // Right Command return 0x5D; // VK_LWIN (5B) Left windows/meta key case 55: // Left Command return 0x5B; // VK_CAPITAL (14) caps locks key case 57: // Capslock return 0x14; // VK_SHIFT (10) either shift key case 56: // Left Shift case 60: // Right Shift return 0x10; // VK_MENU (12) either alt key case 58: // Left Alt case 61: // Right Alt return 0x12; // VK_CONTROL (11) either ctrl key case 59: // Left Ctrl case 62: // Right Ctrl return 0x11; // VK_CLEAR (0C) CLEAR key case 71: return 0x0C; // VK_NUMPAD0 (60) Numeric keypad 0 key case 82: return 0x60; // VK_NUMPAD1 (61) Numeric keypad 1 key case 83: return 0x61; // VK_NUMPAD2 (62) Numeric keypad 2 key case 84: return 0x62; // VK_NUMPAD3 (63) Numeric keypad 3 key case 85: return 0x63;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -