-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyListener.cpp
More file actions
139 lines (113 loc) · 2.92 KB
/
KeyListener.cpp
File metadata and controls
139 lines (113 loc) · 2.92 KB
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
#include "KeyListener.h"
#include "Timer.h"
#include "var.hpp"
#include <thread>
#include <math.h>
KeyListener* KeyListener::_instance = nullptr;
KeyListener* KeyListener::Instance()
{
if (!_instance) {
_instance = new KeyListener();
}
return _instance;
}
void KeyListener::updateKeyDown(){
_lastKeyDown = clock();
if (_mode == Mode::OFF) {
Timer::Instance()->callUpdateFunc();
}
else {
Timer::Instance()->updateFunc( (1+rand() % (colors.size() - 1)) * color_wait);
}
}
float KeyListener::getLuminance()
{
Timer::Instance()->fractional;
auto now = clock();
switch (_mode)
{
case Mode::ON:
if (now - _lastKeyDown >= sleepTime){
_lastKeyDown = now - sleepTime;
setMode(Mode::Closeing);
}
return 1.0f;
case Mode::Closeing:
if (now - _lastKeyDown > sleepTime + closeingTime) {
setMode(Mode::OFF);
return 0.0f;
}
else if (now - _lastKeyDown < sleepTime) {
setMode(Mode::ON);
return 1.0f;
}
return 1.0f - float(now - _lastKeyDown - sleepTime) / closeingTime;
case Mode::OFF:
if (now - _lastKeyDown < sleepTime) {
setMode(Mode::ON);
return 1.0f;
}
return 0.0f;
}
if (now - _lastKeyDown < sleepTime) {
return 1.0f;
}
else if (now - _lastKeyDown > sleepTime + closeingTime) {
return 0.0f;
}
else {
return float(now - _lastKeyDown - sleepTime) / closeingTime;
}
}
// 全局钩子回调函数
LRESULT CALLBACK KeyboardHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
// 判断钩子是否允许处理键盘按键消息
if (nCode >= 0)
{
if (wParam == WM_KEYDOWN)
{
KeyListener::Instance()->updateKeyDown();
}
}
// 将消息传递给下一个钩子
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
void KeyListener::setMode(Mode mode){
_mode = mode;
switch (mode)
{
case Mode::ON:
Timer::Instance()->fractional = 1;
Timer::Instance()->sleepTime = 500;
break;
case Mode::Closeing:
Timer::Instance()->fractional = 1;
Timer::Instance()->sleepTime = 10;
break;
case Mode::OFF:
Timer::Instance()->fractional = 512;
Timer::Instance()->sleepTime = 100;
break;
}
}
void windowsEnentTmain() {
HHOOK _keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookCallback, NULL, 0);
if (_keyboardHook == NULL)
{
throw "无法安装Windows钩子";
}
MSG message;
while (GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
UnhookWindowsHookEx(_keyboardHook);
}
KeyListener::KeyListener() {
windowsEventT = std::thread(windowsEnentTmain);
windowsEventT.detach();
}
KeyListener::~KeyListener() {
}