asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.12k stars 1.23k forks source link

Sending pyautogui.press(str([Alt, S])) is pressing Shift, S #403

Open MostHated opened 4 years ago

MostHated commented 4 years ago

Hey there, I am testing on Windows 10, I am attempting to take a list and send it into the press() function. The receiving application can show was keys were received, and it is showing that Shift + S was sent and not Alt + S.

Here is what I was using, it is a PySide2 Tree widget getting clicked and sending the contents of a particular column when it is double-clicked, which contains text delimited by a space, but combined as a hotkey combination with a +, such as "Shift+S Alt+S" or "Ctrl+Alt+S Alt+S" or simply "S". I am taking whatever the first hotkey combination is and sending it to the parent widget as a keypress combination. I was trying to use the hotkey() function first, but the parent never received anything, so I tried using the press() function, which did get received, but it was received wrong.

    def searchclick_cb(self, item, column):
        self.winactive = 1
        self.searchbox.releaseKeyboard()
        self.searchbox.clearFocus()

        parent_widget.activateWindow()
        parent_widget.setFocus()
        parent_widget.focusWidget()

        text = item.text(2)
        split1 = text.split(' ')
        split2 = split1[0].split('+')

        time.sleep(.700)
        press(str(split2))
        time.sleep(.700)

        self.activateWindow()
        self.searchbox.setFocus()
        self.searchbox.grabKeyboard()
        self.winactive = 0

Thanks, -MH

MostHated commented 4 years ago

Apparently I had to get a bit more explicit with it:

        if len(split2) == 2:
            hotkey(str(split2[0]), str(split2[1]))
        if len(split2) == 3:
            hotkey(str(split2[0]), str(split2[1]), str(split2[2]))
        else:
            hotkey(str(split2))

It throws you off a bit, because the example explination says "pass a list"

But it seems that you can't literally pass a list of keys?

Thanks, -MH