1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
/*
* Copyright (C) 2004, 2006, 2007, 2010 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"
#if PLATFORM(MAC)
#import "KeyEventCocoa.h"
#import "Logging.h"
#import "WindowsKeyboardCodes.h"
#import <Carbon/Carbon.h>
using namespace WTF;
namespace WebCore {
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;
}
if ([event modifierFlags] & NSNumericPadKeyMask)
return true;
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 inline bool isKeyUpEvent(NSEvent *event)
{
if ([event type] != NSFlagsChanged)
return [event type] == NSKeyUp;
// FIXME: This logic fails if the user presses both Shift keys at once, for example:
// we treat releasing one of them as keyDown.
switch ([event keyCode]) {
case 54: // Right Command
case 55: // Left Command
return ([event modifierFlags] & NSCommandKeyMask) == 0;
case 57: // Capslock
return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0;
case 56: // Left Shift
case 60: // Right Shift
return ([event modifierFlags] & NSShiftKeyMask) == 0;
case 58: // Left Alt
case 61: // Right Alt
return ([event modifierFlags] & NSAlternateKeyMask) == 0;
case 59: // Left Ctrl
case 62: // Right Ctrl
return ([event modifierFlags] & NSControlKeyMask) == 0;
case 63: // Function
return ([event modifierFlags] & NSFunctionKeyMask) == 0;
}
return false;
}
static inline String textFromEvent(NSEvent* event)
{
if ([event type] == NSFlagsChanged)
return "";
return [event characters];
}
static inline String unmodifiedTextFromEvent(NSEvent* event)
{
if ([event type] == NSFlagsChanged)
return "";
return [event charactersIgnoringModifiers];
}
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";
}
return keyIdentifierForCharCode([s characterAtIndex:0]);
}
static int windowsKeyCodeForKeyEvent(NSEvent *event)
{
int code = 0;
// There are several kinds of characters for which we produce key code from char code:
// 1. Roman letters. Windows keyboard layouts affect both virtual key codes and character codes for these,
// so e.g. 'A' gets the same keyCode on QWERTY, AZERTY or Dvorak layouts.
// 2. Keys for which there is no known Mac virtual key codes, like PrintScreen.
// 3. Certain punctuation keys. On Windows, these are also remapped depending on current keyboard layout,
// but see comment in windowsKeyCodeForCharCode().
if ([event type] == NSKeyDown || [event type] == NSKeyUp) {
// Cmd switches Roman letters for Dvorak-QWERTY layout, so try modified characters first.
NSString* s = [event characters];
code = [s length] > 0 ? windowsKeyCodeForCharCode([s characterAtIndex:0]) : 0;
if (code)
return code;
// Ctrl+A on an AZERTY keyboard would get VK_Q keyCode if we relied on -[NSEvent keyCode] below.
s = [event charactersIgnoringModifiers];
code = [s length] > 0 ? windowsKeyCodeForCharCode([s characterAtIndex:0]) : 0;
if (code)
return code;
}
// Map Mac virtual key code directly to Windows one for any keys not handled above.
// E.g. the key next to Caps Lock has the same Event.keyCode on U.S. keyboard ('A') and on Russian keyboard (CYRILLIC LETTER EF).
return windowsKeyCodeForKeyCode([event keyCode]);
}
PlatformKeyboardEvent::PlatformKeyboardEvent(NSEvent *event)
: m_type(isKeyUpEvent(event) ? PlatformKeyboardEvent::KeyUp : PlatformKeyboardEvent::KeyDown)
, m_text(textFromEvent(event))
, m_unmodifiedText(unmodifiedTextFromEvent(event))
, m_keyIdentifier(keyIdentifierForKeyEvent(event))
, m_autoRepeat(([event type] != NSFlagsChanged) && [event isARepeat])
, m_windowsVirtualKeyCode(windowsKeyCodeForKeyEvent(event))
, m_nativeVirtualKeyCode([event keyCode])
, m_isKeypad(isKeypadEvent(event))
, m_shiftKey([event modifierFlags] & NSShiftKeyMask)
, m_ctrlKey([event modifierFlags] & NSControlKeyMask)
, m_altKey([event modifierFlags] & NSAlternateKeyMask)
, m_metaKey([event modifierFlags] & NSCommandKeyMask)
, m_macEvent(event)
{
// Always use 13 for Enter/Return -- we don't want to use AppKit's different character for Enter.
if (m_windowsVirtualKeyCode == VK_RETURN) {
m_text = "\r";
m_unmodifiedText = "\r";
}
// AppKit sets text to "\x7F" for backspace, but the correct KeyboardEvent character code is 8.
if (m_windowsVirtualKeyCode == VK_BACK) {
m_text = "\x8";
m_unmodifiedText = "\x8";
}
// Always use 9 for Tab -- we don't want to use AppKit's different character for shift-tab.
if (m_windowsVirtualKeyCode == VK_TAB) {
m_text = "\x9";
m_unmodifiedText = "\x9";
}
}
void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
{
// Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
ASSERT(m_type == KeyDown);
ASSERT(type == RawKeyDown || type == Char);
m_type = type;
if (backwardCompatibilityMode)
return;
if (type == RawKeyDown) {
m_text = String();
m_unmodifiedText = String();
} else {
m_keyIdentifier = String();
m_windowsVirtualKeyCode = 0;
if (m_text.length() == 1 && (m_text[0U] >= 0xF700 && m_text[0U] <= 0xF7FF)) {
// According to NSEvents.h, OpenStep reserves the range 0xF700-0xF8FF for function keys. However, some actual private use characters
// happen to be in this range, e.g. the Apple logo (Option+Shift+K).
// 0xF7FF is an arbitrary cut-off.
m_text = String();
m_unmodifiedText = String();
}
}
}
bool PlatformKeyboardEvent::currentCapsLockState()
{
return GetCurrentKeyModifiers() & alphaLock;
}
void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
{
UInt32 currentModifiers = GetCurrentKeyModifiers();
shiftKey = currentModifiers & ::shiftKey;
ctrlKey = currentModifiers & ::controlKey;
altKey = currentModifiers & ::optionKey;
metaKey = currentModifiers & ::cmdKey;
}
}
#endif // PLATFORM(MAC)
|