2022-10-07 21:27:27 +08:00
|
|
|
|
#!/usr/bin/env python3
|
2022-10-14 22:11:05 +08:00
|
|
|
|
#########################
|
|
|
|
|
# 版本:1.0.0
|
|
|
|
|
# Python
|
|
|
|
|
#########################
|
2022-10-07 21:27:27 +08:00
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import time
|
|
|
|
|
import json
|
|
|
|
|
import threading
|
|
|
|
|
import traceback
|
|
|
|
|
import pynput.keyboard as keyboard
|
|
|
|
|
keyList = []
|
2022-10-07 21:42:03 +08:00
|
|
|
|
programPath = os.path.split(os.path.realpath(__file__))[0] # 返回 string
|
2022-10-07 21:27:27 +08:00
|
|
|
|
keyChangeMap = [
|
|
|
|
|
["ctrl", keyboard.Key.ctrl],
|
|
|
|
|
["alt", keyboard.Key.alt],
|
|
|
|
|
["esc", keyboard.Key.esc],
|
2022-10-07 21:42:03 +08:00
|
|
|
|
["enter", keyboard.Key.enter]
|
2022-10-07 21:27:27 +08:00
|
|
|
|
]
|
2022-10-08 21:54:02 +08:00
|
|
|
|
keyMap = []
|
2022-10-11 21:28:26 +08:00
|
|
|
|
for i in os.listdir(f"{programPath}/list"):
|
|
|
|
|
print(i)
|
2022-10-08 21:54:02 +08:00
|
|
|
|
try:
|
2022-10-11 21:28:26 +08:00
|
|
|
|
file = open(f"{programPath}/list/{i}", "r")
|
2022-10-08 21:54:02 +08:00
|
|
|
|
keyMapTemp = json.loads(file.read())
|
|
|
|
|
except:
|
2022-10-11 21:28:26 +08:00
|
|
|
|
print(f"{programPath}/list/{i} 读取失败!")
|
2022-10-08 21:54:02 +08:00
|
|
|
|
continue
|
|
|
|
|
for i in keyMapTemp:
|
|
|
|
|
keyMap.append(i)
|
2022-10-07 21:27:27 +08:00
|
|
|
|
for i in range(len(keyMap)):
|
|
|
|
|
for k in range(len(keyMap[i])):
|
|
|
|
|
for j in keyChangeMap:
|
|
|
|
|
if keyMap[i][k] == j[0]:
|
|
|
|
|
keyMap[i][k] = j[1]
|
2022-10-07 21:42:03 +08:00
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
keyMap[i][k] = keyMap[i][k].replace("{programPath}", programPath)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2022-10-07 21:27:27 +08:00
|
|
|
|
|
2022-10-11 21:28:26 +08:00
|
|
|
|
print(keyList)
|
2022-10-07 21:27:27 +08:00
|
|
|
|
def on_press(key):
|
|
|
|
|
try:
|
|
|
|
|
if key.char in keyList:
|
|
|
|
|
# 重复的值就不认了,摊牌了
|
|
|
|
|
return
|
|
|
|
|
keyList.append(key.char)
|
|
|
|
|
print(f'alphanumeric key {key.char} pressed')
|
|
|
|
|
except AttributeError:
|
|
|
|
|
keyList.append(key)
|
|
|
|
|
print(f'special key {key} pressed')
|
|
|
|
|
|
|
|
|
|
def on_release(key):
|
|
|
|
|
print(f'{key} released')
|
|
|
|
|
try:
|
|
|
|
|
del keyList[keyList.index(key.char)]
|
|
|
|
|
except AttributeError:
|
|
|
|
|
del keyList[keyList.index(key)]
|
|
|
|
|
except:
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
def ReadKey():
|
|
|
|
|
next = False
|
|
|
|
|
for i in keyMap:
|
|
|
|
|
for k in range(0, len(i) - 1):
|
|
|
|
|
k = i[k]
|
|
|
|
|
if not k in keyList:
|
|
|
|
|
next = True
|
|
|
|
|
break
|
|
|
|
|
if not next:
|
|
|
|
|
# 执行命令
|
|
|
|
|
os.system(i[-1])
|
|
|
|
|
# 必须等待按键全部松开才行
|
|
|
|
|
while len(keyList) != 0:
|
|
|
|
|
time.sleep(0.01)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def Read():
|
|
|
|
|
while True:
|
|
|
|
|
ReadKey()
|
2022-10-08 21:54:02 +08:00
|
|
|
|
if os.path.exists("/tmp/deepin-wine-runner-keyboard-exit"):
|
|
|
|
|
# 移除文件
|
|
|
|
|
try:
|
|
|
|
|
os.remove("/tmp/deepin-wine-runner-keyboard-exit")
|
|
|
|
|
except:
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
listener.stop()
|
|
|
|
|
break
|
2022-10-07 21:27:27 +08:00
|
|
|
|
time.sleep(0.01)
|
|
|
|
|
|
|
|
|
|
# Lock 锁防止多次调用
|
2022-10-24 22:11:03 +08:00
|
|
|
|
#if os.path.exists("/tmp/deepin-wine-runner-keyboard-lock"):
|
|
|
|
|
# print("不可多次调用")
|
|
|
|
|
# print("锁 /tmp/deepin-wine-runner-keyboard-lock 已存在")
|
|
|
|
|
# sys.exit(1)
|
|
|
|
|
#os.mknod("/tmp/deepin-wine-runner-keyboard-lock")
|
2022-10-07 21:27:27 +08:00
|
|
|
|
threading.Thread(target=Read).start()
|
|
|
|
|
with keyboard.Listener(
|
|
|
|
|
on_press=on_press,
|
|
|
|
|
on_release=on_release) as listener:
|
|
|
|
|
listener.join()
|
2022-10-14 22:11:05 +08:00
|
|
|
|
os.remove("/tmp/deepin-wine-runner-keyboard-lock")
|