Skip to content

Instantly share code, notes, and snippets.

@fearofcode
Last active November 9, 2024 05:15
Show Gist options
  • Save fearofcode/107bef5a6d776c95c9d98080b4620a5e to your computer and use it in GitHub Desktop.
Save fearofcode/107bef5a6d776c95c9d98080b4620a5e to your computer and use it in GitHub Desktop.
Send space to semi-AFK equipment bench in Satisfactory (Satisfactory window does not need to be active)
import ctypes
from ctypes import wintypes
import time
import sys
# Windows API constants
WM_KEYDOWN = 0x0100
WM_KEYUP = 0x0101
VK_SPACE = 0x20
# Windows API function prototypes
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
def find_window(window_title):
"""Find window handle by title."""
target_window = []
def enum_windows_callback(hwnd, lParam):
if not IsWindowVisible(hwnd):
return True
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
title = buff.value
if window_title.lower() in title.lower():
target_window.append(hwnd)
return False
return True
EnumWindows(EnumWindowsProc(enum_windows_callback), 0)
return target_window[0] if target_window else None
def send_space_to_window(hwnd):
"""Send spacebar keypress to specific window."""
ctypes.windll.user32.PostMessageA(hwnd, WM_KEYDOWN, VK_SPACE, 0)
def release_space_to_window(hwnd):
"""Send spacebar key release to specific window."""
ctypes.windll.user32.PostMessageA(hwnd, WM_KEYUP, VK_SPACE, 0)
def is_key_pressed(key):
"""Check if a key is pressed using GetAsyncKeyState."""
return bool(ctypes.windll.user32.GetAsyncKeyState(ord(key)) & 0x8000)
def hold_key(window_title):
"""Hold the spacebar down for specified window until q is pressed."""
# Find the window
hwnd = find_window(window_title)
if not hwnd:
print(f"Could not find window with title containing: {window_title}")
return
print(f"Found window: {window_title}")
print("Starting in 3 seconds...")
print("Press 'Q' to quit")
time.sleep(3)
running = True
while running:
if is_key_pressed('Q'):
running = False
else:
send_space_to_window(hwnd)
time.sleep(0.1) # Small delay to prevent overwhelming the system
# Make sure to release the key when script ends
release_space_to_window(hwnd)
print("Spacebar released")
if __name__ == "__main__":
window_title = "Satisfactory"
if len(sys.argv) > 1:
window_title = sys.argv[1]
hold_key(window_title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment