微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pyautogui未在所需窗口中提供输出

如何解决pyautogui未在所需窗口中提供输出

因此,我编写了一个程序来移动minecraft中的播放器,以防止服务器将我标记为AFK。 该代码在其他应用程序中提供输出并模拟键盘。 但是,在minecraft中,它似乎没有提供任何形式的输出。 我怀疑这是因为我要切换到另一个窗口,但是它可以在其他应用程序(例如记事本)上运行。 我尝试了各种时间,它认为这不是问题 延迟是为了让我有时间切换到所需的窗口 这是我的代码

import pyautogui as gui
import time
wait_time = 10
def move_ingame():
    print("moving!")
    gui.press('space',presses=3,interval=1 )
def start_afk():
    print("Starting AFK in {} seconds. Make sure you have MC running and open.".format(wait_time))
    time.sleep(wait_time)
    while True:
        move_ingame()
        time.sleep(30)
def change_wait_time():
    print("Wait time is set to {} seconds. Enter value to change it to.".format(wait_time))
    change_val = input()
    if type(change_val) == int:
        wait_time = change_val
        print("Wait time has been changed to {}".format(wait_time))
        x1 = input("Press enter to go back to main_menu")
    else:
        print("Please enter a valid number. Only integers. No decimals allowed! Im lazy but ill change that soon ;)")
def load_page():
    print("Welcome to Franklin's  server AFK autobot")
    print("Type \"q\" and press enter to change initial wait time")
    init_input = input()
    if init_input == "Q" or init_input == "q":
        change_wait_time()
    else:
        start_afk()
load_page()

解决方法

这可能是因为普通输入无效,您需要直接输入。我有一个做这种事情的小模块,让我演示一下

directkeys.py:

import ctypes
import time

SendInput = ctypes.windll.user32.SendInput

W = 0x11
A = 0x1E
S = 0x1F
D = 0x20
M = 0x32
K = 0x25
SPACE = 0x39

# C struct redefinitions
PUL = ctypes.POINTER(ctypes.c_ulong)


class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk",ctypes.c_ushort),("wScan",("dwFlags",ctypes.c_ulong),("time",("dwExtraInfo",PUL)]


class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg",("wParamL",ctypes.c_short),("wParamH",ctypes.c_ushort)]


class MouseInput(ctypes.Structure):
    _fields_ = [("dx",ctypes.c_long),("dy",("mouseData",PUL)]


class Input_I(ctypes.Union):
    _fields_ = [("ki",KeyBdInput),("mi",MouseInput),("hi",HardwareInput)]


class Input(ctypes.Structure):
    _fields_ = [("type",("ii",Input_I)]


from ctypes import windll,Structure,c_long,byref


class POINT(Structure):
    _fields_ = [("x",c_long),("y",c_long)]


def queryMousePosition():
    pt = POINT()
    windll.user32.GetCursorPos(byref(pt))
    return pt
    # return { "x": pt.x,"y": pt.y}/





def click(x,y):
    # convert to ctypes pixels
    # x = int(x * 0.666)
    # y = int(y * 0.666)
    ctypes.windll.user32.SetCursorPos(x,y)
    ctypes.windll.user32.mouse_event(2,0)  # left down
    ctypes.windll.user32.mouse_event(4,0)  # left up


def moveMouseTo(x,y):
    # convert to ctypes pixels
    # x = int(x * 0.666)
    # y = int(y * 0.666)
    print(x,y)
    ctypes.windll.user32.SetCursorPos(x,y)
    # ctypes.windll.user32.mouse_event(2,0)  # left down
    # ctypes.windll.user32.mouse_event(4,0)  # left up


def PressKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput(0,hexKeyCode,0x0008,ctypes.pointer(extra))
    x = Input(ctypes.c_ulong(1),ii_)
    ctypes.windll.user32.SendInput(1,ctypes.pointer(x),ctypes.sizeof(x))


def ReleaseKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput(0,0x0008 | 0x0002,ii_)

现在,您只需从directkeys.py替换pyautogui函数即可。

让我给你举个例子:

from directkeys import PressKey,ReleaseKey,W
import time

def holdW():
    PressKey(W)
    time.sleep(5)
    ReleaseKey(W)

请注意,如果您按其他键,则需要搜索它们各自的键扫描代码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。