Skip to content

Instantly share code, notes, and snippets.

@AScriver
Created January 18, 2024 00:23
Show Gist options
  • Save AScriver/956e6c1a6fdf0d2c87cc09c67a133009 to your computer and use it in GitHub Desktop.
Save AScriver/956e6c1a6fdf0d2c87cc09c67a133009 to your computer and use it in GitHub Desktop.
Roblox anti-afk script that uses a virtual controller to simulate input.
import time
## fix ms-gamebar annoyance after uninstalling Xbox: https://www.reddit.com/r/Windows11/comments/vm046d/is_there_any_way_to_remove_the_xbox_game_bar/ie0j6o3/
# Try importing the required packages
try:
import vgamepad as vg
import pygetwindow as gw
import win32gui
import win32con
except ImportError:
print("Some dependencies are missing. Installing them...")
import subprocess
subprocess.call(['pip', 'install', 'vgamepad', 'pygetwindow', 'pywin32'])
print("Dependencies installed. Please restart the script.")
exit()
class RobloxWindowController:
def __init__(self):
self.gamepad = None
def restore_window(self, window):
if window.isMinimized:
print("Restoring window...")
window.restore()
def focus_window(self, window):
if not window.isActive:
print("Trying to focus on Roblox window...")
try:
window.activate() # Activate the window
except:
pass
def handle_roblox_window(self, roblox_window):
was_minimized = False
self.gamepad = vg.VX360Gamepad()
try:
if roblox_window.isMinimized:
was_minimized = True
print("Restoring window...")
roblox_window.restore()
self.focus_window(roblox_window)
time.sleep(0.5) # Wait for a short while after activating
print("Press")
self.gamepad.press_button(vg.XUSB_BUTTON.XUSB_GAMEPAD_X)
self.gamepad.update()
time.sleep(0.1)
print("Release")
self.gamepad.release_button(vg.XUSB_BUTTON.XUSB_GAMEPAD_X)
self.gamepad.update()
if was_minimized:
roblox_window.minimize()
except IndexError:
print("Roblox window not found.")
time.sleep(10) # Wait before trying again
except Exception as e:
print(f"Error with window '{roblox_window.title}': {e}")
self.gamepad = None
def run(self):
while True:
roblox_windows = gw.getWindowsWithTitle("Roblox") # Get all windows with 'Roblox' in title
if not roblox_windows:
print("Roblox window not found.")
time.sleep(10) # Wait before trying again
continue
for roblox_window in roblox_windows:
if roblox_window.title == "Roblox":
self.handle_roblox_window(roblox_window)
time.sleep(900) # Wait before the next loop iteration
if __name__ == "__main__":
controller = RobloxWindowController()
controller.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment