1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-07-08 05:48:09 +02:00

Add experimental key input

This commit is contained in:
Elias Steurer 2020-07-03 14:36:42 +02:00
parent 1ee39de01f
commit be60ca06b1
2 changed files with 68 additions and 12 deletions

View File

@ -14,10 +14,56 @@ BOOL WINAPI SearchForWorkerWindow(HWND hwnd, LPARAM lparam)
}
HHOOK g_mouseHook;
HHOOK g_keyHook;
QPoint g_LastMousePosition { 0, 0 };
QQuickView* g_winGlobalHook = nullptr;
LRESULT __stdcall MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
LRESULT __stdcall WinWindow::KeyHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
QString activeWindowName = printWindowNameByhWnd(GetForegroundWindow());
if (activeWindowName != "Window") {
qInfo() << "Ignore input" << activeWindowName;
return CallNextHookEx(g_keyHook, nCode, wParam, lParam);
} else {
qInfo() << activeWindowName;
}
QKeyEvent::Type type;
switch (wParam) {
case WM_KEYDOWN:
type = QKeyEvent::Type::KeyPress;
qInfo() << "KeyPress";
qInfo() << " Window " << GetForegroundWindow();
case WM_KEYUP:
type = QKeyEvent::Type::KeyRelease;
qInfo() << "KeyRelease";
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
break;
}
KBDLLHOOKSTRUCT kbdStruct;
kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
if (kbdStruct.vkCode == VK_SPACE) {
auto event = QKeyEvent(QKeyEvent::Type::KeyPress, Qt::Key_Space, Qt::NoModifier, " ");
QTimer::singleShot(100, []() {
auto eventRelease = QKeyEvent(QKeyEvent::Type::KeyRelease, Qt::Key_A, Qt::KeyboardModifier::NoModifier, " ");
qInfo() << "eventRelease QKeyEvent";
QApplication::sendEvent(g_winGlobalHook, &eventRelease);
});
QApplication::sendEvent(g_winGlobalHook, &event);
}
return CallNextHookEx(g_keyHook, nCode, wParam, lParam);
}
LRESULT __stdcall WinWindow::MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
Qt::MouseButton mouseButton {};
@ -80,13 +126,13 @@ WinWindow::WinWindow(
: BaseWindow(projectPath, activeScreensList, checkWallpaperVisible)
{
m_windowHandle = reinterpret_cast<HWND>(m_window.winId());
if (!IsWindow(m_windowHandle)) {
qFatal("Could not get a valid window handle!");
}
ShowWindow(m_windowHandleWorker, SW_HIDE);
setAppID(id);
bool ok = false;
@ -247,6 +293,9 @@ void WinWindow::setupWindowMouseHook()
if (!(g_mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, nullptr, 0))) {
qDebug() << "Faild to install mouse hook!";
}
if (!(g_mouseHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyHookCallback, nullptr, 0))) {
qDebug() << "Faild to install key hook!";
}
}
}
@ -261,14 +310,14 @@ bool WinWindow::searchWorkerWindowToParentTo()
return EnumWindows(SearchForWorkerWindow, reinterpret_cast<LPARAM>(&m_windowHandleWorker));
}
QString printWindowNameByhWnd(HWND hWnd)
QString WinWindow::printWindowNameByhWnd(HWND hWnd)
{
std::wstring title(GetWindowTextLength(hWnd) + 1, L'\0');
GetWindowTextW(hWnd, &title[0], title.size());
return QString::fromStdWString(title);
}
BOOL CALLBACK FindTheDesiredWnd(HWND hWnd, LPARAM lParam)
BOOL CALLBACK WinWindow::findTheDesiredWnd(HWND hWnd, LPARAM lParam)
{
DWORD dwStyle = (DWORD)GetWindowLong(hWnd, GWL_STYLE);
if ((dwStyle & WS_MAXIMIZE) != 0) {
@ -283,7 +332,7 @@ struct sEnumInfo {
HMONITOR hMonitor = NULL;
};
BOOL CALLBACK GetMonitorByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
BOOL CALLBACK WinWindow::getMonitorByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
auto info = (sEnumInfo*)dwData;
if (info->hMonitor == hMonitor)
@ -292,12 +341,12 @@ BOOL CALLBACK GetMonitorByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcM
return TRUE;
}
int GetMonitorIndex(HMONITOR hMonitor)
int WinWindow::getMonitorIndex(HMONITOR hMonitor)
{
sEnumInfo info;
info.hMonitor = hMonitor;
if (EnumDisplayMonitors(NULL, NULL, GetMonitorByHandle, (LPARAM)&info))
if (EnumDisplayMonitors(NULL, NULL, getMonitorByHandle, (LPARAM)&info))
return -1;
return info.iIndex;
@ -307,15 +356,15 @@ void WinWindow::checkForFullScreenWindow()
{
HWND hFoundWnd = nullptr;
EnumWindows(&FindTheDesiredWnd, reinterpret_cast<LPARAM>(&hFoundWnd));
EnumWindows(&findTheDesiredWnd, reinterpret_cast<LPARAM>(&hFoundWnd));
// True if one window has WS_MAXIMIZE
if (hFoundWnd != nullptr) {
DWORD dwFlags = 0;
HMONITOR monitor = MonitorFromWindow(hFoundWnd, dwFlags);
HMONITOR wallpaper = MonitorFromWindow(m_windowHandle, dwFlags);
int monitorIndex = GetMonitorIndex(monitor);
int wallpaperIndex = GetMonitorIndex(wallpaper);
int monitorIndex = getMonitorIndex(monitor);
int wallpaperIndex = getMonitorIndex(wallpaper);
// qDebug() << monitorIndex << wallpaperIndex;
// If the window that has WS_MAXIMIZE is at the same monitor as this wallpaper

View File

@ -52,8 +52,6 @@
#include "basewindow.h"
#include "windowsdesktopproperties.h"
class WinWindow : public BaseWindow {
Q_OBJECT
@ -73,6 +71,14 @@ private:
void setupWallpaperForMultipleScreens(const QVector<int>& activeScreensList);
void setupWindowMouseHook();
bool searchWorkerWindowToParentTo();
static QString printWindowNameByhWnd(HWND hWnd);
static LRESULT KeyHookCallback(int nCode, WPARAM wParam, LPARAM lParam);
static LRESULT MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam);
int getMonitorIndex(HMONITOR hMonitor);
static BOOL getMonitorByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);
static BOOL findTheDesiredWnd(HWND hWnd, LPARAM lParam);
private slots:
void checkForFullScreenWindow();
@ -85,4 +91,5 @@ private:
HWND m_windowHandleWorker;
QTimer m_checkForFullScreenWindowTimer;
WindowsDesktopProperties m_windowsDesktopProperties;
};