Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the mouse in Minecraft using Python?

All in all, I'm trying to programmatically -and externally- control the Minecraft player's orientation.

No APIs, no Java mods to the game environment

Typically this requires the movement of the mouse, but every single mouse movement simulating python3 library that I've tried doesn't move the player's head in-game. Each library does something different, too.

For example, pyautogui doesn't do anything until you move the mouse manually after the script has finished. Doing this will jerk the player's view to where the program supposedly moved it to, before continuing to follow your current mouse movements. This happens for both mouse commands.

import pyautogui

pyautogui.moveTo(500, 500)
pyautogui.moveRel(100, 100)  

The pynput library had the same weird result as pyautogui:

from pynput.mouse import Controller

mouse = Controller()

mouse.position = (100, 200)
mouse.move(200, -100)

Quartz doesn't do anything at all:

import Quartz

class Mouse():
    down = [Quartz.kCGEventLeftMouseDown, Quartz.kCGEventRightMouseDown, Quartz.kCGEventOtherMouseDown]
    up = [Quartz.kCGEventLeftMouseUp, Quartz.kCGEventRightMouseUp, Quartz.kCGEventOtherMouseUp]
    [LEFT, RIGHT, OTHER] = [0, 1, 2]

    def click_pos(self, x, y, button=LEFT):
        self.move(x, y)
        self.click(button)

    def to_relative(self, x, y):
        curr_pos = Quartz.CGEventGetLocation( Quartz.CGEventCreate(None) )
        x += curr_pos.x;
        y += curr_pos.y;


mouse = Mouse()
mouse.to_relative(200, 200)

And the python mouse library is outdated: the error showed that it will only run on Darwin (I'm on macOS High Sierra). I was sad on learning this because of the description on the Github page. It says "Global event hook on all mice devices (captures events regardless of focus)". I then thought that, somehow, Minecraft was sucking up all the simulated mouse movements on it's own. Either way, I'm not using the right interface for this game, and I need something that can bypass Minecraft's interesting mouse controls to get the movement that I want.

I even tried using mouse keys (mac's mouse-moving accessibility feature that lets you control the mouse with only keys) along with pyautogui.

import pyautogui # with mouse keys on
import time
# mouse keys is an accessibility feature on mac that controls the mouse with the keyboard

print("[ALERT]: Make sure mouse keys is on! (press option 5 times if shortcut is enabled)")

pyautogui.keyDown('8')  # up in mouse keys
time.sleep(5)
pyautogui.keyUp('8')

I wasn't particularly surprised that the last one didn't work, but I think I'm running out of ways to try and bypass whatever is making Minecraft not take my python-mouse input. At this point, I'm pretty sure that there must be some distinction in the kind of input that I'm giving the computer. Minecraft as a program doesn't use the mouse like other programs do, and python mice don't control the mouse like other sources do, so there is a disconnect.

I'm on my macOS High Sierra running Minecraft in both fullscreen and windowed mode, trying everything I can to get this to function properly. I'll start the test script (python 3.6) in PyCharm, change windows (or window focus) to Minecraft (with adequate delay time in-program), and then witness what happens. Mouse clicking, keyboard presses, and even hotkeys that involve the command and escape keys all work fine in Minecraft with pyautogui, so I'm not worried about those at all. It's literally just the mouse movement that's not doing anything.

First of all, is this the right place to ask this question? Is there anything else to try, or is there something crucial that I'm missing, that would allow my mouse input to be responded to correctly?

like image 518
pizzafoot Avatar asked Jun 06 '18 04:06

pizzafoot


People also ask

Can Python control the mouse?

In order to control a mouse using Python, you may use the PyAutoGUI library. Next, you'll see the following 4 scenarios which demonstrate how to control a mouse using Python by: Moving a mouse cursor to a specific location. Clicking on a specific location.

How do I make my mouse move automatically in Python?

This code uses moveTo() function, which takes x and y coordinates, and an optional duration argument. This function moves your mouse pointer from it's current location to x, y coordinate, and takes time as specified by duration argument to do so.


2 Answers

I am trying to do the same thing, and I got mine to move the view in Minecraft (Java edition).

What worked for me was to use pynput with relative mouse commands. It also needed 'Raw Input' to be off in the Minecraft settings. [Esc -> Options... -> Controls... -> Mouse Settings... -> Raw input: OFF]

import pynput
mouse = pynput.mouse.Controller()
mouse.move(10, 10)

Also, here's the beginnings to a smooth movement of the mouse if anyone wants it:

def move_smooth(xm, ym, t):
    for i in range(t):
        if i < t/2:
            h = i
        else:
            h = t - i
        mouse.move(h*xm, h*ym)
        time.sleep(1/60)

move_smooth(2, 2, 40)

Now, onto trying to make the keyboard work :P

like image 122
Emerson Peters Avatar answered Sep 20 '22 08:09

Emerson Peters


I am in a similar situation to you. I was also unable to find a way to register my mouse movements in games such as minecraft.

However, I learned that you can use Java and the built-in robot library to achieve the mouse movement as desired. I don't know if you are set on python but if not it's worth checking out.

like image 43
Reid Haegele Avatar answered Sep 20 '22 08:09

Reid Haegele