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:
JavaScript
x
29
29
1
Traceback (most recent call last):
2
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0librunpy.py", line 196, in _run_module_as_main
3
return _run_code(code, main_globals, None,
4
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0librunpy.py", line 86, in _run_code
5
exec(code, run_globals)
6
File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy__main__.py", line 39, in <module>
7
cli.main()
8
File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy/..debugpyservercli.py", line 430, in main
9
run()
10
File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy/..debugpyservercli.py", line 284, in run_file
11
runpy.run_path(target, run_name="__main__")
12
File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 321, in run_path
13
return _run_module_code(code, init_globals, run_name,
14
File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 135, in _run_module_code
15
_run_code(code, mod_globals, init_globals,
16
File "c:UsersUSERNAME.vscodeextensionsms-python.python-2022.20.1pythonFileslibpythondebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 124, in _run_code
17
exec(code, run_globals)
18
File "c:UsersUSERNAMEDocumentsPythonjokesjokes.py", line 33, in <module>
19
keyboard.press_and_release(f'shift+{char.lower()}')
20
File "C:UsersUSERNAMEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packageskeyboard__init__.py", line 379, in send
21
parsed = parse_hotkey(hotkey)
22
File "C:UsersUSERNAMEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packageskeyboard__init__.py", line 358, in parse_hotkey
23
steps.append(tuple(key_to_scan_codes(key) for key in keys))
24
File "C:UsersUSERNAMEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packageskeyboard__init__.py", line 358, in <genexpr>
25
steps.append(tuple(key_to_scan_codes(key) for key in keys))
26
File "C:UsersUSERNAMEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packageskeyboard__init__.py", line 324, in key_to_scan_codes
27
raise ValueError('Key {} is not mapped to any known key.'.format(repr(key)), e)
28
ValueError: ("Key '→' is not mapped to any known key.", ValueError("Key name '→' is not mapped to any known key."))
29
My code:
JavaScript
1
36
36
1
import sys
2
sys.stdout = open('C:\Users\USERNAME\Documents\Python\jokes\program.txt', 'w', encoding='utf-8')
3
4
with open('C:\Users\USERNAME\Documents\Python\jokes\jokes.txt', encoding='utf-8') as jokesfile:
5
jokes = jokesfile.readlines()
6
7
print(f'Disp "Press enter to hear a joke."nPausenrandInt(1,{len(jokes)})→XnClrHome')
8
i = 0
9
for joke in jokes:
10
joke, answer = joke.split('<>')
11
delay = 2
12
print(f'If X = {i}nThennOutput(0,0,"{joke}")nWait {delay}nOutput(5,0,"{answer}")n')
13
14
print('Pause')
15
16
import keyboard
17
import logging
18
19
logging.log(1, 'Press enter to paste file.')
20
21
with open('C:\Users\USERNAME\Documents\Python\jokes\program.txt', encoding='utf-8') as programfile:
22
chars = programfile.read()
23
24
while not keyboard.is_pressed('enter'):
25
pass
26
while keyboard.is_pressed('enter'):
27
pass
28
29
for char in chars: # loop through all characters in the program
30
if char == 'n': # if it's a newline, manually add it.
31
keyboard.press_and_release('enter')
32
if char.upper() == char and char not in ' nt,0123456789/*-+[];',./`': # if it's a capital letter
33
keyboard.press_and_release(f'shift+{char.lower()}')
34
else: # normal character
35
keyboard.press_and_release(char)
36
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:
JavaScript
1
6
1
import pynput
2
3
keyboard = pynput.keyboard.Controller()
4
5
keyboard.type("→")
6