1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-10-06 09:17:07 +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_mouseHook;
HHOOK g_keyHook;
QPoint g_LastMousePosition { 0, 0 }; QPoint g_LastMousePosition { 0, 0 };
QQuickView* g_winGlobalHook = nullptr; 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 {}; Qt::MouseButton mouseButton {};
@ -80,13 +126,13 @@ WinWindow::WinWindow(
: BaseWindow(projectPath, activeScreensList, checkWallpaperVisible) : BaseWindow(projectPath, activeScreensList, checkWallpaperVisible)
{ {
m_windowHandle = reinterpret_cast<HWND>(m_window.winId()); m_windowHandle = reinterpret_cast<HWND>(m_window.winId());
if (!IsWindow(m_windowHandle)) { if (!IsWindow(m_windowHandle)) {
qFatal("Could not get a valid window handle!"); qFatal("Could not get a valid window handle!");
} }
ShowWindow(m_windowHandleWorker, SW_HIDE); ShowWindow(m_windowHandleWorker, SW_HIDE);
setAppID(id); setAppID(id);
bool ok = false; bool ok = false;
@ -247,6 +293,9 @@ void WinWindow::setupWindowMouseHook()
if (!(g_mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, nullptr, 0))) { if (!(g_mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, nullptr, 0))) {
qDebug() << "Faild to install mouse hook!"; 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)); 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'); std::wstring title(GetWindowTextLength(hWnd) + 1, L'\0');
GetWindowTextW(hWnd, &title[0], title.size()); GetWindowTextW(hWnd, &title[0], title.size());
return QString::fromStdWString(title); 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); DWORD dwStyle = (DWORD)GetWindowLong(hWnd, GWL_STYLE);
if ((dwStyle & WS_MAXIMIZE) != 0) { if ((dwStyle & WS_MAXIMIZE) != 0) {
@ -283,7 +332,7 @@ struct sEnumInfo {
HMONITOR hMonitor = NULL; 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; auto info = (sEnumInfo*)dwData;
if (info->hMonitor == hMonitor) if (info->hMonitor == hMonitor)
@ -292,12 +341,12 @@ BOOL CALLBACK GetMonitorByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcM
return TRUE; return TRUE;
} }
int GetMonitorIndex(HMONITOR hMonitor) int WinWindow::getMonitorIndex(HMONITOR hMonitor)
{ {
sEnumInfo info; sEnumInfo info;
info.hMonitor = hMonitor; info.hMonitor = hMonitor;
if (EnumDisplayMonitors(NULL, NULL, GetMonitorByHandle, (LPARAM)&info)) if (EnumDisplayMonitors(NULL, NULL, getMonitorByHandle, (LPARAM)&info))
return -1; return -1;
return info.iIndex; return info.iIndex;
@ -307,15 +356,15 @@ void WinWindow::checkForFullScreenWindow()
{ {
HWND hFoundWnd = nullptr; HWND hFoundWnd = nullptr;
EnumWindows(&FindTheDesiredWnd, reinterpret_cast<LPARAM>(&hFoundWnd)); EnumWindows(&findTheDesiredWnd, reinterpret_cast<LPARAM>(&hFoundWnd));
// True if one window has WS_MAXIMIZE // True if one window has WS_MAXIMIZE
if (hFoundWnd != nullptr) { if (hFoundWnd != nullptr) {
DWORD dwFlags = 0; DWORD dwFlags = 0;
HMONITOR monitor = MonitorFromWindow(hFoundWnd, dwFlags); HMONITOR monitor = MonitorFromWindow(hFoundWnd, dwFlags);
HMONITOR wallpaper = MonitorFromWindow(m_windowHandle, dwFlags); HMONITOR wallpaper = MonitorFromWindow(m_windowHandle, dwFlags);
int monitorIndex = GetMonitorIndex(monitor); int monitorIndex = getMonitorIndex(monitor);
int wallpaperIndex = GetMonitorIndex(wallpaper); int wallpaperIndex = getMonitorIndex(wallpaper);
// qDebug() << monitorIndex << wallpaperIndex; // qDebug() << monitorIndex << wallpaperIndex;
// If the window that has WS_MAXIMIZE is at the same monitor as this wallpaper // 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 "basewindow.h"
#include "windowsdesktopproperties.h" #include "windowsdesktopproperties.h"
class WinWindow : public BaseWindow { class WinWindow : public BaseWindow {
Q_OBJECT Q_OBJECT
@ -73,6 +71,14 @@ private:
void setupWallpaperForMultipleScreens(const QVector<int>& activeScreensList); void setupWallpaperForMultipleScreens(const QVector<int>& activeScreensList);
void setupWindowMouseHook(); void setupWindowMouseHook();
bool searchWorkerWindowToParentTo(); 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: private slots:
void checkForFullScreenWindow(); void checkForFullScreenWindow();
@ -85,4 +91,5 @@ private:
HWND m_windowHandleWorker; HWND m_windowHandleWorker;
QTimer m_checkForFullScreenWindowTimer; QTimer m_checkForFullScreenWindowTimer;
WindowsDesktopProperties m_windowsDesktopProperties; WindowsDesktopProperties m_windowsDesktopProperties;
}; };