How to control the mouse in Minecraft using Python?

后端 未结 5 794
清酒与你
清酒与你 2021-02-06 14:11

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

Typical

相关标签:
5条回答
  • 2021-02-06 14:46

    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

    0 讨论(0)
  • 2021-02-06 14:47

    Go to https://www.learncodebygaming.com/blog/pyautogui-not-working-use-directinput.

    pydirectinput is basically pyautogui but it works with games, and uses newer winapi functions.

    0 讨论(0)
  • 2021-02-06 14:55

    I managed to make it work with the mouse library. Instead of using mouse.move(x,y,absolute,duration) I used mouse._os_mouse.move_to(x,y) and mouse._os_mouse.move_relative(x,y). Take into account that if you want a smooth effect you'll have to implement it yourself using something like time.sleep(s).

    0 讨论(0)
  • 2021-02-06 14:59

    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.

    0 讨论(0)
  • 2021-02-06 15:00

    Try running python as admin and run the game in windowed mode. Pyautogui should work then.

    0 讨论(0)
提交回复
热议问题