win32gui.GetCursorPos()返回位置比预期位置小80%

时间:2018-12-29 05:13:15

标签: python python-2.7 winapi

前段时间,我创建了一个小的Python脚本来将光标位置绑定到给定的bbox,这样,在玩效果不佳的游戏时,我的光标就不会在第二个监视器上浮动了。当我做到这一点时,我使用的是1080p显示器,但在那里没有任何问题,但是在最近将显示器换为1440p并更新了bbox之后,我发现返回的位置非常一致。

从win32gui.GetCursorPos()返回的每个职位都比我期望的要小80%。左上角按预期返回(0,0),第一个监视器的右下角是(2047,1151),第二个监视器的右下角是(4095,1151),比实际屏幕尺寸(两个屏幕的2560x1440p)小80% )。

源代码-

import time, win32api, win32con, win32gui

prev_len = 0 # Used for printr()

def printr( msg):
    '''Print that automatically resets previous message'''
    global prev_len
    print '\r', ' '*prev_len, '\r', msg,
    prev_len = len( msg)

def get_pos():
    return win32gui.GetCursorPos()

def set_pos(( x, y)):
    win32api.SetCursorPos( (int(x),int(y)))

def key_pressed( key):
    return win32api.GetAsyncKeyState( key)

def combo_pressed( key_li):
    for key in key_li:
        if key_pressed( key) == False:
            return False
    return True

def check_pos( pos, bbox):
    check_more, check_less = bbox
    x_pos, y_pos = pos
    if check_more[0] > x_pos:
        x_pos = check_more[0]
    elif x_pos > check_less[0]:
        x_pos = check_less[0]
    else:
        pass
    if check_more[1] > y_pos:
        y_pos = check_more[1]
    elif y_pos > check_less[1]:
        y_pos = check_less[1]
    else:
        pass
    return (x_pos, y_pos)

KEY_COMBO = [
    win32con.VK_LCONTROL,
    win32con.VK_LSHIFT,
    ord( 'P')]
SLEEP_TIME = 0.05

SCREEN_SIZE = (2560, 1440)
CENTER_SCREEN = tuple([ x/2 for x in SCREEN_SIZE])

SCREEN_BBOX = ((0,0),SCREEN_SIZE)

lock_mode = False # Toggle whether or not position is currently locked to bbox
allow_toggle = True # Make sure toggle doesn't happen multiple times per key combo

print "Payday 2 Utility to make sure cursor is locked onto primary screen during play.\n"
printr( "Lock Disabled.")

while True:
    time.sleep( SLEEP_TIME)
    if combo_pressed( KEY_COMBO) and allow_toggle:
        lock_mode = not lock_mode
        allow_toggle = False

        if lock_mode:
            printr( "Lock Enabled. Cursor bound to: (%s,%s)" % SCREEN_BBOX)
        else:
            printr( "Lock Disabled.")
    else:
        allow_toggle = True
    #printr( get_pos())
    if lock_mode:
        set_pos( check_pos( get_pos(), SCREEN_BBOX) )

我正在运行Windows 8.1 64位,上面是源代码,但是这些是唯一相关的位:

import win32gui while True: time.sleep( SLEEP_TIME) print win32gui.GetCursorPos()

当前返回的光标位置小于预期的位置。

0 个答案:

没有答案