2017-06-29 00:57:46 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2018 Albert Vaca Cintora <albertvaka@gmail.com>
|
2017-06-29 00:57:46 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2017-06-29 00:57:46 +01:00
|
|
|
*/
|
|
|
|
|
2018-04-02 19:20:24 +01:00
|
|
|
#include "windowsremoteinput.h"
|
|
|
|
|
2017-06-29 00:57:46 +01:00
|
|
|
#include <QCursor>
|
2018-10-30 10:16:20 +00:00
|
|
|
#include <QDebug>
|
2017-06-29 00:57:46 +01:00
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Translation table to keep in sync within all the implementations
|
2018-10-30 10:16:20 +00:00
|
|
|
int SpecialKeysMap[] = {
|
2022-09-10 22:23:52 +01:00
|
|
|
0, // Invalid
|
|
|
|
VK_BACK, // 1
|
|
|
|
VK_TAB, // 2
|
|
|
|
VK_RETURN, // 3
|
|
|
|
VK_LEFT, // 4
|
|
|
|
VK_UP, // 5
|
|
|
|
VK_RIGHT, // 6
|
|
|
|
VK_DOWN, // 7
|
|
|
|
VK_PRIOR, // 8
|
|
|
|
VK_NEXT, // 9
|
|
|
|
VK_HOME, // 10
|
|
|
|
VK_END, // 11
|
|
|
|
VK_RETURN, // 12
|
|
|
|
VK_DELETE, // 13
|
|
|
|
VK_ESCAPE, // 14
|
|
|
|
VK_SNAPSHOT, // 15
|
|
|
|
VK_SCROLL, // 16
|
|
|
|
0, // 17
|
|
|
|
0, // 18
|
|
|
|
0, // 19
|
|
|
|
0, // 20
|
|
|
|
VK_F1, // 21
|
|
|
|
VK_F2, // 22
|
|
|
|
VK_F3, // 23
|
|
|
|
VK_F4, // 24
|
|
|
|
VK_F5, // 25
|
|
|
|
VK_F6, // 26
|
|
|
|
VK_F7, // 27
|
|
|
|
VK_F8, // 28
|
|
|
|
VK_F9, // 29
|
|
|
|
VK_F10, // 30
|
|
|
|
VK_F11, // 31
|
|
|
|
VK_F12, // 32
|
2018-10-30 10:16:20 +00:00
|
|
|
};
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
template<typename T, size_t N>
|
|
|
|
size_t arraySize(T (&arr)[N])
|
|
|
|
{
|
|
|
|
(void)arr;
|
|
|
|
return N;
|
|
|
|
}
|
2018-10-30 10:16:20 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
WindowsRemoteInput::WindowsRemoteInput(QObject *parent)
|
2018-04-02 19:20:24 +01:00
|
|
|
: AbstractRemoteInput(parent)
|
2017-06-29 00:57:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool WindowsRemoteInput::handlePacket(const NetworkPacket &np)
|
2017-06-29 00:57:46 +01:00
|
|
|
{
|
|
|
|
float dx = np.get<float>(QStringLiteral("dx"), 0);
|
|
|
|
float dy = np.get<float>(QStringLiteral("dy"), 0);
|
|
|
|
|
|
|
|
bool isSingleClick = np.get<bool>(QStringLiteral("singleclick"), false);
|
|
|
|
bool isDoubleClick = np.get<bool>(QStringLiteral("doubleclick"), false);
|
|
|
|
bool isMiddleClick = np.get<bool>(QStringLiteral("middleclick"), false);
|
|
|
|
bool isRightClick = np.get<bool>(QStringLiteral("rightclick"), false);
|
|
|
|
bool isSingleHold = np.get<bool>(QStringLiteral("singlehold"), false);
|
|
|
|
bool isSingleRelease = np.get<bool>(QStringLiteral("singlerelease"), false);
|
|
|
|
bool isScroll = np.get<bool>(QStringLiteral("scroll"), false);
|
|
|
|
QString key = np.get<QString>(QStringLiteral("key"), QLatin1String(""));
|
|
|
|
int specialKey = np.get<int>(QStringLiteral("specialKey"), 0);
|
|
|
|
|
2021-06-29 02:32:33 +01:00
|
|
|
if (isSingleClick || isDoubleClick || isMiddleClick || isRightClick || isSingleHold || isScroll || isSingleRelease || !key.isEmpty() || specialKey) {
|
2022-09-10 22:23:52 +01:00
|
|
|
INPUT input = {0};
|
|
|
|
input.type = INPUT_MOUSE;
|
2017-06-29 00:57:46 +01:00
|
|
|
|
|
|
|
if (isSingleClick) {
|
2022-09-10 22:23:52 +01:00
|
|
|
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
|
|
|
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2017-06-29 00:57:46 +01:00
|
|
|
} else if (isDoubleClick) {
|
2022-09-10 22:23:52 +01:00
|
|
|
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
|
|
|
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
|
|
|
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
|
|
|
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
|
|
|
} else if (isMiddleClick) {
|
|
|
|
input.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
|
|
|
input.mi.dwFlags = MOUSEEVENTF_MIDDLEUP;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2017-06-29 00:57:46 +01:00
|
|
|
} else if (isRightClick) {
|
2022-09-10 22:23:52 +01:00
|
|
|
input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
|
|
|
input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
|
|
|
} else if (isSingleHold) {
|
|
|
|
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
|
|
|
} else if (isSingleRelease) {
|
|
|
|
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2017-06-29 00:57:46 +01:00
|
|
|
} else if (isScroll) {
|
2022-09-10 22:23:52 +01:00
|
|
|
input.mi.dwFlags = MOUSEEVENTF_WHEEL;
|
|
|
|
input.mi.mouseData = dy;
|
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
|
|
|
|
} else if (!key.isEmpty() || specialKey) {
|
|
|
|
input.type = INPUT_KEYBOARD;
|
|
|
|
|
|
|
|
input.ki.time = 0;
|
|
|
|
input.ki.dwExtraInfo = 0;
|
|
|
|
input.ki.wScan = 0;
|
|
|
|
input.ki.dwFlags = 0;
|
2017-06-29 00:57:46 +01:00
|
|
|
|
|
|
|
bool ctrl = np.get<bool>(QStringLiteral("ctrl"), false);
|
|
|
|
bool alt = np.get<bool>(QStringLiteral("alt"), false);
|
|
|
|
bool shift = np.get<bool>(QStringLiteral("shift"), false);
|
2018-11-07 16:13:18 +00:00
|
|
|
bool super = np.get<bool>(QStringLiteral("super"), false);
|
2017-06-29 00:57:46 +01:00
|
|
|
|
2018-10-30 10:16:20 +00:00
|
|
|
if (ctrl) {
|
|
|
|
input.ki.wVk = VK_LCONTROL;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
if (alt) {
|
|
|
|
input.ki.wVk = VK_LMENU;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
if (shift) {
|
|
|
|
input.ki.wVk = VK_LSHIFT;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
2018-11-07 14:59:26 +00:00
|
|
|
if (super) {
|
|
|
|
input.ki.wVk = VK_LWIN;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-11-07 14:59:26 +00:00
|
|
|
}
|
2017-06-29 00:57:46 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (specialKey) {
|
2017-06-29 00:57:46 +01:00
|
|
|
if (specialKey >= (int)arraySize(SpecialKeysMap)) {
|
|
|
|
qWarning() << "Unsupported special key identifier";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-30 10:16:20 +00:00
|
|
|
input.ki.wVk = SpecialKeysMap[specialKey];
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2017-06-29 00:57:46 +01:00
|
|
|
|
2018-10-30 10:16:20 +00:00
|
|
|
input.ki.dwFlags = KEYEVENTF_KEYUP;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2017-06-29 00:57:46 +01:00
|
|
|
|
|
|
|
} else {
|
2022-09-10 22:23:52 +01:00
|
|
|
for (int i = 0; i < key.length(); i++) {
|
2022-04-28 13:40:53 +01:00
|
|
|
wchar_t inputChar = (wchar_t)key.at(i).unicode();
|
2018-10-30 10:16:20 +00:00
|
|
|
short inputVk = VkKeyScanExW(inputChar, GetKeyboardLayout(0));
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (inputVk != -1) {
|
2018-10-30 10:16:20 +00:00
|
|
|
// Uses virtual keycodes so key combinations work
|
|
|
|
input.ki.wScan = 0;
|
|
|
|
input.ki.dwFlags = 0;
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (inputVk & 0x100) {
|
2018-10-30 10:16:20 +00:00
|
|
|
input.ki.wVk = VK_LSHIFT;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
if (inputVk & 0x200) {
|
|
|
|
input.ki.wVk = VK_LCONTROL;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
if (inputVk & 0x400) {
|
|
|
|
input.ki.wVk = VK_LMENU;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
input.ki.wVk = inputVk & 0xFF;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
|
|
|
|
input.ki.dwFlags = KEYEVENTF_KEYUP;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
|
|
|
|
if ((inputVk & 0x100) && !shift) {
|
|
|
|
input.ki.wVk = VK_LSHIFT;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
if ((inputVk & 0x200) && !ctrl) {
|
|
|
|
input.ki.wVk = VK_LCONTROL;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
if ((inputVk & 0x400) && !alt) {
|
|
|
|
input.ki.wVk = VK_LMENU;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Falls back to KEYEVENTF_UNICODE
|
|
|
|
input.ki.wVk = 0;
|
|
|
|
input.ki.wScan = inputChar;
|
|
|
|
input.ki.dwFlags = KEYEVENTF_UNICODE;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
|
|
|
|
input.ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_UNICODE;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2017-06-29 00:57:46 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
2017-06-29 00:57:46 +01:00
|
|
|
|
2018-10-30 10:16:20 +00:00
|
|
|
input.ki.dwFlags = KEYEVENTF_KEYUP;
|
|
|
|
input.ki.wScan = 0;
|
|
|
|
|
|
|
|
if (ctrl) {
|
|
|
|
input.ki.wVk = VK_LCONTROL;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
if (alt) {
|
|
|
|
input.ki.wVk = VK_LMENU;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-10-30 10:16:20 +00:00
|
|
|
}
|
|
|
|
if (shift) {
|
|
|
|
input.ki.wVk = VK_LSHIFT;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2017-06-29 00:57:46 +01:00
|
|
|
}
|
2018-11-07 14:59:26 +00:00
|
|
|
if (super) {
|
|
|
|
input.ki.wVk = VK_LWIN;
|
2022-09-10 22:23:52 +01:00
|
|
|
::SendInput(1, &input, sizeof(INPUT));
|
2018-11-07 14:59:26 +00:00
|
|
|
}
|
2017-06-29 00:57:46 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
} else { // Is a mouse move event
|
2021-06-01 19:41:16 +01:00
|
|
|
POINT point;
|
|
|
|
if (GetCursorPos(&point)) {
|
2021-08-08 22:36:56 +01:00
|
|
|
return SetCursorPos(point.x + (int)dx, point.y + (int)dy);
|
|
|
|
} else {
|
|
|
|
return false;
|
2021-06-01 19:41:16 +01:00
|
|
|
}
|
2017-06-29 00:57:46 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-18 20:58:02 +01:00
|
|
|
|
|
|
|
bool WindowsRemoteInput::hasKeyboardSupport()
|
|
|
|
{
|
|
|
|
return true;
|
2019-12-22 07:49:55 +00:00
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_windowsremoteinput.cpp"
|