#🔒 Keyboard Events not being detected

33 messages · Page 1 of 1 (latest)

neon musk
#

im trying to make a macro for this game i play on roblox

i tried some different modules for listening for inputs and simulating them
pynput worked great but i wanted to make it more effecient
i tried using keyboard and mouse modules, but sometimes they just,,, wouldnt work and i really dont understand why
so im trying now to use ctypes idrk what im doing so its easier to just show the code

if you run the code you would think it works perfectly... the keys are spammed and stuff just as you would expect, but in the game the keypresses just wont register. like in a browser if i run the macro i can see the keys are being pressed like it will type them, but it just wont pick them up on the roblox game lol, but it will type them in the roblox chatbox.

i really dont know what im doing sorry if the code is a mess, ive never shown my code to anyone before so idk whats going on

import ctypes
import sys
import time
from threading import Thread


class Macro:
    def __init__(self, keys):
        self.keys = keys

        self.user32 = ctypes.windll.user32

        self.VK_CODES = {
            'esc': 0x1B,  # ESC key
            'f': 0x46,  # F key
            'c': 0x43,  # C key
            'r': 0x52,  # R key
            'v': 0x56,  # V key
            'g': 0x47,  # G key
            '1': 0x31,  # 1 key
            '2': 0x32,  # 2 key
            'end': 0x23,  # END key
            'home': 0x24,  # HOME key
        }

        self.spam_thread = None
        self.switch_thread = None

    def click(self):
        self.user32.mouse_event(0x02, 0, 0, 0, 0)  # Mouse down
        time.sleep(0.001)  # Small delay between press and release
        self.user32.mouse_event(0x04, 0, 0, 0, 0)  # Mouse up

    def press_and_release(self, key):
        key = self.VK_CODES[key]
        self.user32.keybd_event(key, 0, 0, 0)  # key press
        time.sleep(0.001)  # Small delay between press and release
        self.user32.keybd_event(key, 0, 0x02, 0)  # key release

    def spam(self):
        try:
            while True:
                self.click()
                for key in self.keys:
                    self.press_and_release(key)
                time.sleep(0.1)
        except SystemExit:
            pass

    def switch(self):
        try:
            while True:
                time.sleep(0.3)
                self.press_and_release("2")
                time.sleep(0.3)
                self.press_and_release("1")
        except SystemExit:
            pass

    def toggle(self):
        if self.spam_thread is None and self.switch_thread is None:
            self.spam_thread = Thread(target=self.spam, daemon=True)
            self.spam_thread.start()

            self.switch_thread = Thread(target=self.switch, daemon=True)
            self.switch_thread.start()

            print("threads started!")

        else:
            ctypes.pythonapi.PyThreadState_SetAsyncExc(self.spam_thread.ident, ctypes.py_object(SystemExit))
            self.spam_thread = None

            ctypes.pythonapi.PyThreadState_SetAsyncExc(self.switch_thread.ident, ctypes.py_object(SystemExit))
            self.switch_thread = None

            print("threads closed!")

    def add_listener(self, key, func, wait=False):
        key = self.VK_CODES[key]

        def wrapper():
            while True:
                if self.user32.GetAsyncKeyState(key) & 0x8000 != 0:
                    func()
                    while self.user32.GetAsyncKeyState(key) & 0x8000 != 0:
                        time.sleep(0.001)
                time.sleep(0.001)

        thread = Thread(target=wrapper, daemon=True)
        thread.start()

        if wait:
            thread.join()

    def listener(self):
        self.add_listener("end", self.toggle)
        self.add_listener("esc", sys.exit, wait=True)

    def start(self):
        print(sys.argv[0])
        self.listener()


def main():
    macro = Macro("frcv")
    macro.start()


if __name__ == '__main__':
    main()
leaden fieldBOT
#

@neon musk

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

neon musk
#

its this dumb roblox game called elemental dungeons

#

um here i can show you like what its supposed to do if you are interested

sweet granite
neon musk
#

yea i think so i mean like its just an autoclicker basically and i looked it up and it said that was allowed so

neon musk
#

um i just searched are autoclickers allowed in roblox and the first response said they are fine but it doesnt really matter... i used to talk about them publicly with others and stuff and like the mods were okay with it and the devs dont care they games basically dead anyway

#

but um i just dont understand why the keyboard presses dont work in the game

#

like they work in the chatbox

#

but not on the game itself

neon musk
#

idk i was using like the keyboard module but it wasnt working so i asked chatgpt like how the keyboard module itslef works and it said it sends inputs using the windows api and then gave me that code with ctypes so i just used that

sweet granite
#

i guessed that code was AI

sweet granite
neon musk
neon musk
# sweet granite what was wrong with using `keyboard`?

well the listeners wouldnt work that was the main thing. i was using on_press_key() and it would just like not respond sometimes, as far as simulating the keypresses that all worked fine, which is like my issue now is that i cant get that part to work. i could just use the keyboard module but idk i just kinda wanted to try and make it myself its probably dumb

sweet granite
#

ooh

neon musk
#

here ill show you the code

sweet granite
#

so that doesnt exist farm_vcatthink

neon musk
#
keyboard.on_press_key("end", lambda event: self.toggle())
#

its this function and it just like it would work most of the time but sometimes i would press end and like nothing would happen

#

and it was really annoying

#

i set the priority to high for the process and that worked better but sometimes it still missed it so i gave up on using it

neon musk
#

should i just like give up and use the pynput one because atleast that worked,,,

sweet granite
#

give me a moment, just gonna run some errands

neon musk
#

okay, thanks for the help

leaden fieldBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.