moses-palmer / pynput

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

Bug in the .type() method #547

Open thomaslc66 opened 1 year ago

thomaslc66 commented 1 year ago

Description When using the type command, depending on the trigger, the text is type in upper case or lower case

Platform and pynput version Plateform:: Win 11 x64 pynput version: 1.7.6

Small code to Reproduce the bug

from pynput.keyboard import Key, Controller, Listener

class KeyListener:
    def __init__(self):
        self.listener = Listener(on_press=self.on_press)
        self.listener.start()
        self.keyboard = Controller()

    def on_press(self, key):
        try:
            if key == Key.esc:
                self.stop()
            elif key == Key.enter or key.char == ";":
                # Execute the command when "Enter" or ";" is detected
                self.execute_command()
        except AttributeError:
            pass

    def execute_command(self):
        # just print something
        self.keyboard.type('Test from the execute command')

    def stop(self):
        self.listener.stop()

def main():   
    listener = KeyListener()
    listener.listener.join()

if __name__ == "__main__":
    main()

image

If for example, you edit the key.char == ";" to be /or: or any special char it's not working and the text is displayed in upper case. But if you put a letter let's say 'z' then the text is displayed correctly.

I'm currently using an qwertz keyboard.

I just wanted to understand if it's related to my keyboard or if it's something that everybody is experiencing. I'm currently testing on another computer to see if I have the same behavior

thomaslc66 commented 1 year ago

Ok I've tested it on a windows 10 x64 and it seems that the behavior is different as only the first word is put in upper case

image

thomaslc66 commented 1 year ago

So as only the TEST word was in upper case I though this was due to the fact that the word was touching the ":" a bit like a markdown style thingy. So I've test to add a space and instead of "test from..." I've changed it to " test from..." and the result is that now more of the text is in upper case

image

moses-palmer commented 1 year ago

Thank you for your report.

The keyboard state on Windows is a global, in-kernel data structure. pynput attempts to send characters and not keys when simulating key presses, but perhaps those too are affected by the keyboard state.

Does your keyboard layout require you to press shift when typing a semicolon? Maybe that is the cause of this strange interaction?