This source file includes following definitions.
- isKeyDown
- getLocationModifier
- SetToggleKeyState
- keyboardEvent
- GetRelativeCursorPos
- resetLastClickState
- isSystemKeyEvent
- mouseEvent
- mouseWheelEvent
#include "config.h"
#include "WebInputEventFactory.h"
#include "WebInputEvent.h"
#include "wtf/Assertions.h"
namespace blink {
static const unsigned long defaultScrollLinesPerWheelDelta = 3;
static const unsigned long defaultScrollCharsPerWheelDelta = 1;
static bool isKeyDown(WPARAM wparam)
{
return GetKeyState(wparam) & 0x8000;
}
static int getLocationModifier(WPARAM wparam, LPARAM lparam)
{
int modifier = 0;
switch (wparam) {
case VK_RETURN:
if ((lparam >> 16) & KF_EXTENDED)
modifier = WebInputEvent::IsKeyPad;
break;
case VK_INSERT:
case VK_DELETE:
case VK_HOME:
case VK_END:
case VK_PRIOR:
case VK_NEXT:
case VK_UP:
case VK_DOWN:
case VK_LEFT:
case VK_RIGHT:
if (!((lparam >> 16) & KF_EXTENDED))
modifier = WebInputEvent::IsKeyPad;
break;
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_DIVIDE:
case VK_MULTIPLY:
case VK_SUBTRACT:
case VK_ADD:
case VK_DECIMAL:
case VK_CLEAR:
modifier = WebInputEvent::IsKeyPad;
break;
case VK_SHIFT:
if (isKeyDown(VK_LSHIFT))
modifier = WebInputEvent::IsLeft;
else if (isKeyDown(VK_RSHIFT))
modifier = WebInputEvent::IsRight;
break;
case VK_CONTROL:
if (isKeyDown(VK_LCONTROL))
modifier = WebInputEvent::IsLeft;
else if (isKeyDown(VK_RCONTROL))
modifier = WebInputEvent::IsRight;
break;
case VK_MENU:
if (isKeyDown(VK_LMENU))
modifier = WebInputEvent::IsLeft;
else if (isKeyDown(VK_RMENU))
modifier = WebInputEvent::IsRight;
break;
case VK_LWIN:
modifier = WebInputEvent::IsLeft;
break;
case VK_RWIN:
modifier = WebInputEvent::IsRight;
break;
}
ASSERT(!modifier
|| modifier == WebInputEvent::IsKeyPad
|| modifier == WebInputEvent::IsLeft
|| modifier == WebInputEvent::IsRight);
return modifier;
}
static void SetToggleKeyState(WebInputEvent* event)
{
if (::GetKeyState(VK_NUMLOCK) & 1)
event->modifiers |= WebInputEvent::NumLockOn;
if (::GetKeyState(VK_CAPITAL) & 1)
event->modifiers |= WebInputEvent::CapsLockOn;
}
WebKeyboardEvent WebInputEventFactory::keyboardEvent(HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam)
{
WebKeyboardEvent result;
result.timeStampSeconds = GetMessageTime() / 1000.0;
result.windowsKeyCode = static_cast<int>(wparam);
result.nativeKeyCode = static_cast<int>(lparam);
switch (message) {
case WM_SYSKEYDOWN:
result.isSystemKey = true;
case WM_KEYDOWN:
result.type = WebInputEvent::RawKeyDown;
break;
case WM_SYSKEYUP:
result.isSystemKey = true;
case WM_KEYUP:
result.type = WebInputEvent::KeyUp;
break;
case WM_IME_CHAR:
result.type = WebInputEvent::Char;
break;
case WM_SYSCHAR:
result.isSystemKey = true;
result.type = WebInputEvent::Char;
case WM_CHAR:
result.type = WebInputEvent::Char;
break;
default:
ASSERT_NOT_REACHED();
}
if (result.type == WebInputEvent::Char || result.type == WebInputEvent::RawKeyDown) {
result.text[0] = result.windowsKeyCode;
result.unmodifiedText[0] = result.windowsKeyCode;
}
if (result.type != WebInputEvent::Char)
result.setKeyIdentifierFromWindowsKeyCode();
if (GetKeyState(VK_SHIFT) & 0x8000)
result.modifiers |= WebInputEvent::ShiftKey;
if (GetKeyState(VK_CONTROL) & 0x8000)
result.modifiers |= WebInputEvent::ControlKey;
if (GetKeyState(VK_MENU) & 0x8000)
result.modifiers |= WebInputEvent::AltKey;
if (LOWORD(lparam) > 1)
result.modifiers |= WebInputEvent::IsAutoRepeat;
result.modifiers |= getLocationModifier(wparam, lparam);
SetToggleKeyState(&result);
return result;
}
static int gLastClickCount;
static double gLastClickTime;
static LPARAM GetRelativeCursorPos(HWND hwnd)
{
POINT pos = {-1, -1};
GetCursorPos(&pos);
ScreenToClient(hwnd, &pos);
return MAKELPARAM(pos.x, pos.y);
}
void WebInputEventFactory::resetLastClickState()
{
gLastClickTime = gLastClickCount = 0;
}
bool WebInputEventFactory::isSystemKeyEvent(const WebKeyboardEvent& event)
{
return event.modifiers & WebInputEvent::AltKey || event.windowsKeyCode == VK_F10;
}
WebMouseEvent WebInputEventFactory::mouseEvent(HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam)
{
WebMouseEvent result;
switch (message) {
case WM_MOUSEMOVE:
result.type = WebInputEvent::MouseMove;
if (wparam & MK_LBUTTON)
result.button = WebMouseEvent::ButtonLeft;
else if (wparam & MK_MBUTTON)
result.button = WebMouseEvent::ButtonMiddle;
else if (wparam & MK_RBUTTON)
result.button = WebMouseEvent::ButtonRight;
else
result.button = WebMouseEvent::ButtonNone;
break;
case WM_MOUSELEAVE:
result.type = WebInputEvent::MouseLeave;
result.button = WebMouseEvent::ButtonNone;
lparam = GetRelativeCursorPos(hwnd);
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
result.type = WebInputEvent::MouseDown;
result.button = WebMouseEvent::ButtonLeft;
break;
case WM_MBUTTONDOWN:
case WM_MBUTTONDBLCLK:
result.type = WebInputEvent::MouseDown;
result.button = WebMouseEvent::ButtonMiddle;
break;
case WM_RBUTTONDOWN:
case WM_RBUTTONDBLCLK:
result.type = WebInputEvent::MouseDown;
result.button = WebMouseEvent::ButtonRight;
break;
case WM_LBUTTONUP:
result.type = WebInputEvent::MouseUp;
result.button = WebMouseEvent::ButtonLeft;
break;
case WM_MBUTTONUP:
result.type = WebInputEvent::MouseUp;
result.button = WebMouseEvent::ButtonMiddle;
break;
case WM_RBUTTONUP:
result.type = WebInputEvent::MouseUp;
result.button = WebMouseEvent::ButtonRight;
break;
default:
ASSERT_NOT_REACHED();
}
result.timeStampSeconds = GetMessageTime() / 1000.0;
result.x = static_cast<short>(LOWORD(lparam));
result.y = static_cast<short>(HIWORD(lparam));
result.windowX = result.x;
result.windowY = result.y;
POINT globalPoint = { result.x, result.y };
ClientToScreen(hwnd, &globalPoint);
result.globalX = globalPoint.x;
result.globalY = globalPoint.y;
static int lastClickPositionX;
static int lastClickPositionY;
static WebMouseEvent::Button lastClickButton = WebMouseEvent::ButtonLeft;
double currentTime = result.timeStampSeconds;
bool cancelPreviousClick =
(abs(lastClickPositionX - result.x) > (GetSystemMetrics(SM_CXDOUBLECLK) / 2))
|| (abs(lastClickPositionY - result.y) > (GetSystemMetrics(SM_CYDOUBLECLK) / 2))
|| ((currentTime - gLastClickTime) * 1000.0 > GetDoubleClickTime());
if (result.type == WebInputEvent::MouseDown) {
if (!cancelPreviousClick && (result.button == lastClickButton))
++gLastClickCount;
else {
gLastClickCount = 1;
lastClickPositionX = result.x;
lastClickPositionY = result.y;
}
gLastClickTime = currentTime;
lastClickButton = result.button;
} else if (result.type == WebInputEvent::MouseMove
|| result.type == WebInputEvent::MouseLeave) {
if (cancelPreviousClick) {
gLastClickCount = 0;
lastClickPositionX = 0;
lastClickPositionY = 0;
gLastClickTime = 0;
}
}
result.clickCount = gLastClickCount;
if (wparam & MK_CONTROL)
result.modifiers |= WebInputEvent::ControlKey;
if (wparam & MK_SHIFT)
result.modifiers |= WebInputEvent::ShiftKey;
if (GetKeyState(VK_MENU) & 0x8000)
result.modifiers |= WebInputEvent::AltKey;
if (wparam & MK_LBUTTON)
result.modifiers |= WebInputEvent::LeftButtonDown;
if (wparam & MK_MBUTTON)
result.modifiers |= WebInputEvent::MiddleButtonDown;
if (wparam & MK_RBUTTON)
result.modifiers |= WebInputEvent::RightButtonDown;
SetToggleKeyState(&result);
return result;
}
WebMouseWheelEvent WebInputEventFactory::mouseWheelEvent(HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam)
{
WebMouseWheelEvent result;
result.type = WebInputEvent::MouseWheel;
result.timeStampSeconds = GetMessageTime() / 1000.0;
result.button = WebMouseEvent::ButtonNone;
typedef SHORT (WINAPI *GetKeyStateFunction)(int key);
GetKeyStateFunction getKeyState;
UINT keyState;
float wheelDelta;
bool horizontalScroll = false;
if ((message == WM_VSCROLL) || (message == WM_HSCROLL)) {
getKeyState = GetAsyncKeyState;
keyState = 0;
if (getKeyState(VK_SHIFT))
keyState |= MK_SHIFT;
if (getKeyState(VK_CONTROL))
keyState |= MK_CONTROL;
POINT cursorPosition = {0};
GetCursorPos(&cursorPosition);
result.globalX = cursorPosition.x;
result.globalY = cursorPosition.y;
switch (LOWORD(wparam)) {
case SB_LINEUP:
wheelDelta = WHEEL_DELTA;
break;
case SB_LINEDOWN:
wheelDelta = -WHEEL_DELTA;
break;
case SB_PAGEUP:
wheelDelta = 1;
result.scrollByPage = true;
break;
case SB_PAGEDOWN:
wheelDelta = -1;
result.scrollByPage = true;
break;
default:
wheelDelta = 0;
break;
}
if (message == WM_HSCROLL)
horizontalScroll = true;
} else {
getKeyState = GetKeyState;
keyState = GET_KEYSTATE_WPARAM(wparam);
result.globalX = static_cast<short>(LOWORD(lparam));
result.globalY = static_cast<short>(HIWORD(lparam));
wheelDelta = static_cast<float>(GET_WHEEL_DELTA_WPARAM(wparam));
if (message == WM_MOUSEHWHEEL) {
horizontalScroll = true;
wheelDelta = -wheelDelta;
}
}
if (keyState & MK_SHIFT)
horizontalScroll = true;
if (keyState & MK_SHIFT)
result.modifiers |= WebInputEvent::ShiftKey;
if (keyState & MK_CONTROL)
result.modifiers |= WebInputEvent::ControlKey;
if (getKeyState(VK_MENU) & 0x8000)
result.modifiers |= WebInputEvent::AltKey;
if (keyState & MK_LBUTTON)
result.modifiers |= WebInputEvent::LeftButtonDown;
if (keyState & MK_MBUTTON)
result.modifiers |= WebInputEvent::MiddleButtonDown;
if (keyState & MK_RBUTTON)
result.modifiers |= WebInputEvent::RightButtonDown;
SetToggleKeyState(&result);
POINT clientPoint = { result.globalX, result.globalY };
MapWindowPoints(0, hwnd, &clientPoint, 1);
result.x = clientPoint.x;
result.y = clientPoint.y;
result.windowX = result.x;
result.windowY = result.y;
static const float scrollbarPixelsPerLine = 100.0f / 3.0f;
wheelDelta /= WHEEL_DELTA;
float scrollDelta = wheelDelta;
if (horizontalScroll) {
unsigned long scrollChars = defaultScrollCharsPerWheelDelta;
SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &scrollChars, 0);
scrollDelta *= static_cast<float>(scrollChars) * scrollbarPixelsPerLine;
} else {
unsigned long scrollLines = defaultScrollLinesPerWheelDelta;
SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
if (scrollLines == WHEEL_PAGESCROLL)
result.scrollByPage = true;
if (!result.scrollByPage)
scrollDelta *= static_cast<float>(scrollLines) * scrollbarPixelsPerLine;
}
if (horizontalScroll) {
result.deltaX = scrollDelta;
result.wheelTicksX = wheelDelta;
} else {
result.deltaY = scrollDelta;
result.wheelTicksY = wheelDelta;
}
return result;
}
}