Reputation: 1
So I made a program to move the player in Minecraft to prevent servers from marking me as AFK. The code gives output in other applications and simulates the keyboard. However, in Minecraft, it doesn't seem to give any output of any sort. I suspect this is because I am switching to another window but it worked on other apps such as notepad. I've tried various timings and it figured that isn't the problem The delay is to give me time to switch to the required window Here is my code:
import pyautogui as gui
import time
wait_time = 10
def move_ingame():
print("moving!")
gui.press('space', presses=3, interval=1 )
def start_afk():
print("Starting AFK in {} seconds. Make sure you have MC running and open.".format(wait_time))
time.sleep(wait_time)
while True:
move_ingame()
time.sleep(30)
def change_wait_time():
print("Wait time is set to {} seconds. Enter value to change it to.".format(wait_time))
change_val = input()
if type(change_val) == int:
wait_time = change_val
print("Wait time has been changed to {}".format(wait_time))
x1 = input("Press enter to go back to main_menu")
else:
print("Please enter a valid number. Only integers. No decimals allowed! Im lazy but ill change that soon ;)")
def load_page():
print("Welcome to Franklin's server AFK autobot")
print("Type \"q\" and press enter to change initial wait time")
init_input = input()
if init_input == "Q" or init_input == "q":
change_wait_time()
else:
start_afk()
load_page()
Upvotes: 0
Views: 145
Reputation: 347
This is probably because normal input doesn't work, you need direct input. I have a little module that does this sort of thing, let me demonstrate
directkeys.py:
import ctypes
import time
SendInput = ctypes.windll.user32.SendInput
W = 0x11
A = 0x1E
S = 0x1F
D = 0x20
M = 0x32
K = 0x25
SPACE = 0x39
# C struct redefinitions
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
_fields_ = [("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", PUL)]
class HardwareInput(ctypes.Structure):
_fields_ = [("uMsg", ctypes.c_ulong),
("wParamL", ctypes.c_short),
("wParamH", ctypes.c_ushort)]
class MouseInput(ctypes.Structure):
_fields_ = [("dx", ctypes.c_long),
("dy", ctypes.c_long),
("mouseData", ctypes.c_ulong),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", PUL)]
class Input_I(ctypes.Union):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("hi", HardwareInput)]
class Input(ctypes.Structure):
_fields_ = [("type", ctypes.c_ulong),
("ii", Input_I)]
from ctypes import windll, Structure, c_long, byref
class POINT(Structure):
_fields_ = [("x", c_long), ("y", c_long)]
def queryMousePosition():
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return pt
# return { "x": pt.x, "y": pt.y}/
def click(x, y):
# convert to ctypes pixels
# x = int(x * 0.666)
# y = int(y * 0.666)
ctypes.windll.user32.SetCursorPos(x, y)
ctypes.windll.user32.mouse_event(2, 0, 0, 0, 0) # left down
ctypes.windll.user32.mouse_event(4, 0, 0, 0, 0) # left up
def moveMouseTo(x, y):
# convert to ctypes pixels
# x = int(x * 0.666)
# y = int(y * 0.666)
print(x, y)
ctypes.windll.user32.SetCursorPos(x, y)
# ctypes.windll.user32.mouse_event(2, 0, 0, 0, 0) # left down
# ctypes.windll.user32.mouse_event(4, 0, 0, 0, 0) # left up
def PressKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput(0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(1), ii_)
ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def ReleaseKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput(0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(1), ii_)
Now you would just replace your pyautogui functions from directkeys.py.
Let me give you an example:
from directkeys import PressKey, ReleaseKey, W
import time
def holdW():
PressKey(W)
time.sleep(5)
ReleaseKey(W)
Be aware if you press other keys, you will need to search their respective key scan codes.
Upvotes: 1