2

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 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."))

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."\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)

I can't figure out how to type this key.

dimani128
  • 75
  • 7
  • What is your question about this? – mkrieger1 Dec 26 '22 at 23:56
  • I think you are asking how you can type that character with python-keyboard, is this correct? – MrDiamond Dec 26 '22 at 23:58
  • 1
    How would you enter this character by hand? – user2357112 Dec 27 '22 at 00:03
  • @user2357112 It would usually be with an alt code, holding the right alt key and pressing the alt code numbers on your numpad, and releasing the alt key would type it. I can't find any resources on how to press right alt or any numpad key with python keyboard. – MrDiamond Dec 27 '22 at 00:18
  • Check this Question and answer. https://stackoverflow.com/questions/67103616/what-are-all-the-key-names-in-keyboard-module. – Carl_M Dec 27 '22 at 00:35
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 27 '22 at 00:42

1 Answers1

2

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("→")
MrDiamond
  • 958
  • 3
  • 17