boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.8k stars 433 forks source link

StopIteration error when using keyboard.write() with keys that aren't digits and lowercase? #485

Open AmeliaYeah opened 3 years ago

AmeliaYeah commented 3 years ago

So, I'm making a python script with the keyboard module, and I wanted to make it write an email. The code was relatively straightforward: keyboard.write('test@test.com'). When I ran the command, everything worked fine up until it reached the "@" symbol, to which it produced an exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 854, in write
    scan_code, modifiers = next(iter(entries))
StopIteration

I then opened up a python interpreter and went to investigate by using keyboard.write() with string.printable. Lo and behold, it still errored out, but it also errored out not just at a symbol but at any uppercase letter. The string that got successfully printed was 0123456789abcdefghijklmnopqrstuvwxyz

This seems to be a relatively new issue since I had this script for quite a while (around 2 weeks or so) which worked perfectly fine and didn't have this issue. However, since I installed a different distro recently, I had to reinstall everything including the keyboard package (which was around 2 hours ago I believe). Any help?

Kcchouette commented 3 years ago

Hello,

I have the same issue on Linux with the example of the README:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    keyboard.write('The quick brown fox jumps over the lazy dog.')
  File "/usr/lib/python3.9/site-packages/keyboard/__init__.py", line 854, in write
    scan_code, modifiers = next(iter(entries))
StopIteration

If I use 0123456789abcdefghijklmnopqrstuvwxyz instead of The quick brown fox jumps over the lazy dog., it's working

My source code of test.py is:

import keyboard

# Blocks until you press esc.
keyboard.wait('esc')
keyboard.write('The quick brown fox jumps over the lazy dog.')
jp-x-g commented 2 years ago

For all of those helpless internet wanderers, the issue is this: it doesn't do uppercase letters because keyboards don't have them. That is to say, there's no "A" key, there's just an "a" and a "shift" key. Per the documentation, you're supposed to do something like: keyboard.write("Barnacle Jim has a long face.", exact=True) which will force it to just send the string as Unicode. This actually worked, when I did it a few days ago -- but it's not working now, for reasons I can't understand, and all searches I've tried have landed me in sad little threads like this with people just as flummoxed as me. At any rate, maybe this will give you something to chew on.