首页 > 解决方案 > GetAxis("Mouse X/Y") 不适用于虚拟鼠标

问题描述

我最近尝试在 python 中为统一游戏编写一个机器人,以使用 pyautogui 或 autohotkey 等虚拟鼠标来控制角色。游戏有多种相机模式。一个使用光标位置,另一个使用 GetAxis("Mouse X/Y")。在使用 GetAxis() 的选项上,我使用的库、pyautogui、pydirectinput 甚至 autohotkey 宏都无法移动鼠标,尽管它适用于使用光标位置的相机。为什么会这样?使用 GetAxis() 时失败的脚本示例:因为我还需要一个 ahk 示例,所以从另一个堆栈溢出帖子中引出:

CoordMode, mouse, screen

toggle := 0, fixedY := A_ScreenHeight/2 ; choose the y you like

F1::
MouseGetPos, MouseX, MouseY
if toggle := !toggle
 gosub, MoveTheMouse
else
 SetTimer, MoveTheMouse, off
return

MoveTheMouse:
Random, x, 1, % A_ScreenWidth
MouseMove, %x%, %fixedY%, 100
Random, Time, 1000*60, 1000*60*5
SetTimer, MoveTheMouse, -%time%  ; every 3 seconds 
return

用于移动的 C# 代码是:

case CAMERA_TYPE.CAMERA_THAT_WORKS_WITH_BOT:
                if (Input.mousePosition.x < (float)Screen.width * 0.4f)
                {
                    mainT.RotateAround(mainT.position, Vector3.up, -(((float)Screen.width * 0.4f - Input.mousePosition.x) / (float)Screen.width * 0.4f) * getSensitivityMultiWithDeltaTime() * 150f);
                }
                else if (Input.mousePosition.x > (float)Screen.width * 0.6f)
                {
                    mainT.RotateAround(mainT.position, Vector3.up, (Input.mousePosition.x - (float)Screen.width * 0.6f) / (float)Screen.width * 0.4f * getSensitivityMultiWithDeltaTime() * 150f);
                }
                mainT.rotation = Quaternion.Euler(140f * ((float)Screen.height * 0.6f - Input.mousePosition.y) / (float)Screen.height * 0.5f, mainT.rotation.eulerAngles.y, mainT.rotation.eulerAngles.z);
                mainT.position -= mainT.forward * this.distance * this.distanceMulti * this.distanceOffsetMulti;
                break;
            case CAMERA_TYPE.CAMERA_THAT_FAILS_WITH_BOT:
                if (!CustomInputs.Inputs.menuOn)
                {
                    Screen.lockCursor = true;
                }
                float num5 =0f;
                float num6 =0f;
                if (((int)GameManager.settings[300]) == 0)
                {
                    num5 = (Input.GetAxis("Mouse X") * 10f) * this.getSensitivityMulti();
                    num6 = ((-Input.GetAxis("Mouse Y") * 10f) * this.getSensitivityMulti()) * this.getReverse();
                }
                else if (((int)GameManager.settings[300]) == 1)
                {
                    num5 = (Input.GetAxisRaw("Mouse X") * 10f) * this.getSensitivityMulti();
                    num6 = ((-Input.GetAxisRaw("Mouse Y") * 10f) * this.getSensitivityMulti()) * this.getReverse();
                }

                mainT.RotateAround(mainT.position, Vector3.up, num5);
                float num7 = mainT.rotation.eulerAngles.x % 360f;
                float num8 = num7 + num6;
                if (((num6 <= 0f) || (((num7 >= 260f) || (num8 <= 260f)) && ((num7 >= 80f) || (num8 <= 80f)))) && ((num6 >= 0f) || (((num7 <= 280f) || (num8 >= 280f)) && ((num7 <= 100f) || (num8 >= 100f)))))
                {
                    mainT.RotateAround(mainT.position, mainT.right, num6);
                }
                mainT.position -= (Vector3)(((mainT.forward * this.distance) * this.distanceMulti) * this.distanceOffsetMulti);
                break;

由于被反编译,其中一些代码无法理解。另外请注意,不要担心这里的改装,因为该游戏基本上已被开发人员放弃了 4 年,现在在社区服务器上运行。

经过进一步搜索,我发现 GetAxis 可能使用鼠标速度/加速度,我认为这些鼠标移动功能不会以任何方式影响它。在我的用例中,我需要测试的是一种读取鼠标加速度/速度的方法和一种移动设置鼠标加速度/速度的方法。如果我在这里有正确的想法,请告诉我,任何与此相关的材料的参考将不胜感激。

标签: unity3dautohotkey

解决方案


我发现的解决方法是使用 windows dll 调用:AHK:

DllCall("mouse_event", "UInt", 0x01, "UInt", 100, "UInt", 100)

Python:

import ctypes
ctypes.windll.user32.mouse_event(0x01, x, y, 0, 0)

推荐阅读