-
Notifications
You must be signed in to change notification settings - Fork 7
/
mainwindow.cpp
115 lines (101 loc) · 2.98 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
// Crosslink between Qt class and win callback
MainWindow * mwReference;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
switch (wParam)
{
// Pass KeyDown/KeyUp messages for Qt class to logicize
case WM_KEYDOWN:
mwReference->keyDown(PKBDLLHOOKSTRUCT(lParam)->vkCode);
break;
case WM_KEYUP:
mwReference->keyUp(PKBDLLHOOKSTRUCT(lParam)->vkCode);
break;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Setup variables
mwReference = this;
bWinKey = false;
// Install the low-level keyboard & mouse hooks
hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0);
}
MainWindow::~MainWindow()
{
// Be friendly! Remove hooks!
UnhookWindowsHookEx(hhkLowLevelKybd);
delete ui;
}
void MainWindow::keyDown(DWORD key)
{
// This section gets launched for every key pressed ever
// Filter out the keys we are interested
// Launch doMultimedia everytime key of interest is pressed
// Otherwise it'll still launch even if non-interested key is pressed
// Also will result in massive trigger spam when right keys are pressed
// By keeping it within the parenthesis, doMultimedia will fire on that specific key repeat
// Otherwise it'll spam on both win and key repeat, meaning it'll fire 30+ a second if outside brackets
if(key == VK_LWIN || key == VK_RWIN)
{
bWinKey = true;
doMultimedia(key);
}
if(key == VK_F9)
doMultimedia(key);
if(key == VK_F10)
doMultimedia(key);
if(key == VK_DOWN)
doMultimedia(key);
if(key == VK_UP)
doMultimedia(key);
if(key == VK_END)
doMultimedia(key);
}
void MainWindow::keyUp(DWORD key)
{
// Remove the bools
if(key == VK_LWIN || key == VK_RWIN)
bWinKey = false;
}
void MainWindow::pressKey(DWORD vkKeyCode)
{
INPUT Input;
// Set up a generic keyboard event.
Input.type = INPUT_KEYBOARD;
Input.ki.wScan = 0;
Input.ki.time = 0;
Input.ki.dwExtraInfo = 0;
Input.ki.dwFlags = 0;
Input.ki.wVk = vkKeyCode;
SendInput(1, &Input, sizeof(INPUT));
}
void MainWindow::doMultimedia(DWORD vkKeyCode)
{
// Check if any win key is being pressed first
if(!bWinKey) return;
// Play / Pause
if (vkKeyCode == VK_F9)
pressKey(VK_MEDIA_PLAY_PAUSE);
// Next Track
else if (vkKeyCode == VK_F10)
pressKey(VK_MEDIA_NEXT_TRACK);
// Lower Volume
else if (vkKeyCode == VK_DOWN)
pressKey(VK_VOLUME_DOWN);
// Increase Volume
else if (vkKeyCode == VK_UP)
pressKey(VK_VOLUME_UP);
// Mute
else if (vkKeyCode == VK_END)
pressKey(VK_VOLUME_MUTE);
}