简体   繁体   English

PyAutoGUI 似乎忽略了小键盘的按键操作

[英]PyAutoGUI seems to ignore numpad key presses

I'm using Windows, Python, and PyAutoGUI to try to automate some activities in Minecraft as a fun project.我正在使用 Windows、Python 和 PyAutoGUI 来尝试将 Minecraft 中的一些活动作为一个有趣的项目自动化。

I have been successful with using PyAutoGUI to switch to Minecraft once I start the script in Visual Studio Code, click on the "Back to Game" button, and then move the avatar forward by holding the "w" key.在 Visual Studio Code 中启动脚本后,我成功使用 PyAutoGUI 切换到 Minecraft,单击“返回游戏”按钮,然后按住“w”键向前移动头像。

I am using a 3rd party program called "NeatMouse" to use my numpad keys in place of using a mouse.我正在使用名为“NeatMouse”的第 3 方程序来使用我的小键盘键代替鼠标。 The numpad 8 button is equivalent to moving the mouse up, which in Minecraft causes your avatar to look up.小键盘 8 按钮相当于向上移动鼠标,这在 Minecraft 中会让您的头像向上看。 When I press this button myself in Minecraft, it works as expected, so it must be the case that NeatMouse is not the problem.当我自己在 Minecraft 中按下这个按钮时,它按预期工作,所以 NeatMouse 一定不是问题所在。

When I try to have PyAutoGUI replicate this same key press, it seems like nothing is happening.当我尝试让 PyAutoGUI 复制相同的按键时,似乎什么也没发生。

I have tried different combinations of我尝试了不同的组合

  • pag.press() pag.press()
  • pag.hold() pag.hold()
  • pag.keyDown() & pag.keyUp() pag.keyDown() & pag.keyUp()

These functions do work for the WASD keys, so I know that Minecraft is able to receive keyboard input from PyAutoGUI, so that generally must not be the problem.这些功能确实适用于 WASD 键,所以我知道 Minecraft 能够从 PyAutoGUI 接收键盘输入,所以这通常不是问题所在。

Here is a sample code block of what I have tried.这是我尝试过的示例代码块。

import pyautogui as pag
def align_vertical_facing_axis(target):
    facing = get_facing_axes()[1]
    
    # Looking down
    if facing > target:
        print('torture')
        pag.keyDown('num8')
        sleep(1)
        pag.keyUp('num8')

get_facing_axes() is a function I wrote to retrieve the axes the avatar is facing as a tuple from the Minecraft debug screen. get_facing_axes() 是我编写的 function ,用于从 Minecraft 调试屏幕中检索化身面向的轴作为元组。 A positive value in the [1] index means the character is looking at a downward angle. [1] 索引中的正值表示角色正在向下看。 When I run this script, it does print "torture" to my console so I know for sure it is entering that "if" block.当我运行这个脚本时,它确实会在我的控制台上打印“torture”,所以我确定它正在进入那个“if”块。

That was the long version of the explanation, the short version is: PyAutoGUI won't press the numpad keys, what do??那是解释的长版,短版是:PyAutoGUI 不会按小键盘键,怎么办??

After some research, pyautogui.platformModule contains the mappings for numpad.经过一些研究,pyautogui.platformModule 包含了数字键盘的映射。

Here are the windows key mappings: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes以下是 windows 键映射: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

import pyautogui as pag

#This is where you update key mappings, num8 is now referring to the virtual key code VK_NUMPAD8 or 0x68 

pag.platformModule.keyboardMapping.update({'num8':0x68}) 

#To call it, you use the mapping name you gave to the virtual key code
pag.press('num8')

Use the list I gave you for other key mapping values such as 0x68 for num8 and so on.使用我为您提供的其他键映射值的列表,例如 0x68 代表 num8 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM