moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.79k stars 248 forks source link

Garbled number presses in .type unless some key is pressed beforehand. #320

Closed vincent-hugot closed 3 years ago

vincent-hugot commented 3 years ago

Hello,

Here is a simple script with (imo) strange behaviour:

the script types a series of floating point values, separated by ENTER. However, without an initial, otherwise useless keypress, here backspace, before the .type call, the numbers come out completely garbled.

Here is the script:

"""
edt.insa: I'm waaay too lazy to type all those marks by hands.

VH
"""

from pynput.keyboard import Key, Controller
from time import sleep

k = Controller()

marks = [ min((i % 21) + i/100,20) for i in range(58) ]

def type_marks(l):
    sleep(5) # time to position yourself in first cell
    k.press(Key.backspace)  # black magic bugfix: without useless keypress before .type,
    k.release(Key.backspace)# numbers come out garbled, eg @<)@... -> open pynput issue
    for m in l:
        k.type(str(m))
        k.press(Key.enter)
        k.release(Key.enter)

type_marks(marks)

Potentially relevant info: I use an AZERTY keyboard (French), though my system is in English. KUbuntu 17.10, Py 3.9.0. pynput installed today.

example:

type_marks(range(10)) # with backspace press removed
# should type 0 to 9
)
!
@
#
$
%
^
&
*
(

It looks as though as though Shift is pressed on a QWERTY keyboard (I don't have one on hand to check). Needless to say I neither press shift during execution nor have capslock on. And why would an initial backspace press change anything?

Am I missing something obvious here ?

Cordially, VH

moses-palmer commented 3 years ago

Thank you for your report.

That is indeed strange. I have tested your script with and without black magic applied on Ubuntu 20.10, and both versions give the same output. I installed the layout French (AZERTY) and it still worked. Do you have any other software that might interfere installed? Have you confirmed the error in other Desktop Environments? I run GNOME.

vincent-hugot commented 3 years ago

Hello.

I have not tested in other environments; I run KDE. I cannot imagine what kind of software might produce such weird interference.

It would be interesting for me to install Gnome on the same machine and re-test from that DE. However, given how critical that system is to my work right now, I am disinclined to play around with it more than strictly necessary.

I have a laptop with a similar (albeit more recent) config, which is less essential. I'll try and test the script on that; probably this WE.

Cheers, VH

vincent-hugot commented 3 years ago

I could not reproduce that behaviour in a Kubuntu 18.04.5 LTS, using Py 3.8.0, otherwise very similar to the other system.

I might test with 3.9.0 at some point.

moses-palmer commented 3 years ago

In order not to clutter the github interface, I will close this issue. If you manage to reproduce it consistently, please reopen.