-
Notifications
You must be signed in to change notification settings - Fork 0
/
wakerdialog.cpp
226 lines (174 loc) · 5.22 KB
/
wakerdialog.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "wakerdialog.h"
#include "ui_wakerdialog.h"
WakerDialog * wdRef;
QTime lastInputTime;
QTime currentTime;
#define IDLE_DURATION 100 // sec. - 해당 초만큼 입력이 없었으면, VK_xxx 전송
#define TIMER_DURATION (2*60*1000) // msec. - 2분 마다 체크.
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
qDebug() << "LowLevelKeyboardProc() / HC_ACTION";
switch (wParam)
{
// Pass KeyDown/KeyUp messages for Qt class to logicize
case WM_KEYDOWN:
wdRef->keyDown(PKBDLLHOOKSTRUCT(lParam)->vkCode);
break;
/*
case WM_KEYUP:
wdRef->keyUp(PKBDLLHOOKSTRUCT(lParam)->vkCode);
break;
*/
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
WakerDialog::WakerDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::WakerDialog)
{
ui->setupUi(this);
wdRef = this;
hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL /*WH_MOUSE_LL*/, LowLevelKeyboardProc, 0, 0);
initilize();
createActions();
createTrayIcon();
trayIcon->show();
// createTimer(); // minimize 구간으로 이동
}
WakerDialog::~WakerDialog()
{
delete ui;
}
void WakerDialog::keyDown(DWORD key)
{
qDebug() << "key down:" << key;
lastInputTime = QTime::currentTime();
}
void WakerDialog::pressKey(DWORD vkKeyCode)
{
qDebug() << " WakerDialog::pressKey() ";
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));
Input.ki.dwFlags = KEYEVENTF_KEYUP; //0;
SendInput(1, &Input, sizeof(INPUT));
}
void WakerDialog::OnTimerCallBack()
{
currentTime = QTime::currentTime();
qDebug() << "OnTimerCallBack() / current time:" << currentTime;
qDebug() << "last input time:" << lastInputTime << "\n diff:" << currentTime.secsTo(lastInputTime)
<< " / diff:" << lastInputTime.secsTo(currentTime) << " / " << sbUserInputInterval->value();
if(lastInputTime.secsTo(currentTime) > sbUserInputInterval->value())
{
pressKey(VK_SHIFT);
}
}
void WakerDialog::timeKeeperDefaultCheckSlot(bool checked)
{
qDebug() << "timeKeeperDefaultCheckSlot:" << checked;
if(checked)
{
this->sbTimeKeeperInterval->setValue(120);
this->sbTimeKeeperInterval->setDisabled(true);
}
else
{
this->sbTimeKeeperInterval->setEnabled(true);
}
}
void WakerDialog::userInputDefaultCheckSlot(bool checked)
{
qDebug() << "userInputDefaultCheckSlot:" << checked;
if(checked)
{
this->sbUserInputInterval->setValue(100);
this->sbUserInputInterval->setDisabled(true);
}
else
{
this->sbUserInputInterval->setEnabled(true);
}
}
void WakerDialog::quitSlot()
{
QApplication::quit();
}
void WakerDialog::minimizedSlot()
{
QDialog::close();
}
/* protected */
void WakerDialog::closeEvent(QCloseEvent *evt)
{
qDebug() << "WakerDialog::closeEvent()";
createTimer();
}
/* private */
void WakerDialog::initilize()
{
cbTimeKeeperDefault = ui->cbTimeKeeperDefault;
cbUserInputDefault = ui->cbUserInputDefault;
sbTimeKeeperInterval = ui->sbTimeKeeperInterval;
sbUserInputInterval = ui->sbUserInputInterval;
btnQuit = ui->btnQuit;
btnMinimized = ui->btnMinimized;
if(cbTimeKeeperDefault->isChecked())
{
sbTimeKeeperInterval->setValue(120);
sbTimeKeeperInterval->setDisabled(true);
}
else
{
sbTimeKeeperInterval->setEnabled(true);
}
if(cbUserInputDefault->isChecked())
{
sbUserInputInterval->setValue(100);
sbUserInputInterval->setDisabled(true);
}
else
{
sbUserInputInterval->setEnabled(true);
}
}
void WakerDialog::createActions()
{
titleAction = new QAction(tr("&TimeKeeper Waker"), this);
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
connect(cbTimeKeeperDefault, SIGNAL(toggled(bool)), this, SLOT(timeKeeperDefaultCheckSlot(bool)));
connect(cbUserInputDefault, SIGNAL(toggled(bool)), this, SLOT(userInputDefaultCheckSlot(bool)));
connect(btnQuit, SIGNAL(clicked()), this, SLOT(quitSlot()));
connect(btnMinimized, SIGNAL(clicked()), this, SLOT(minimizedSlot()));
}
void WakerDialog::createTrayIcon()
{
qDebug() << "WakerDialog::createTrayIcon()";
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(titleAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
QIcon icon(":/images/heart.png");
trayIcon->setIcon(icon);
trayIcon->setContextMenu(trayIconMenu);
}
void WakerDialog::createTimer()
{
timer = new QTimer();
lastInputTime = QTime::currentTime();
connect(timer, SIGNAL(timeout()), this, SLOT(OnTimerCallBack()));
// timer->start(TIMER_DURATION);
qDebug() << "sbTimeKeeperInterval->value():" << sbTimeKeeperInterval->value();
timer->start(sbTimeKeeperInterval->value() * 1000);
}