-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.py
More file actions
339 lines (286 loc) · 11.6 KB
/
window.py
File metadata and controls
339 lines (286 loc) · 11.6 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QLabel, QApplication
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPainter, QColor, QBrush, QCursor
from pynput import keyboard
import time
from threading import Thread
from collections import deque
class KeyboardMonitor:
"""键盘监控类"""
def __init__(self):
self.key_times = deque(maxlen=100) # 记录按键时间
self.frequency = 0 # 当前频率
self.listener = None
self.running = False
def start(self):
"""启动键盘监听"""
self.running = True
def on_press(key):
if self.running:
self.key_times.append(time.time())
return True
# 在独立线程中运行监听器
self.listener_thread = Thread(
target=self._run_listener,
args=(on_press,),
daemon=True
)
self.listener_thread.start()
# 启动定时器计算频率
self.timer = QTimer()
self.timer.timeout.connect(self.update_frequency)
self.timer.start(100) # 每100ms更新一次频率
def _run_listener(self, on_press):
"""运行键盘监听器"""
with keyboard.Listener(on_press=on_press) as listener:
self.listener = listener
listener.join()
def update_frequency(self):
"""更新按键频率"""
if not self.running:
return
current_time = time.time()
cutoff = current_time - 1.0 # 最近1秒
# 计算频率
count = 0
for t in reversed(self.key_times):
if t > cutoff:
count += 1
else:
break
self.frequency = count
def stop(self):
"""停止监听"""
self.running = False
if self.listener:
self.listener.stop()
class DotWidget(QWidget):
"""单个圆点霓虹灯组件"""
def __init__(self, parent=None, radius=6):
super().__init__(parent=parent)
self.radius = radius
self.current_color = QColor("#333333") # 默认灰色
self.target_color = self.current_color
self.setFixedSize(radius * 2 + 4, radius * 2 + 4)
def set_color(self, color, animate=True):
"""设置圆点颜色"""
self.target_color = QColor(color)
if not animate or self.current_color == self.target_color:
self.current_color = self.target_color
self.update()
else:
# 平滑过渡
self.current_color = self.target_color
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
center_x = self.width() // 2
center_y = self.height() // 2
# 绘制发光效果
glow_color = QColor(self.current_color)
glow_color.setAlpha(100)
painter.setBrush(QBrush(glow_color))
painter.setPen(Qt.NoPen)
painter.drawEllipse(int(center_x - self.radius*1.5),
int(center_y - self.radius*1.5),
int(self.radius*3), int(self.radius*3))
# 绘制主圆点
painter.setBrush(QBrush(self.current_color))
painter.drawEllipse(center_x - self.radius,
center_y - self.radius,
self.radius*2, self.radius*2)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
screen = QApplication.primaryScreen().availableGeometry()
self.screen_width = screen.width()
self.screen_height = screen.height()
# 窗口设置
self.narrow_width = 15
self.wide_width = 60
self.w_height = 40
self.current_width = self.narrow_width
# 颜色配置
self.color_states = [
"#333333", # 灰色(无输入)
"#004400", # 深绿
"#008800", # 中绿
"#00CC00", # 亮绿
"#44FF00", # 绿黄
"#88FF00", # 黄绿
"#CCFF00", # 亮黄绿
"#FFFF00", # 黄色
"#FFCC00", # 橙黄
"#FF9900", # 橙色
"#FF6600", # 橙红
"#FF3300", # 红橙
"#FF0000", # 红色
"#FF0066", # 品红(最高频率)
]
self.gray_color = self.color_states[0] # 灰色(无输入状态)
# 键盘监控
self.key_monitor = KeyboardMonitor()
# 动画状态
self.current_level = 0 # 0表示灰色
self.target_level = 0
self.setup_window_flags()
self.init_ui()
self.init_timer()
self.start_keyboard_monitor()
def setup_window_flags(self):
"""设置窗口属性"""
self.setWindowFlags(
Qt.FramelessWindowHint | # 无边框
Qt.WindowStaysOnTopHint | # 置顶
Qt.Tool # 防止出现在ctrl+tab中
)
self.setAttribute(Qt.WA_TranslucentBackground)
def init_ui(self):
"""初始化用户界面"""
x = self.screen_width - self.current_width
y = self.screen_height - 5 * self.w_height
self.setGeometry(x, y, self.current_width, self.w_height)
# 创建中央部件
central_widget = QWidget()
central_widget.setStyleSheet("""
QWidget {
background-color: rgba(0, 0, 0, 0);
border: none;
}
""")
self.setCentralWidget(central_widget)
# 创建三个霓虹灯圆点
layout = QVBoxLayout(central_widget)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(5)
# 初始显示灰色
self.dots = [
DotWidget(central_widget, 8), # 上
DotWidget(central_widget, 8), # 中
DotWidget(central_widget, 8) # 下
]
for dot in self.dots:
dot.set_color(self.gray_color)
layout.addWidget(dot, 0, Qt.AlignCenter)
layout.addStretch()
# 创建自定义提示标签
self.custom_tooltip = QLabel("点击并按下ESC退出", self)
self.custom_tooltip.setAlignment(Qt.AlignCenter)
self.custom_tooltip.setStyleSheet("""
QLabel {
color: white;
background-color: rgba(50, 50, 50, 200);
border-radius: 5px;
padding: 5px 10px;
font-size: 12px;
}
""")
self.custom_tooltip.hide()
self.custom_tooltip.setWindowFlags(
Qt.ToolTip |
Qt.FramelessWindowHint |
Qt.WindowStaysOnTopHint
)
self.custom_tooltip.setAttribute(Qt.WA_ShowWithoutActivating)
def start_keyboard_monitor(self):
"""启动键盘监控"""
self.key_monitor.start()
def init_timer(self):
"""初始化定时器"""
self.timer = QTimer()
self.timer.timeout.connect(self.update_animation)
self.timer.start(50) # 20fps动画
def update_animation(self):
"""更新动画"""
frequency = self.key_monitor.frequency
# 计算目标等级
if frequency == 0:
# 无输入:目标等级为0,当前等级下降
target_level = 0
if self.current_level > 0:
self.current_level = max(0, self.current_level - 0.05)
else:
# 有输入:计算目标等级
target_level = min(frequency // 3, 6)
# 更新当前等级
if target_level > self.current_level:
# 频率增加:立即跳到目标等级
self.current_level = target_level
elif target_level < self.current_level:
# 频率减少:缓慢下降到目标等级
self.current_level = max(target_level, self.current_level - 0.05)
# 获取整数等级
level = int(self.current_level)
# 优雅的颜色映射配置
color_mapping = {
0: [0, 0, 0], # 灰色,全部使用索引0
1: [3, 2, 1], # 绿-黄绿-深绿
2: [6, 5, 4], # 亮黄绿-黄绿-绿黄
3: [9, 8, 7], # 橙-橙黄-黄
4: [12, 11, 10], # 品红-红-红橙
5: [13, 13, 13], # 最高频率:全部品红
}
# 获取对应等级的颜色索引
color_indices = color_mapping.get(level, color_mapping[4])
# 应用颜色
for i, dot in enumerate(self.dots):
color_index = color_indices[i] if i < len(color_indices) else color_indices[-1]
# 确保索引不越界
safe_index = min(color_index, len(self.color_states) - 1)
dot.set_color(self.color_states[safe_index])
def keyPressEvent(self, event):
"""处理键盘按下事件"""
if event.key() == Qt.Key_Escape:
self.close()
def closeEvent(self, event):
"""关闭事件"""
print("退出中...")
if hasattr(self, 'timer'):
self.timer.stop()
if hasattr(self, 'key_monitor'):
self.key_monitor.stop()
if hasattr(self, 'dots'):
for dot in self.dots:
dot.setParent(None) # 断开父子关系
dot.deleteLater() # 延迟删除
self.dots.clear()
self.hide()
event.accept()
QTimer.singleShot(100, QApplication.quit)
def mousePressEvent(self, event):
"""鼠标按下事件"""
if event.button() == Qt.LeftButton:
self.drag_position = event.globalPos() - self.frameGeometry().topLeft()
event.accept()
def mouseMoveEvent(self, event):
"""鼠标移动事件"""
if event.buttons() == Qt.LeftButton:
if hasattr(self, 'drag_position'):
self.move(event.globalPos() - self.drag_position)
event.accept()
def enterEvent(self, event):
"""鼠标进入"""
self.current_width = self.wide_width
x = self.screen_width - self.current_width
self.setGeometry(x, self.y(), self.current_width, self.w_height)
# 获取鼠标的全局坐标
mouse_pos = QCursor.pos()
# 计算提示窗口位置(鼠标右下角偏移)
tooltip_x = mouse_pos.x() + 15 # 向右偏移15px
tooltip_y = mouse_pos.y() + 15 # 向下偏移15px
screen_rect = QApplication.primaryScreen().availableGeometry()
if tooltip_x + self.custom_tooltip.width() > screen_rect.right():
tooltip_x = mouse_pos.x() - self.custom_tooltip.width() - 15
# 如果提示框超出屏幕底部,显示在鼠标上方
if tooltip_y + self.custom_tooltip.height() > screen_rect.bottom():
tooltip_y = mouse_pos.y() - self.custom_tooltip.height() - 15
self.custom_tooltip.move(tooltip_x, tooltip_y)
self.custom_tooltip.show()
QTimer.singleShot(3000, self.custom_tooltip.hide)
def leaveEvent(self, event):
"""鼠标离开 - 变窄"""
self.current_width = self.narrow_width
x = self.screen_width - self.current_width
self.setGeometry(x, self.y(), self.current_width, self.w_height)
self.custom_tooltip.hide()