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.22k stars 1.24k forks source link

Delay #597

Closed nickplj12 closed 3 years ago

nickplj12 commented 3 years ago

I'm trying to make a bot for the Roblox game Funky Friday. It's a rhythm game based off Friday Night Funkin.

fnf

It's not registering all notes, only pressing one note at a time. Here is my code:

import pyautogui
import pydirectinput

leftColor = (194, 75, 153)
downColor = (0, 255, 255)
upColor = (18, 250, 5)
rightColor = (249, 57, 63)

leftX = 731
#leftY = 189
downX = 888
#downY = 199
upX = 1024
#upY = 191
rightX = 1177
#rightY = 183

y = 189

leftKey = 'left'
downKey = 'down'
upKey = 'up'
rightKey = 'right'

while True:
    if pyautogui.pixelMatchesColor(leftX, y, (leftColor), tolerance = 50): #i also made the tolerance at 80 at one point
        print("LEFT")
        pydirectinput.press(leftKey)
    elif pyautogui.pixelMatchesColor(downX, y, (downColor), tolerance = 50):
        print("DOWN")
        pydirectinput.press(downKey)
    elif pyautogui.pixelMatchesColor(upX, y, (upColor), tolerance = 50):
        print("UP")
        pydirectinput.press(upKey)
    elif pyautogui.pixelMatchesColor(rightX, y, (rightColor), tolerance = 50):
        print("RIGHT")
        pydirectinput.press(rightKey)
nickplj12 commented 3 years ago

Windows 10 x86_64

nmstoker commented 3 years ago

As you are using another module (import pydirectinput) it looks to me like this isn't directly related to PyAutoGUI and you'd do better to go to the repo for that module or Google it.

However if pydirectinput works like PtAutoGUI's own press function then it's expected that it will be pressing them one at a time because the press corresponds to a key up then a key down, so your code effectively let's go of the first key then moves on to the next.

If you want multiple keys at once you need to send the key downs all together and then the key ups all together (for the keys you want pressed at the same time at least)

nmstoker commented 3 years ago

@nickpythonluajava I saw you'd also posted this in PyDirectInput:

https://github.com/learncodebygaming/pydirectinput/issues/23

So ideally you should:

  1. Close this issue (as I mentioned above it's not relevent to this repo)
  2. Answer your own issue using the explanation given here about the order of the down and up keys (ie doing all down keys first and then the up keys) and then when you've written it up, you can close that issue too

Hope the original advice above was useful, I'm pretty sure that the order is your problem.

Anyway, it's always good to close issues when you can or they've been opened in the wrong place. And writing up a line or two so others benefit from what you found is helpful to the wider community