Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
HookWindows.cpp
1#include "HookWindows.h"
2#include "FrmViewer.h"
3#include <QApplication>
4#include <QKeyEvent>
5#include <QLoggingCategory>
6
7static Q_LOGGING_CATEGORY(log, "Client.Hook.Windows")
8
9CHook* CHook::GetHook(QObject *parent)
10{
11 CHookWindows* p = nullptr;
12 if(!p)
13 p = new CHookWindows(parent);
14 return p;
15}
16
17CHookWindows::CHookWindows(QObject *parent)
18 : CHook(parent),
19 m_hKeyboard(nullptr)
20{
21 RegisterKeyboard();
22}
23
24CHookWindows::~CHookWindows()
25{
26 qDebug(log) << "CHookWindows::~CHookWindows()";
27 UnRegisterKeyboard();
28}
29
30// See: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644985(v=vs.85)
31LRESULT CALLBACK CHookWindows::keyboardHookProc(INT code, WPARAM wparam, LPARAM lparam)
32{
33 if (code == HC_ACTION)
34 {
35 KBDLLHOOKSTRUCT* hook = reinterpret_cast<KBDLLHOOKSTRUCT*>(lparam);
36 /*
37 qDebug(log) << "process vkCode:" << hook->vkCode
38 << "scanCode:" << hook->scanCode
39 << "flags:" << hook->flags;//*/
40 int key = 0;
41 Qt::KeyboardModifiers keyMdf = Qt::NoModifier;
42 switch(hook->vkCode)
43 {
44// case VK_TAB:
45// {
46// key = Qt::Key_Tab;
47// break;
48// }
49// case VK_DELETE:
50// {
51// key = Qt::Key_Delete;
52// break;
53// }
54 case VK_LWIN:
55 {
56 key = Qt::Key_Super_L;
57 keyMdf = Qt::MetaModifier;
58 break;
59 }
60 case VK_RWIN:
61 {
62 key = Qt::Key_Super_R;
63 keyMdf = Qt::MetaModifier;
64 break;
65 }
66 /*
67 case VK_MENU:
68 case VK_LMENU:
69 case VK_RMENU:
70 {
71 key = Qt::Key_Menu;
72 keyMdf = Qt::KeyboardModifier::AltModifier;
73 break;
74 }
75 case VK_CONTROL:
76 case VK_LCONTROL:
77 case VK_RCONTROL:
78 {
79 key = Qt::Key_Control;
80 keyMdf = Qt::KeyboardModifier::ControlModifier;
81 break;
82 }
83 case VK_SHIFT:
84 case VK_LSHIFT:
85 case VK_RSHIFT:
86 {
87 key = Qt::Key_Shift;
88 keyMdf = Qt::KeyboardModifier::ShiftModifier;
89 break;
90 }*/
91 default:
92 break;
93 }
94 if(key) {
95 CFrmViewer* self = qobject_cast<CFrmViewer*>(QApplication::focusWidget());
96 WId foreground_window = reinterpret_cast<WId>(GetForegroundWindow());
97 if (self && self->parentWidget()->window()->winId() == foreground_window)
98 {
99 if(wparam == WM_KEYDOWN || wparam == WM_SYSKEYDOWN)
100 emit self->sigKeyPressEvent(new QKeyEvent(QKeyEvent::KeyPress, key, keyMdf));
101 if(wparam == WM_KEYUP || wparam == WM_SYSKEYUP)
102 emit self->sigKeyReleaseEvent(new QKeyEvent(QKeyEvent::KeyRelease, key, Qt::NoModifier));
103 /*
104 qDebug(log) << "process vkCode:" << hook->vkCode
105 << "scanCode:" << hook->scanCode
106 << "flags:" << hook->flags;//*/
107 return 0;
108 }
109 }
110 }
111
112 return CallNextHookEx(nullptr, code, wparam, lparam);
113}
114
115// See: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowshookexw
116int CHookWindows::RegisterKeyboard()
117{
118 if(m_hKeyboard)
119 UnRegisterKeyboard();
120 m_hKeyboard = SetWindowsHookExW(WH_KEYBOARD_LL, keyboardHookProc, nullptr, 0);
121 return 0;
122}
123
124int CHookWindows::UnRegisterKeyboard()
125{
126 if(m_hKeyboard)
127 {
128 UnhookWindowsHookEx(m_hKeyboard);
129 m_hKeyboard = nullptr;
130 }
131 return 0;
132}
A widget which displays output image from a CConnectDesktop and sends input keypresses and mouse acti...
Definition FrmViewer.h:49
The class is the HOOK abstract class.
Definition Hook.h:14