简体   繁体   English

win32api和python导致我的屏幕在移动鼠标时变黑

[英]win32api and python causing my screen to go black when it moves the mouse

For whatever reason the screen is going black after the second or third movement of the mouse. 无论出于何种原因,在鼠标第二或第三次移动后,屏幕都会变黑。 First is the function i use to move the mouse: 首先是我用来移动鼠标的功能:

import ctypes
import time

SendInput = ctypes.windll.user32.SendInput


def MoveMouse(x, y):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    x = int(x*(65536/ctypes.windll.user32.GetSystemMetrics(0))+1)
    y = int(y*(65536/ctypes.windll.user32.GetSystemMetrics(1))+1)
    ii_.mi = MouseInput(x, y, 0, 0x0001 | 0x8000, 1, ctypes.pointer(extra))
    x = Input(ctypes.c_ulong(0), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

this causes the screen to go black 这会导致屏幕变黑

import numpy as np
from PIL import ImageGrab
import cv2
import time
import win32api, win32con
from directkeys import PressKey,ReleaseKey, W, A, S, D, MoveMouse
from grabscreen import grab_screen
x_pad = 0
y_pad = 0
def left():
    PressKey(W)
    PressKey(A)
    #ReleaseKey(W)
    ReleaseKey(D)
    #ReleaseKey(A)
    time.sleep(.9)
    ReleaseKey(A)
def leftClick():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
    print ("Click.")
def mousePos(cord):
    win32api.SetCursorPos((x_pad + cord[0], y_pad + cord[1]))
def screen_record(): 
    last_time = time.time()
    while(True):
        # 800x600 windowed mode for GTA 5, at the top left position of your main screen.
        # 40 px accounts for title bar. 
        printscreen =  grab_screen(region=(0,40,800,640))
        #rgb_im = printscreen.convert('RGB')
        pixels =int(printscreen[300, 300, 0]) 
        print(pixels)
        #r, g, b = printscreen.getpixel((551, 350 ))
        if pixels == 255:
            #if r == 127and g == 26 and b == 25:
            x, y = win32api.GetCursorPos()
            #x += 44
            time.sleep(1)
            MoveMouse(x, y)
            time.sleep(1)
#


        #print (r, g, b)
screen_record()

Thanks any help would be appreciated. 谢谢任何帮助,将不胜感激。 To summarize i need help determining why the Move Mouse function is causing my entire desktop monitor to black out until the function is no longer being used. 总而言之,我需要帮助确定为什么“移动鼠标”功能会导致我的整个台式机显示器变黑,直到不再使用该功能为止。

Had the same issue, try changing in: 遇到相同的问题,请尝试更改:

ii_.mi = MouseInput(x, y, 0, 0x0001 | 0x8000, 1, ctypes.pointer(extra))

the fifth parameter (IE "1") to a 0, so it looks like this: 第五个参数(即“ 1”)为0,因此如下所示:

ii_.mi = MouseInput(x, y, 0, 0x0001 | 0x8000, 0, ctypes.pointer(extra))

Worked for me... Good luck 为我工作...祝你好运

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

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