2
* Copyright (C) 2006, 2007, 2010 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#include "PlatformKeyboardEvent.h"
30
#include <wtf/ASCIICType.h>
32
#ifndef MAPVK_VSC_TO_VK_EX
33
#define MAPVK_VSC_TO_VK_EX 3
40
static const unsigned short HIGH_BIT_MASK_SHORT = 0x8000;
42
// FIXME: This is incomplete. We could change this to mirror
43
// more like what Firefox does, and generate these switch statements
45
static String keyIdentifierForWindowsKeyCode(unsigned short keyCode)
132
return "PrintScreen";
141
// Standard says that DEL becomes U+007F.
145
return String::format("U+%04X", toASCIIUpper(keyCode));
149
static bool isKeypadEvent(WPARAM code, LPARAM keyData, PlatformEvent::Type type)
151
if (type != PlatformEvent::RawKeyDown && type != PlatformEvent::KeyUp)
174
return HIWORD(keyData) & KF_EXTENDED;
185
return !(HIWORD(keyData) & KF_EXTENDED);
191
static int windowsKeycodeWithLocation(WPARAM keycode, LPARAM keyData)
193
if (keycode != VK_CONTROL && keycode != VK_MENU && keycode != VK_SHIFT)
196
// If we don't need to support Windows XP or older Windows,
197
// it might be better to use MapVirtualKeyEx with scancode and
198
// extended keycode (i.e. 0xe0 or 0xe1).
199
if ((keyData >> 16) & KF_EXTENDED) {
212
int scancode = (keyData >> 16) & 0xFF;
213
int regeneratedVirtualKeyCode = ::MapVirtualKey(scancode, MAPVK_VSC_TO_VK_EX);
214
return regeneratedVirtualKeyCode ? regeneratedVirtualKeyCode : keycode;
217
static inline String singleCharacterString(UChar c)
219
return String(&c, 1);
222
PlatformKeyboardEvent::PlatformKeyboardEvent(HWND, WPARAM code, LPARAM keyData, Type type, bool systemKey)
223
: 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)
224
, m_text((type == PlatformEvent::Char) ? singleCharacterString(code) : String())
225
, m_unmodifiedText((type == PlatformEvent::Char) ? singleCharacterString(code) : String())
226
, m_keyIdentifier((type == PlatformEvent::Char) ? String() : keyIdentifierForWindowsKeyCode(code))
227
, m_windowsVirtualKeyCode((type == RawKeyDown || type == KeyUp) ? windowsKeycodeWithLocation(code, keyData) : 0)
228
, m_nativeVirtualKeyCode(m_windowsVirtualKeyCode)
230
, m_autoRepeat(HIWORD(keyData) & KF_REPEAT)
231
, m_isKeypad(isKeypadEvent(code, keyData, type))
232
, m_isSystemKey(systemKey)
236
void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type, bool)
238
// No KeyDown events on Windows to disambiguate.
239
ASSERT_NOT_REACHED();
242
bool PlatformKeyboardEvent::currentCapsLockState()
244
return GetKeyState(VK_CAPITAL) & 1;
247
void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
249
shiftKey = GetKeyState(VK_SHIFT) & HIGH_BIT_MASK_SHORT;
250
ctrlKey = GetKeyState(VK_CONTROL) & HIGH_BIT_MASK_SHORT;
251
altKey = GetKeyState(VK_MENU) & HIGH_BIT_MASK_SHORT;