keyboard
keyboard copied to clipboard
StopIteration during keyboard.write
On Fedora 28 with Python 3.6.6 I get the following problem:
>>> text = "TESTAA"
>>> keyboard.write(text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/some/path/venv/lib/python3.6/site-packages/keyboard/__init__.py", line 852, in write
scan_code, modifiers = next(iter(entries))
StopIteration
>>> TEST
Hi! I have exactly the same problem on Ubuntu 18.04, however I can use keyboard from pynput module for the same purposes, here is an example:
from pynput.keyboard import Controller
keyboard=Controller()
keyboard.type('TESTAA')
For doing so you need to download the pynput module, which can be downloaded from the terminal via pip with: pip install pynput
There is also a complete tutorial about simulating keypresses with pynput: https://nitratine.net/simulate-keypresses-in-python/
Hi! I have exactly the same problem on Ubuntu 18.04, however I can use keyboard from pynput module for the same purposes, here is an example:
from pynput.keyboard import Controller
keyboard=Controller()
keyboard.type('TESTAA')
For doing so you need to download the pynput module, which can be downloaded from the terminal via pip with:pip install pynput
There is also a complete tutorial about simulating keypresses with pynput: https://nitratine.net/simulate-keypresses-in-python/
Hi! I can use keyboard from pynput module , but i had the save errors with @exploide !
I haven't tried yet using another module, but I have the same error
I wrote one for loop and it worked, I had to run it about 12 times to fill some stuff, all was fine I wrote another different for loop and I got iteration error
I'm on Ubuntu 18.04, but I believe I had similar error some time ago on Win10
Me too, same issue on below platforms:
4.18.0-kali3-amd64 #1 SMP Debian 4.18.20-2kali2 (2018-11-30) x86_64 GNU/Linux
4.4.0-142-generic #168-Ubuntu SMP Wed Jan 16 21:00:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Same here, ubuntu 18.10
Traceback (most recent call last):
File "gateway.py", line 3, in <module>
keyboard.write('The quick brown fox jumps over the lazy dog.')
File "/home/fgervais/personal/microbit/radio-keyboard/.venv/lib/python3.6/site-packages/keyboard/__init__.py", line 852, in write
scan_code, modifiers = next(iter(entries))
StopIteration
I got a bit more info on this.
This is my _nixkeyboard.py:from_name.
I'm not sure why but it's missing a lot of characters. If I keyboard.write() characters from this table it works fine.
defaultdict(<class 'list'>,
{' ': [],
"'": [(40, ())],
'+': [(78, ()), (118, ()), (118, ('shift',)), (118, ('alt gr',))],
',': [(51, ())],
'-': [(74, ())],
'.': [(52, ()), (83, ()), (121, ())],
'/': [(53, ())],
'0': [(11, ()), (82, ())],
'1': [(2, ()), (79, ())],
'2': [(3, ()), (80, ())],
'3': [(4, ()), (81, ())],
'4': [(5, ()), (75, ())],
'5': [(6, ()), (76, ())],
'6': [(7, ()), (77, ())],
'7': [(8, ()), (71, ())],
'8': [(9, ()), (72, ())],
'9': [(10, ()), (73, ())],
';': [(39, ())],
'<': [(86, ())],
'=': [(13, ())],
'>': [(86, ('shift',))],
'L': [],
'R': [],
'[': [(26, ())],
'\\': [(43, ())],
']': [(27, ())],
'^': [],
'`': [(41, ())],
'a': [(30, ())],
'alt': [(56, ()), (100, ()), (125, ()), (126, ())],
'alt gr': [],
'alt l': [(56, ()), (100, ()), (125, ()), (126, ())],
'alt lock': [],
'alt r': [],
'altgr l': [(56, ()), (100, ()), (125, ()), (126, ())],
'altgr lock': [],
'altgr r': [],
'altl': [(56, ()), (100, ()), (125, ()), (126, ())],
'altllock': [],
'altr': [],
'altrlock': [],
'b': [(48, ())],
'backspace': [(14, ()), (35, ())],
'bielorussian cyrillic capital letter i': [],
'bielorussian cyrillic small letter i': [],
'break': [(101, ())],
'c': [(46, ())],
'caps lock': [(58, ())],
'capsshift': [],
'ctrl': [(29, ()), (97, ())],
'ctrll': [],
'ctrlr': [],
'cyrillic capital letter e': [],
'cyrillic capital letter ge': [],
'cyrillic capital letter ghe': [],
'cyrillic capital letter ghe with upturn': [],
'cyrillic capital letter ha': [],
'cyrillic capital letter i': [],
'cyrillic capital letter ia': [],
'cyrillic capital letter ii': [],
'cyrillic capital letter iu': [],
'cyrillic capital letter kha': [],
'cyrillic capital letter reversed e': [],
'cyrillic capital letter short i': [],
'cyrillic capital letter short ii': [],
'cyrillic capital letter ya': [],
'cyrillic capital letter yeri': [],
'cyrillic capital letter yeru': [],
'cyrillic capital letter yu': [],
'cyrillic small letter e': [],
'cyrillic small letter ge': [],
'cyrillic small letter ghe': [],
'cyrillic small letter ghe with upturn': [],
'cyrillic small letter ha': [],
'cyrillic small letter i': [],
'cyrillic small letter ia': [],
'cyrillic small letter ii': [],
'cyrillic small letter iu': [],
'cyrillic small letter kha': [],
'cyrillic small letter reversed e': [],
'cyrillic small letter short i': [],
'cyrillic small letter short ii': [],
'cyrillic small letter ya': [],
'cyrillic small letter yeri': [],
'cyrillic small letter yeru': [],
'cyrillic small letter yu': [],
'd': [(32, ())],
'delete': [(111, ())],
'do': [(116, ()), (116, ('shift',)), (116, ('alt gr',))],
'doubleacute': [],
'down': [(108, ())],
'e': [(18, ())],
'end': [(107, ())],
'enter': [(28, ()), (96, ()), (36, ())],
'esc': [(1, ())],
'f': [(33, ())],
'f1': [(59, ())],
'f10': [(68, ())],
'f11': [(87, ())],
'f12': [(88, ())],
'f13': [(113, ()), (113, ('shift',)), (113, ('alt gr',))],
'f14': [(114, ()), (114, ('shift',)), (114, ('alt gr',))],
'f17': [(117, ()), (117, ('shift',)), (117, ('alt gr',))],
'f2': [(60, ())],
'f3': [(61, ())],
'f4': [(62, ())],
'f5': [(63, ())],
'f6': [(64, ())],
'f7': [(65, ())],
'f8': [(66, ())],
'f9': [(67, ())],
'g': [(34, ())],
'h': [(35, ()), (14, ()), (35, ())],
'help': [(115, ()), (115, ('shift',)), (115, ('alt gr',))],
'home': [(102, ())],
'i': [(23, ()), (15, ()), (23, ())],
'iabovedot': [],
'idotabove': [],
'idotless': [],
'insert': [(110, ())],
'j': [(36, ()), (28, ()), (96, ()), (36, ())],
'k': [(37, ())],
'keyboardsignal': [],
'keypad +': [(78, ()),
(118, ()),
(118, ('shift',)),
(118, ('alt gr',))],
'keypad -': [(74, ())],
'keypad .': [(83, ()), (121, ())],
'keypad 0': [(82, ())],
'keypad 1': [(79, ())],
'keypad 2': [(80, ())],
'keypad 3': [(81, ())],
'keypad 4': [(75, ())],
'keypad 5': [(76, ())],
'keypad 6': [(77, ())],
'keypad 7': [(71, ())],
'keypad 8': [(72, ())],
'keypad 9': [(73, ())],
'keypad enter': [(96, ())],
'keypad ×': [(55, ())],
'keypad ÷': [(98, ())],
'khi': [],
'ksi': [],
'l': [(38, ())],
'lamda': [],
'last console': [(84, ())],
'left': [(105, ())],
'm': [(50, ())],
'macro': [(112, ()), (112, ('shift',)), (112, ('alt gr',))],
'menu': [(127, ())],
'multiplication': [(55, ())],
'n': [(49, ())],
'no-break space': [],
'num lock': [(69, ())],
'o': [(24, ())],
'ooblique': [],
'p': [(25, ())],
'page down': [(109, ())],
'page up': [(104, ())],
'paragraph sign': [],
'pause': [(119, ())],
'pilcrow': [],
'q': [(16, ())],
'r': [(19, ())],
'right': [(106, ())],
'rightanglequote': [],
's': [(31, ())],
'scontrol': [],
'scroll lock': [(70, ())],
'sctrl': [],
'shift': [(42, ()), (54, ())],
'shift l': [],
'shift r': [],
'shiftl': [],
'shiftr': [],
'soft hyphen': [],
'space': [(57, ())],
'spawn console': [],
't': [(20, ())],
'tab': [(15, ()), (23, ())],
'u': [(22, ())],
'ukrainian cyrillic capital letter ghe with upturn': [],
'ukrainian cyrillic capital letter i': [],
'ukrainian cyrillic small letter ghe with upturn': [],
'ukrainian cyrillic small letter i': [],
'uncaps shift': [],
'up': [(103, ())],
'v': [(47, ())],
'w': [(17, ())],
'x': [(45, ())],
'y': [(21, ())],
'z': [(44, ())],
'|': [(86, ('alt gr',))],
'~': [],
'§': [],
'\xad': [],
'¶': [],
'¸': [],
'»': [],
'×': [(55, ())],
'÷': [(98, ())],
'ø': [],
'ı': [],
'ˆ': [],
'ˇ': [],
'˘': [],
'˛': [],
'˜': [],
'λ': [],
'ξ': [],
'χ': [],
'−': [(12, ())]})
Just ran into this issue today while trying to use keyboard.write
with a string with capital letters in it. We could only get it to work by using a lowercase string:
# Doesn't work
keyboard.write("AAAAA")
# Does work
keyboard.write("aaaaa")
For our use case, capitalization is not important, so we're just lowercasing our strings before we write them.
Knowing this, it seems like you'd have to use other methods to get proper capitalization in your strings (some combination of keyboard.press
and keyboard.release
could maybe work). It also looks like this example from the README
keyboard.write('The quick brown fox jumps over the lazy dog.')
doesn't work (at least on Linux).
Issue still prevalent on Linux, symbols and capital letters will throw "stopiteration"
Same issue on Centos 7 machine... script is simple
import keyboard keyboard.write("Python Programming is kinda cool I should say!", delay=0.3) keyboard.send('windows+l')
Steps to repro:
$ sudo python3 test_keyboard.py
Traceback (most recent call last):
File "test_keyboard.py", line 3, in
I've done research in SO but nothing... this appears the only place and the issue is Open.
If someone knows an alternative to the "keyboard" module that would be fantastic!
I am aware that something like this could be used(windows example):
import os os.system('rundll32.exe user32.dll,LockWorkStation')
However something more high level that can work on most machines would be best.
same thing happens here. i only get that error message when i am trying on linux. it works with no error in windows
I have the same problem on Ubuntu 18.04.4. I did a little digging and problem comes from "dumpkeys --keys-only". It's used to get the keys from the system and on my system a few of the capital letters are missing.
Use dumpkeys --keys-only | grep ^keycode
to check which characters are affected.
I have an issue in Ubuntu-20.04 while using pynput module Code:
from pynput.keyboard import Controller
import time
keyboard = Controller()
time.sleep(2)
keyboard.type("hello world")
Output:
/usr/bin/python3.8 /home/<user>/PycharmProjects/Bots/bot.py
Process finished with exit code 0
and it printed : h wor
I pulled it from pip today and it still has this error on Linux. the pynput example worked, though, so I think I'll go with that.
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("Baba booey.", 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.
Using the exact=True
param as mentioned above didn't solve my issue.
As a workaround I send each char of a string separately and wrap it in a try except:
for char in 'poespoespSSSSoes!!POESPOESPOES!poespoespoes':
if char.isupper() or not char.isalpha():
keyboard.press_and_release(f'shift+{char.lower()}')
else:
try:
keyboard.press(char)
except Exception as err:
print(err)
try:
keyboard.release(char)
except Exception as err:
print(err)
This is the error when trying to press a capital char:
Traceback (most recent call last):
File "/home/simonl/bin/projects/autokeyclone/temp.py", line 16, in <module>
keyboard.press_and_release(char)
File "/usr/lib/python3.10/site-packages/keyboard/__init__.py", line 379, in send
parsed = parse_hotkey(hotkey)
File "/usr/lib/python3.10/site-packages/keyboard/__init__.py", line 344, in parse_hotkey
scan_codes = key_to_scan_codes(hotkey)
File "/usr/lib/python3.10/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 'S' is not mapped to any known key.", None)
edit: using keyboard.press
with special keys like !
ain't working on wayland using sway, but keyboard.press_and_release('shift+1')
does produce !
so I had to create a map for each special key and go from there.
edit: I ended up sending text to the clipboard and pasting it instead of pressing each character individually xD
Issue is still reproducible (Standard Manjaro + Python 3.10). Workaround for that problem could be to use caps lock button:
keyboard.send("capslock")
keyboard.write("text")
keyboard.send("capslock")
issue still here on arch linux.