forked from xiaoyifang/Capture2Text
-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeyboardHook.cpp
130 lines (108 loc) · 3.76 KB
/
KeyboardHook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Copyright (C) 2010-2017 Christopher Brochtrup
This file is part of Capture2Text.
Capture2Text is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Capture2Text is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Capture2Text. If not, see <http://www.gnu.org/licenses/>.
*/
#include "KeyboardHook.h"
void KeyboardHook::run()
{
if(hHook == nullptr)
{
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, nullptr, 0);
if (hHook == nullptr)
{
qDebug() << "Keyboard Hook Failed!";
return;
}
}
QEventLoop eventLoop;
eventLoop.exec();
}
void KeyboardHook::addHotkey(int id, Hotkey hotkey)
{
if(hotkeys.contains(id))
{
if(hotkey.getVkCode() == 0)
{
hotkeys.remove(id);
}
else
{
hotkeys[id] = hotkey;
}
}
else
{
hotkeys.insert(id, hotkey);
}
}
void KeyboardHook::removeHotkey(int id)
{
if(hotkeys.contains(id))
{
hotkeys.remove(id);
}
}
void KeyboardHook::endThread()
{
if(hHook != nullptr)
{
UnhookWindowsHookEx(hHook);
hHook = nullptr;
}
exit(0);
}
LRESULT CALLBACK KeyboardHook::hookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode < 0)
{
return CallNextHookEx(KeyboardHook::getInstance().getHHook(), nCode, wParam, lParam);
}
KBDLLHOOKSTRUCT kbData = *((KBDLLHOOKSTRUCT*)lParam);
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
{
bool modCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
bool modShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
bool modAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
bool modWin = (GetKeyState(VK_LWIN) & 0x8000) != 0 || (GetKeyState(VK_RWIN) & 0x8000) != 0;
//qDebug() << "Key Pressed: " << kbData.vkCode;
for(auto id : KeyboardHook::getInstance().getHotkeys().keys())
{
Hotkey key = KeyboardHook::getInstance().getHotkeys()[id];
if(key.getVkCode() == kbData.vkCode
&& (key.getModCtrl() == modCtrl)
&& (key.getModShift() == modShift)
&& (key.getModAlt() == modAlt)
&& (key.getModWin() == modWin))
{
// qDebug() << "Hotkey ID: " << id;
emit KeyboardHook::getInstance().keyPressed(id);
// Suppress Start Menu and Alt Menu by sending a Ctrl up/down keypress.
// So WinKey becomes WinKey+Ctrl and Alt becomes Alt+Ctrl.
if(!modCtrl && !modShift
&& ((modWin && !modAlt) || (modAlt && !modWin)))
{
int numInputs = 1;
INPUT input;
memset(&input, 0, sizeof(INPUT));
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_CONTROL;
SendInput(numInputs, &input, sizeof(INPUT)); // Ctrl down
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(numInputs, &input, sizeof(INPUT)); // Ctrl up
}
return 1;
}
}
}
return CallNextHookEx(KeyboardHook::getInstance().getHHook(), nCode, wParam, lParam);
}