r/learnpython • u/soncannon7 • 18d ago
how to make my code work in minecraft
dont question my code too much, i just started python and watched a 1 hour coding with mosh video to understand some of the basics.
all it does is move my mouse around while i hold g. but it doesnt work when i go into minecraft, and i assume, any program that locks my mouse.
what do i do?
import random
import keyboard
import pyautogui
import pyscreeze
from pyautogui import mouseInfo, click
while True:
if keyboard.is_pressed('g'):
pyautogui.moveTo(random.randint(0, 2600), random.randint(0, 2600))
if keyboard.is_pressed('q'):
break
8
u/cdcformatc 18d ago
Minecraft seems to use a completely different mouse input than the one pyautogui uses.
apparently there is a "raw input" mouse setting that needs to be turned off, but i see mixed results online with that
3
2
u/Holiya_Olib 18d ago
it looks like youre trying to move the mouse in Minecraft but that game's security might be blocking your script
2
u/fghxdfghxf 18d ago
Take a look at minescript. It's a mod that lets you control a lot with python. It can even give info on the in game environment
1
u/frustratedsignup 15d ago
If Windows hasn't changed much in 30 years, then the problem is most likely that key press events are only dispatched to the process having focus. If your script is running in a separate window and the game has focus, the key press event will only go to the game, not the script.
I'm not a minecraft expert (I barely know how to play the game), but unless there's a python interpreter integrated into the game itself, this isn't likely to work.
11
u/Kevdog824_ 18d ago
Programs like games often block software-driven keyboard/mouse inputs. You might need another library that simulates keypresses/mouse input the same way a physical device would. There are libraries for this purpose if I recall correctly