Search code examples
pythonpython-3.xmultithreadingkeyboardbots

Key pressing in python doesn't work properly (in UCN game bot)


I wrote this code in python3 to cheat the game FNaF : UCN so whenever I'm pressing and holding down the right click button, it'll keep pressing c and <enter> both together, getting me rid of two annoying game mechanics. Here's the code:

import keyboard
from pynput.keyboard import Key, Controller
Keyboard = Controller()
from pynput.mouse import Listener, Button
import threading
import time
Held = False
def pressEnter():
    while Held: # While I'm holding down the button
        Keyboard.press(Key.enter) # Press <enter>
        Keyboard.press(key="c") # Press "c"
        print("Pressing...\n", Held)
        time.sleep(0.1)
def easier_Enter(x, y, butt, held):
    global Held
    Held = held
    if (butt == Button.right) and held:
        threading.Thread(target=pressEnter, daemon=True, args=()).start()
    else: print("Stopped.")
def listener1():
    while True:
        with Listener(on_click=easier_Enter) as l:
            l.join()
def main():
    threading.Thread(target=listener1, daemon=True, args=()).start()
    print("Thread 1 started.")
    while not keyboard.is_pressed('x'):
        try:
            pass
        except KeyboardInterrupt:
            getOut()
    getOut()
def getOut():
    print("Exitted.")
    quit(0)

if __name__ == "__main__": main()

When I run the script, it works just as expected and when I hold down the button, it starts pressing those two keys. However though, it doesn't really work while I'm IN THE GAME which is the whole purpose of the program. It's like it's only working on 1 window.

How can I make it work on the game window as well?


Solution

  • First of all, I better explain the two game mechanics I'm trying to beat in the game, so you can understand the topic much better:

    Old Man Consequences: Appears at random and player needs to catch his fish by pressing c on the keyboard. The fish keeps swimming around the rod and only when it hits rod we can catch it by hitting c.

    El Chip: He shows his ADS at random trying to block your view, you can press the "skip" button appeared on the screen OR alternatively hit enter on the keyboard to close his AD.

    So, in my program I tried hitting the two c & enter keys at once, which a friend told me it usually wouldn't work, so I decided to use time.sleep() to wait for a very short amount of time before pressing the next one. But the method I used to press the keys wasn't a good one, so I found many methods and tried them out and got these results:

    Method 1: pyautogui.press()

    import pyautogui as pag
    ...
    pag.press('c')
    time.sleep(0.1)
    pag.press('enter')
    time.sleep(0.1)
    
    • This method was the first one that worked for me, but it made the game a bit laggy and didn't catch the fish at the right time or closed the AD fast enough.

    Method 2: pyautogui.keyDown() & .keyUp()

    import pyautogui as pag
    ...
    pag.keyDown('c')
    time.sleep(0.1)
    pag.keyUp('c')
    time.sleep(0.1)
    pag.keyDown('enter')
    time.sleep(0.1)
    pag.keyDown('enter')
    time.sleep(0.1)
    
    • This one work absolutely better than the previous one, but still was laggy and usually lost catching the fish, but always closed the AD although it wasn't so fast.

    Method 3: pyautogui.hotkey()

    import pyautogui as pag
    ...
    pag.hotkey('c')
    time.sleep(0.1)
    pag.hotkey('enter')
    time.sleep(0.1)
    
    • This worked WAY MUCH better than the other ones, closes the AD so well but lost to the Old man for a couple of times, and also didn't make the game laggy.

    And the final method and the best method that I found it myself:

    Using keyboard.press_and_release()

    keyboard.press_and_release('c')
    time.sleep(0.1)
    keyboard.press_and_release('enter')
    time.sleep(0.1)
    
    • This one worked perfect! never lost to the Old Man Consequences nor the El Chip! it closes the AD and catches the fish SO FAST and doesn't interrupt the game or makes it glitchy and laggy!

    SO! here's the final result:

    import keyboard
    from pynput.keyboard import Key, Controller
    Keyboard = Controller()
    from pynput.mouse import Listener, Button
    import pyautogui as pag
    import threading
    import time
    Held = False
    def pressEnter():
        while Held:
            keyboard.press_and_release('c')
            time.sleep(0.1)
            keyboard.press_and_release('enter')
            time.sleep(0.1)
    def easier_Enter(x, y, butt, held):
        global Held
        Held = held
        if (butt == Button.right) and held:
            threading.Thread(target=pressEnter, daemon=True, args=()).start()
    def listener1():
        while True:
            with Listener(on_click=easier_Enter) as l:
                l.join()
    def main():
        threading.Thread(target=listener1, daemon=True, args=()).start()
        print("Thread 1 started.")
        while not keyboard.is_pressed('x'):
            try:
                pass
            except KeyboardInterrupt:
                getOut()
        getOut()
    def getOut():
        print("Exitted.")
        quit(0)
    
    if __name__ == "__main__": main()