This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Hotkey_capture.py
143 lines (120 loc) · 3.89 KB
/
Hotkey_capture.py
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
import json
import keyboard
import time
from Toast import show_toast
from MQTT import Send_MQTT_Discovery,Publish_MQTT_Message
listening = False
def send_discovery(hotkeys):
info = "快捷键发现: \n"
for hotkey in hotkeys:
name_id = "hotkey" + hotkey.replace("+", "-")
Send_MQTT_Discovery(name=hotkey,name_id=name_id,type="binary_sensor")
info = info + hotkey
time.sleep(0.5)
init_binary_sensor(hotkeys)
return info
def init_binary_sensor(hotkeys):
for hotkey in hotkeys:
topic = "homeassistant/binary_sensor/" + device_name + "hotkey" + hotkey.replace("+", "-") + "/state"
Publish_MQTT_Message(topic,"OFF")
22
# 初始化
def init_data():
with open('config.json', 'r') as file:
global json_data
global hotkey_notify
global suppress
global device_name
json_data = json.load(file)
hotkey_notify = json_data.get("hotkey_notify")
suppress = json_data.get("suppress")
device_name = json_data.get("device_name")
load_hotkeys()
def save_hotkey(hotkey):
existing_hotkeys = load_hotkeys()
if hotkey not in existing_hotkeys:
with open('hotkeys.txt', 'a') as file:
file.write(hotkey + '\n')
print(f"保存快捷键: {hotkey}")
else:
print(f"快捷键 '{hotkey}' 已存在,未保存。")
def capture_hotkeys():
print("开始捕获快捷键,按 'esc' 停止捕获...")
captured_hotkeys = []
def record_hotkey():
hotkey = keyboard.read_event().name
if hotkey != 'esc':
captured_hotkeys.append(hotkey)
print(f"捕获到快捷键: {captured_hotkeys}")
while True:
event = keyboard.read_event()
if event.event_type == keyboard.KEY_DOWN:
if event.name == 'esc':
break
record_hotkey()
hotkey_join = '+'.join(captured_hotkeys)
save_hotkey(hotkey_join)
return hotkey_join
def load_hotkeys():
try:
with open('hotkeys.txt', 'r') as file:
global hotkeys
hotkeys = [line.strip() for line in file.readlines()]
return hotkeys
except FileNotFoundError:
return []
def command(h):
key_list = h.split('+')
for item in key_list:
keyboard.release(item)
print(item)
print("触发了快捷键:", h)
if hotkey_notify == True:
show_toast("PCTools" ,"触发了快捷键:" + h)
topic = "homeassistant/binary_sensor/" + device_name + "hotkey" + h.replace("+", "-") + "/state"
Publish_MQTT_Message(topic,"ON")
time.sleep(1)
Publish_MQTT_Message(topic,"OFF")
def listen_hotkeys():
global listening
if listening == False:
listening = True
init_data()
send_discovery(hotkeys)
print("开始监听快捷键...", hotkeys)
for hotkey in hotkeys:
keyboard.add_hotkey(hotkey, lambda h=hotkey: command(h),suppress=suppress,trigger_on_release=False)
# keyboard.wait('esc')
return 0
return 1
def stop_listen():
global listening
if listening == True:
listening = False
for hotkey in hotkeys:
keyboard.remove_hotkey(hotkey)
print("已停止监听快捷键")
return 0
return 1
def menu():
while True:
print("按键监听:", listening, "请选择")
print("1. 捕获快捷键并保存")
print("2. 开启监听")
print("3. 发送Discovery MQTT")
print("4. 退出")
choice = input("请输入选项: ")
if choice == '1':
capture_hotkeys()
elif choice == '2':
load_hotkeys()
listen_hotkeys()
elif choice == '3':
init_data()
print(send_discovery(hotkeys))
elif choice == '4':
break
else:
print("无效选项,请重试。")
if __name__ == '__main__':
menu()