I am making a program that writes a program in TI-Basic. In TI-Basic, to set a variable, the syntax is value → varname
. The TI-Connect software doesn’t like it when I copy-paste, so I’m using the keyboard
module to simulate keypresses. I can’t manually type the program as it is about 850 lines long. When I try to run the program, I get the following error:
Traceback (most recent call last): File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0librunpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0librunpy.py", line 86, in _run_code exec(code, run_globals) File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy__main__.py", line 39, in <module> cli.main() File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy/..debugpyservercli.py", line 430, in main run() File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy/..debugpyservercli.py", line 284, in run_file runpy.run_path(target, run_name="__main__") File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 321, in run_path return _run_module_code(code, init_globals, run_name, File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 135, in _run_module_code _run_code(code, mod_globals, init_globals, File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 124, in _run_code exec(code, run_globals) File "c:UsersUSERNAMEDocumentsPythonjokesjokes.py", line 33, in <module> keyboard.press_and_release(f'shift+{char.lower()}') File "C:UsersUSERNAMEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packageskeyboard__init__.py", line 379, in send parsed = parse_hotkey(hotkey) File "C:UsersUSERNAMEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packageskeyboard__init__.py", line 358, in parse_hotkey steps.append(tuple(key_to_scan_codes(key) for key in keys)) File "C:UsersUSERNAMEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packageskeyboard__init__.py", line 358, in <genexpr> steps.append(tuple(key_to_scan_codes(key) for key in keys)) File "C:UsersUSERNAMEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packageskeyboard__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."))
My code:
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."nPausenrandInt(1,{len(jokes)})→XnClrHome') i = 0 for joke in jokes: joke, answer = joke.split('<>') delay = 2 print(f'If X = {i}nThennOutput(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 ' nt,0123456789/*-+[];',./`': # if it's a capital letter keyboard.press_and_release(f'shift+{char.lower()}') else: # normal character keyboard.press_and_release(char)
I can’t figure out how to type this key.
Advertisement
Answer
The best solution I found is to use a different python keyboard library, pynput
. It has native support for typing unicode characters. Here is some sample code:
import pynput keyboard = pynput.keyboard.Controller() keyboard.type("→")