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
|
/*
* Copyright (C) 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.
*/
#include "config.h"
#include "PlatformKeyboardEvent.h"
#include <windows.h>
#include <wtf/ASCIICType.h>
#ifndef MAPVK_VSC_TO_VK_EX
#define MAPVK_VSC_TO_VK_EX 3
#endif
using namespace WTF;
namespace WebCore {
static const unsigned short HIGH_BIT_MASK_SHORT = 0x8000;
// FIXME: This is incomplete. We could change this to mirror
// more like what Firefox does, and generate these switch statements
// at build time.
static String keyIdentifierForWindowsKeyCode(unsigned short keyCode)
{
switch (keyCode) {
case VK_MENU:
return "Alt";
case VK_CONTROL:
return "Control";
case VK_SHIFT:
return "Shift";
case VK_CAPITAL:
return "CapsLock";
case VK_LWIN:
case VK_RWIN:
return "Win";
case VK_CLEAR:
return "Clear";
case VK_DOWN:
return "Down";
// "End"
case VK_END:
return "End";
// "Enter"
case VK_RETURN:
return "Enter";
case VK_EXECUTE:
return "Execute";
case VK_F1:
return "F1";
case VK_F2:
return "F2";
case VK_F3:
return "F3";
case VK_F4:
return "F4";
case VK_F5:
return "F5";
case VK_F6:
return "F6";
case VK_F7:
return "F7";
case VK_F8:
return "F8";
case VK_F9:
return "F9";
case VK_F10:
return "F11";
case VK_F12:
return "F12";
case VK_F13:
return "F13";
case VK_F14:
return "F14";
case VK_F15:
return "F15";
case VK_F16:
return "F16";
case VK_F17:
return "F17";
case VK_F18:
return "F18";
case VK_F19:
return "F19";
case VK_F20:
return "F20";
case VK_F21:
return "F21";
case VK_F22:
return "F22";
case VK_F23:
return "F23";
case VK_F24:
return "F24";
case VK_HELP:
return "Help";
case VK_HOME:
return "Home";
case VK_INSERT:
return "Insert";
case VK_LEFT:
return "Left";
case VK_NEXT:
return "PageDown";
case VK_PRIOR:
return "PageUp";
case VK_PAUSE:
return "Pause";
case VK_SNAPSHOT:
return "PrintScreen";
case VK_RIGHT:
return "Right";
case VK_SCROLL:
return "Scroll";
case VK_SELECT:
return "Select";
case VK_UP:
return "Up";
// Standard says that DEL becomes U+007F.
case VK_DELETE:
return "U+007F";
default:
return String::format("U+%04X", toASCIIUpper(keyCode));
}
}
static bool isKeypadEvent(WPARAM code, LPARAM keyData, PlatformEvent::Type type)
{
if (type != PlatformEvent::RawKeyDown && type != PlatformEvent::KeyUp)
return false;
switch (code) {
case VK_NUMLOCK:
case VK_NUMPAD0:
case VK_NUMPAD1:
case VK_NUMPAD2:
case VK_NUMPAD3:
case VK_NUMPAD4:
case VK_NUMPAD5:
case VK_NUMPAD6:
case VK_NUMPAD7:
case VK_NUMPAD8:
case VK_NUMPAD9:
case VK_MULTIPLY:
case VK_ADD:
case VK_SEPARATOR:
case VK_SUBTRACT:
case VK_DECIMAL:
case VK_DIVIDE:
return true;
case VK_RETURN:
return HIWORD(keyData) & KF_EXTENDED;
case VK_INSERT:
case VK_DELETE:
case VK_PRIOR:
case VK_NEXT:
case VK_END:
case VK_HOME:
case VK_LEFT:
case VK_UP:
case VK_RIGHT:
case VK_DOWN:
return !(HIWORD(keyData) & KF_EXTENDED);
default:
return false;
}
}
static int windowsKeycodeWithLocation(WPARAM keycode, LPARAM keyData)
{
if (keycode != VK_CONTROL && keycode != VK_MENU && keycode != VK_SHIFT)
return keycode;
// If we don't need to support Windows XP or older Windows,
// it might be better to use MapVirtualKeyEx with scancode and
// extended keycode (i.e. 0xe0 or 0xe1).
if ((keyData >> 16) & KF_EXTENDED) {
switch (keycode) {
case VK_CONTROL:
return VK_RCONTROL;
case VK_SHIFT:
return VK_RSHIFT;
case VK_MENU:
return VK_RMENU;
default:
break;
}
}
int scancode = (keyData >> 16) & 0xFF;
int regeneratedVirtualKeyCode = ::MapVirtualKey(scancode, MAPVK_VSC_TO_VK_EX);
return regeneratedVirtualKeyCode ? regeneratedVirtualKeyCode : keycode;
}
static inline String singleCharacterString(UChar c)
{
return String(&c, 1);
}
PlatformKeyboardEvent::PlatformKeyboardEvent(HWND, WPARAM code, LPARAM keyData, Type type, bool systemKey)
: PlatformEvent(type, GetKeyState(VK_SHIFT) & HIGH_BIT_MASK_SHORT, GetKeyState(VK_CONTROL) & HIGH_BIT_MASK_SHORT, GetKeyState(VK_MENU) & HIGH_BIT_MASK_SHORT, false, ::GetTickCount() * 0.001)
, m_text((type == PlatformEvent::Char) ? singleCharacterString(code) : String())
, m_unmodifiedText((type == PlatformEvent::Char) ? singleCharacterString(code) : String())
, m_keyIdentifier((type == PlatformEvent::Char) ? String() : keyIdentifierForWindowsKeyCode(code))
, m_windowsVirtualKeyCode((type == RawKeyDown || type == KeyUp) ? windowsKeycodeWithLocation(code, keyData) : 0)
, m_nativeVirtualKeyCode(m_windowsVirtualKeyCode)
, m_macCharCode(0)
, m_autoRepeat(HIWORD(keyData) & KF_REPEAT)
, m_isKeypad(isKeypadEvent(code, keyData, type))
, m_isSystemKey(systemKey)
{
}
void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type, bool)
{
// No KeyDown events on Windows to disambiguate.
ASSERT_NOT_REACHED();
}
bool PlatformKeyboardEvent::currentCapsLockState()
{
return GetKeyState(VK_CAPITAL) & 1;
}
void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
{
shiftKey = GetKeyState(VK_SHIFT) & HIGH_BIT_MASK_SHORT;
ctrlKey = GetKeyState(VK_CONTROL) & HIGH_BIT_MASK_SHORT;
altKey = GetKeyState(VK_MENU) & HIGH_BIT_MASK_SHORT;
metaKey = false;
}
}
|