Skip to content

Commit

Permalink
初步实现Linux下的 key_translator
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Jun 6, 2024
1 parent 1beb573 commit fd863aa
Showing 1 changed file with 10 additions and 41 deletions.
51 changes: 10 additions & 41 deletions UmiOCR-data/py_src/platform/linux/key_translator.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
# 负责 pynput 的按键转换
# 封装 keyTranslator ,负责key、char、vk的转换

'''
from pynput._util.win32 import KeyTranslator # win32库,转为linux
# ==================== 按键转换器 ====================
# 封装 keyTranslator ,负责key、char、vk的转换
class _KeyTranslatorApi:
def __init__(self):
self._kt = KeyTranslator()
self._layout, _layoutData = self._kt._generate_layout()
self._normalLayout = _layoutData[(False, False, False)] # 选取常规布局,不受修饰键影响
def __call__(self, key):
"""传入pynput的Key对象,返回与修饰键无关的键名char"""
# 比如,就算按下Shift再按“=”,依然返回“=”而不是“+”
try:
if hasattr(key, "name"): # 若为控制键
name = key.name.replace("cmd", "win") # win键名称修正
if "_" in name: # 清除 _l _r 标记后缀
name = name.split("_", 1)[0].lower()
return name.lower()
else: # 若为非控制键,通过vk获取键名,避免组合键的char为空
scan = self._kt._to_scan(key.vk, self._layout) # vk转扫描码
char = self._normalLayout[scan][0] # 扫描码转char
return char.lower()
except Exception as e: # 特殊键(如Fn)没有对应字符,会跳到这里
if key and hasattr(key, "vk"):
return f"<{key.vk}>" # 未知键值,无对应字符,返回键值本身
else:
print(f"[Error] 键值转换异常,未知键值!{str(key)} {type(key)}")
return str(key)
_KTA = _KeyTranslatorApi()
'''


def getKeyName(key): # TODO: 键值转键名
# 传入 pynput 按键事件对象,返回键名字符串
# k = _KTA(key)
k = str(key)
print("获取键名:", k)
return k
if not isinstance(key, str):
key = str(key)
if not key: # Error
return ""
if key.startswith("Key."): # 修饰建
key = key[4:]
if key.startswith("'") and key.endswith("'"):
key = key[1:-1]
# print(f"获取键名:【{key}】")
return key

0 comments on commit fd863aa

Please sign in to comment.