0

On RaspberryPi (and RaspberryPi OS) I installed the keyboard module, the installation seems to be intact, but even the simplest functions don't work at all or behave erratically. I will give you two examples, though there are more. I execute my programs in the terminal using sudo.

Example A gives me no output at all, but shouldn't it actually? Same goes for the keyboard.send function.

import keyboard as kb
kb.write("hello")

example B gives me different output every time..., at some point stops working at all. Also, my idea was to block the output of "1" and only output the statement. With print functions it works fine btw.

import keyboard as kb
while True:
    kb.wait("1")
    kb.write("\nYou have pressed the number one!\n")

example C does do a playback of my input, but then it carries that input over to the next prompt, and takes them as a command as if enter were pressed.

import keyboard as kb
recorded = kb.record(until="esc")
kb.play(recorded)

In example D I simply want to allow for only digits to be entered by the user. It outputs my warning twice. Typing digits works fine, they get stored in a variable which gets printed out in the end. But what does ^[ mean here actually, why is it there at all? Again, all my pressed keys get carried over to the next prompt.

import keyboard as kb

keystroke = ""
digitstring = ""

while keystroke != "esc":
    keystroke = kb.read_key()

    if keystroke.isdigit() == True:
        digitstring = digitstring + keystroke
        continue
    else:
        print("\nOnly digits allowed!")

print(digitstring)

If anyone has any ideas or clues, please shoot! Thank you.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
seb
  • 1
  • 2
  • did you run it in console to see error messages? On Linux this module needs to run as root - so it needs to run script with `sudo` – furas Jul 15 '22 at 11:31
  • keyboard send text to active window - so if active window in console then it will send it to console and console will treat it as normal command from user. If there is `new line` (enter) then console will try to execute it. – furas Jul 15 '22 at 11:33
  • `^[` can means `esc`. Maybe you should check `esc` directly after `read_key()` – furas Jul 15 '22 at 11:35
  • some modules may send text to buffer and they may wait for `\n` to send buffer to console. – furas Jul 15 '22 at 11:37
  • if you want to use keyboard instead of `input()` then it is wrong idea. Keyboard catch keys from system but it doesn't stop system to send the same key to active console - it wasn't created for this. You may need rather module `getchar` or [getch](https://pypi.org/project/getch/) instead of `kb.read_key() ` - see [python - How to read a single character from the user? - Stack Overflow](https://stackoverflow.com/questions/510357/how-to-read-a-single-character-from-the-user) and pypi: [getch](https://pypi.org/search/?q=getch) (i.e. [readkeys](https://pypi.org/project/readkeys/)) – furas Jul 15 '22 at 11:41

0 Answers0