python - 如何使用 Python 控制 Minecraft 中的鼠标?

标签 python macos mouse minecraft pyautogui

总而言之,我正在尝试以编程方式和外部方式控制 Minecraft 玩家的方向。

没有 API,游戏环境没有 Java 模组

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

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

import pyautogui

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

pynput 库和 pyautogui 有同样奇怪的结果:

from pynput.mouse import Controller

mouse = Controller()

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

Quartz 什么都不做:

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 上的描述,我很难过学习这个页。它说“所有鼠标设备上的全局事件 Hook (无论焦点如何捕获事件)”。然后我想,不知何故,Minecraft 自己吸收了所有模拟的鼠标移动。无论哪种方式,我都没有为这个游戏使用正确的界面,我需要一些可以绕过 Minecraft 有趣的鼠标控制的东西来获得我想要的移动。

我什至尝试使用 鼠标键(mac 的鼠标移动辅助功能,让您只用键控制鼠标)和 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 鼠标不像其他来源那样控制鼠标,因此存在断开连接。

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

首先,这里是问这个问题的正确地点吗?是否还有其他方法可以尝试,或者我是否遗漏了一些重要的东西,可以让我的鼠标输入得到正确响应?

最佳答案

我正在尝试做同样的事情,我得到了移动 Minecraft(Java 版)中的 View 。

对我有用的是使用带有相关鼠标命令的 pynput。它还需要在 Minecraft 设置中关闭“原始输入”。 [Esc -> 选项... -> 控制... -> 鼠标设置... -> 原始输入:关闭]

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

此外,如果有人需要的话,这里是鼠标平滑移动的开始:

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

关于python - 如何使用 Python 控制 Minecraft 中的鼠标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50712139/

相关文章:

c++ - Win32 中缺少鼠标移动消息

python - 使用Python从嵌套json字典获取信息的问题

python - Microsoft Graph API 委托(delegate)权限

python - 如何使用 Python 在 Mac 中控制鼠标?

c - 我如何实际使用授权服务?

javascript - 这种 Safari 行为是否违反了 ECMAScript 规范?

javascript - 使用 JavaScript 以编程方式将光标从一个页面 move 到另一页面

python - 如何处理python系列中的多种日期字符串格式

python - 带有词袋的文本分类器和 sklearn 中的附加情感特征

c++ - 如何在 Xcode C++ 中更改编译器