keyboard icon indicating copy to clipboard operation
keyboard copied to clipboard

Can't write _(underscore)

Open jd-solanki opened this issue 3 years ago • 8 comments

I am struggling with writing underscore using keyboard.write. It throws below error:

Traceback (most recent call last):
  File "temp.py", line 3, in <module>
    keyboard.write("Trying to write underscore: _")
  File "/home/john/pylab/env_test/lib/python3.8/site-packages/keyboard/__init__.py", line 854, in write
    scan_code, modifiers = next(iter(entries))
StopIteration

Script Code:

# File: temp.py

import keyboard

keyboard.write("Trying to write underscore: _")

This same for some special keys like *, %, ^ etc.

jd-solanki avatar Mar 17 '21 04:03 jd-solanki

I'm also having this issue

anderph avatar Apr 06 '21 20:04 anderph

So if I remember correctly, Python has several special characters that are used for various things. I believe the underscore will return the previously calculated value, so inside the library somewhere it probably has some sort of problem with these kinds of characters. If it is using regular expressions (regex), all of these special characters are used as flags for identifying strings.

I am actually not at a computer right now so I have no way of testing this idea, but try using an escape character (\) in front of underscores or asterisks or etc.

gavinkrezel avatar Apr 07 '21 17:04 gavinkrezel

Hi, @gavinkrezel I tried using \ after your response but unfortunately, it didn't work.

# File: temp.py

import keyboard

keyboard.write("Trying to write underscore: \_")

jd-solanki avatar Apr 08 '21 02:04 jd-solanki

Eh it was worth a shot I guess.

You might also be able to format it as a raw string. I think you do that by adding an r in front of your string.

Ex. r"\no more special charac\ters"

gavinkrezel avatar Apr 08 '21 15:04 gavinkrezel

Using raw string isn't doing something as well. :laughing:

I guess we shall wait till it gets fixed. :wink:

jd-solanki avatar Apr 09 '21 05:04 jd-solanki

I'm having the same issues.

anderph avatar Apr 11 '21 15:04 anderph

Works on OSX Python3.8.6

lnfjobs avatar May 03 '21 19:05 lnfjobs

I think someone has solved it here: https://github.com/boppreh/keyboard/issues/485#issuecomment-1004688511

Sterver47 avatar Apr 23 '22 21:04 Sterver47