如何使用Python在Minecraft中控制鼠标? [英] How to control the mouse in Minecraft using Python?

查看:178
本文介绍了如何使用Python在Minecraft中控制鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常这需要鼠标的移动,但是我尝试过的模拟python3库的每一次鼠标移动都不会在游戏中移动玩家的头部.每个库也做不同的事情.

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.

例如,pyautogui不会执行任何操作,直到脚本完成后手动移动鼠标为止.这样做会在继续跟随您当前的鼠标移动之前,将玩家的视线移到该程序应移至的位置.这两个鼠标命令都会发生这种情况.

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)  

Pynput库的结果与pyautogui相同:

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)

并且python鼠标库已过时:错误表明它将仅在Darwin上运行(我在macOS High Sierra上).由于 Github 页上的说明,我对此感到很难过.它说所有鼠标设备上的全局事件挂钩(捕获事件,而不管焦点如何)" .然后我以为,《我的世界》以某种方式独自吸收了所有模拟的鼠标移动.无论哪种方式,我都没有为此游戏使用正确的界面,并且我需要一些可以绕过Minecraft有趣的鼠标控件来获得所需动作的东西.

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.

我什至尝试使用鼠标键(mac的鼠标移动辅助功能,使您仅用键即可控制鼠标)以及pyautogui.

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')

我对最后一个不起作用并不感到特别惊讶,但是我认为我已经用尽各种方法来绕过任何使Minecraft不接受我的python鼠标输入的东西.在这一点上,我很确定在我提供给计算机的输入类型上必须有一些区别.作为一个程序,Minecraft不像其他程序那样使用鼠标,而python鼠标不像其他来源那样控制鼠标,因此存在断开连接的情况.

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.

我正在以全屏和窗口模式运行Minecraft的macOS High Sierra上,尽我所能使它正常运行.我将在PyCharm中启动测试脚本(python 3.6),将窗口(或窗口焦点)更改为Minecraft(在程序中具有足够的延迟时间),然后见证发生了什么.在带有pyautogui的Minecraft中,鼠标单击,键盘按键乃至涉及命令和转义键的热键都可以正常工作,因此我完全不担心这些.实际上,只是鼠标移动没有任何作用.

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?

推荐答案

我正在尝试做同样的事情,并且让我在Minecraft(Java版)中移动视图.

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

对我有用的是将pynput与相对的鼠标命令一起使用.还需要在Minecraft设置中关闭原始输入". [Esc->选项...->控件...->鼠标设置...->原始输入:OFF]

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)

现在,尝试使键盘工作:P

Now, onto trying to make the keyboard work :P

这篇关于如何使用Python在Minecraft中控制鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆