jiateng_ws/test_keyboard.py

51 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import logging
import time
from utils.serial_manager import SerialManager
from utils.keyboard_listener import KeyboardListener
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
# 自定义回调函数
def my_pageup_callback():
print("\n" + "="*50)
print("PageUp键被按下自定义回调函数被触发")
print("="*50 + "\n")
def main():
print("初始化SerialManager...")
sm = SerialManager()
# 注册自定义回调函数
kl = KeyboardListener()
kl.register_callback('Key.page_up', my_pageup_callback)
print("启动键盘监听器...")
sm.start_keyboard_listener()
print("键盘监听器已启动按PageUp键触发米电阻查询")
print("程序将运行30秒按Ctrl+C可以提前退出")
try:
for i in range(30):
print(f"等待中 {i+1}/30...", flush=True)
time.sleep(1)
except KeyboardInterrupt:
print("\n用户中断,正在退出...")
finally:
print("停止键盘监听器...")
sm.stop_keyboard_listener(join_thread=True)
print("程序结束")
if __name__ == "__main__":
main()