使用Python keyboard模块输入TI-Basic的→符号报错求助
问题描述
我正在编写Python脚本生成TI-Basic程序,TI-Basic的变量赋值语法是value → varname。因为TI-Connect不支持复制粘贴,且目标程序约850行无法手动输入,所以用keyboard模块模拟按键输入。运行时触发如下错误:
Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "c:\Users\USERNAME\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy\__main__.py", line 39, in <module> cli.main() File "c:\Users\USERNAME\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main run() File "c:\Users\USERNAME\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file runpy.run_path(target, run_name="__main__") File "c:\Users\USERNAME\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path return _run_module_code(code, init_globals, run_name, File "c:\Users\USERNAME\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code _run_code(code, mod_globals, init_globals, File "c:\Users\USERNAME\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code exec(code, run_globals) File "c:\Users\USERNAME\Documents\Python\jokes\jokes.py", line 33, in <module> keyboard.press_and_release(f'shift+{char.lower()}') File "C:\Users\USERNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keyboard\__init__.py", line 379, in send parsed = parse_hotkey(hotkey) File "C:\Users\USERNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keyboard\__init__.py", line 358, in parse_hotkey steps.append(tuple(key_to_scan_codes(key) for key in keys)) File "C:\Users\USERNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keyboard\__init__.py", line 358, in <genexpr> steps.append(tuple(key_to_scan_codes(key) for key in keys)) File "C:\Users\USERNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keyboard\__init__.py", line 324, in key_to_scan_codes raise ValueError('Key {} is not mapped to any known key.'.format(repr(key)), e) ValueError: ("Key '→' is not mapped to any known key.", ValueError("Key name '→' is not mapped to any known key."))
我的代码如下:
import sys sys.stdout = open('C:\\Users\\USERNAME\\Documents\\Python\\jokes\\program.txt', 'w', encoding='utf-8') with open('C:\\Users\\USERNAME\\Documents\\Python\\jokes\\jokes.txt', encoding='utf-8') as jokesfile: jokes = jokesfile.readlines() print(f'Disp "Press enter to hear a joke."\nPause\nrandInt(1,{len(jokes)})→X\nClrHome') i = 0 for joke in jokes: joke, answer = joke.split('<>') delay = 2 print(f'If X = {i}\nThen\nOutput(0,0,"{joke}")\nWait {delay}\nOutput(5,0,"{answer}")\n') print('Pause') import keyboard import logging logging.log(1, 'Press enter to paste file.') with open('C:\\Users\\USERNAME\\Documents\\Python\\jokes\\program.txt', encoding='utf-8') as programfile: chars = programfile.read() while not keyboard.is_pressed('enter'): pass while keyboard.is_pressed('enter'): pass for char in chars: # loop through all characters in the program if char == '\n': # if it's a newline, manually add it. keyboard.press_and_release('enter') if char.upper() == char and char not in ' \n\t,0123456789/*-+[]\;\',./`': # if it's a capital letter keyboard.press_and_release(f'shift+{char.lower()}') else: # normal character keyboard.press_and_release(char)
解决方案
错误原因是keyboard模块无法识别→这个特殊Unicode字符,需要单独处理该字符的按键模拟:
在TI-Connect的PC编辑器中,输入→的快捷键是shift+period(也可直接用shift+>)。修改循环中的字符处理逻辑,增加对→的判断:
for char in chars: # loop through all characters in the program if char == '\n': # if it's a newline, manually add it. keyboard.press_and_release('enter') elif char == '→': # 单独处理赋值箭头 keyboard.press_and_release('shift+period') elif char.upper() == char and char not in ' \n\t,0123456789/*-+[]\;\',./`': # if it's a capital letter keyboard.press_and_release(f'shift+{char.lower()}') else: # normal character keyboard.press_and_release(char)
如果shift+period不生效,可替换为shift+>:
keyboard.press_and_release('shift+>')
内容的提问来源于stack exchange,提问作者dimani128
相关产品推荐
相关产品推荐

