假設我有一個窗口,我想要在特定的x,y座標上模擬鼠標點擊 。我已經有了hwnd,但我不確定 如何構建lParam。我以前使用過SendMessage在按鈕等上點擊 ,但我知道他們的情況。 任何幫助將不勝感激。我也忍不住想知道我是否正確地做這件事。我的最終目標是在Skype主窗口(例如)上點擊某個 用戶。我用EnumChildWindows 查找所有主窗口的子項,但找不到正確的窗口。所以 想通了,我會嘗試使用座標'點擊'它。python win32模擬點擊
5
A
回答
7
感謝ctypes,您可以使用低級別的Windows API。請參見下面的例子(改編自東西我沒有測試它,但它應該沒問題)
import ctypes
MOUSEEVENTF_MOVE = 0x0001 # mouse move
MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
MOUSEEVENTF_MOVEABS = MOUSEEVENTF_MOVE + MOUSEEVENTF_ABSOLUTE
MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
MOUSEEVENTF_LEFTUP = 0x0004 # left button up
MOUSEEVENTF_CLICK = MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP
def click(x, y):
#move first
x = 65536L * x/ctypes.windll.user32.GetSystemMetrics(0) + 1
y = 65536L * y/ctypes.windll.user32.GetSystemMetrics(1) + 1
ctypes.windll.user32.mouse_event(MOUSEEVENTF_MOVEABS, x, y, 0, 0)
#then click
ctypes.windll.user32.mouse_event(MOUSEEVENTF_CLICK, 0, 0, 0, 0)
更新: 我沒有測試下面的代碼,但我認爲它應該幫助你寫的東西得到孩子的位置。然後你可以嘗試點擊正確的位置。
CHILD= None
def the_callback(child_hwnd, regex):
'''Pass to win32gui.EnumWindows() to check all the opened windows'''
if re.match(regex, win32gui.GetWindowText(child_hwnd)):
CHILD= child_hwnd
win32gui.EnumChildWindows(hwnd, the_callback, regex)
if CHILD:
(x_tl, y_tl, x_br, y_br) = win32gui.GetWindowRect(CHILD)
8
我覺得這對你很好,你可以直接使用或者將它導入到你的python程序中。
"""mousemacro.py defines the following functions:
click() -- calls left mouse click
hold() -- presses and holds left mouse button
release() -- releases left mouse button
rightclick() -- calls right mouse click
righthold() -- calls right mouse hold
rightrelease() -- calls right mouse release
middleclick() -- calls middle mouse click
middlehold() -- calls middle mouse hold
middlerelease() -- calls middle mouse release
move(x,y) -- moves mouse to x/y coordinates (in pixels)
getpos() -- returns mouse x/y coordinates (in pixels)
slide(x,y) -- slides mouse to x/y coodinates (in pixels)
also supports optional speed='slow', speed='fast'
"""
from ctypes import*
from ctypes.wintypes import *
from time import sleep
import win32ui
__all__ = ['click', 'hold', 'release', 'rightclick', 'righthold', 'rightrelease', 'middleclick', 'middlehold', 'middlerelease', 'move', 'slide', 'getpos']
# START SENDINPUT TYPE DECLARATIONS
PUL = POINTER(c_ulong)
class KeyBdInput(Structure):
_fields_ = [("wVk", c_ushort),
("wScan", c_ushort),
("dwFlags", c_ulong),
("time", c_ulong),
("dwExtraInfo", PUL)]
class HardwareInput(Structure):
_fields_ = [("uMsg", c_ulong),
("wParamL", c_short),
("wParamH", c_ushort)]
class MouseInput(Structure):
_fields_ = [("dx", c_long),
("dy", c_long),
("mouseData", c_ulong),
("dwFlags", c_ulong),
("time",c_ulong),
("dwExtraInfo", PUL)]
class Input_I(Union):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("hi", HardwareInput)]
class Input(Structure):
_fields_ = [("type", c_ulong),
("ii", Input_I)]
class POINT(Structure):
_fields_ = [("x", c_ulong),
("y", c_ulong)]
# END SENDINPUT TYPE DECLARATIONS
# LEFTDOWN = 0x00000002,
# LEFTUP = 0x00000004,
# MIDDLEDOWN = 0x00000020,
# MIDDLEUP = 0x00000040,
# MOVE = 0x00000001,
# ABSOLUTE = 0x00008000,
# RIGHTDOWN = 0x00000008,
# RIGHTUP = 0x00000010
MIDDLEDOWN = 0x00000020
MIDDLEUP = 0x00000040
MOVE = 0x00000001
ABSOLUTE = 0x00008000
RIGHTDOWN = 0x00000008
RIGHTUP = 0x00000010
FInputs = Input * 2
extra = c_ulong(0)
click = Input_I()
click.mi = MouseInput(0, 0, 0, 2, 0, pointer(extra))
release = Input_I()
release.mi = MouseInput(0, 0, 0, 4, 0, pointer(extra))
x = FInputs((0, click), (0, release))
#user32.SendInput(2, pointer(x), sizeof(x[0])) CLICK & RELEASE
x2 = FInputs((0, click))
#user32.SendInput(2, pointer(x2), sizeof(x2[0])) CLICK & HOLD
x3 = FInputs((0, release))
#user32.SendInput(2, pointer(x3), sizeof(x3[0])) RELEASE HOLD
def move(x,y):
windll.user32.SetCursorPos(x,y)
def getpos():
global pt
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return pt.x, pt.y
def slide(a,b,speed=0):
while True:
if speed == 'slow':
sleep(0.005)
Tspeed = 2
if speed == 'fast':
sleep(0.001)
Tspeed = 5
if speed == 0:
sleep(0.001)
Tspeed = 3
x = getpos()[0]
y = getpos()[1]
if abs(x-a) < 5:
if abs(y-b) < 5:
break
if a < x:
x -= Tspeed
if a > x:
x += Tspeed
if b < y:
y -= Tspeed
if b > y:
y += Tspeed
move(x,y)
def click():
windll.user32.SendInput(2,pointer(x),sizeof(x[0]))
def hold():
windll.user32.SendInput(2, pointer(x2), sizeof(x2[0]))
def release():
windll.user32.SendInput(2, pointer(x3), sizeof(x3[0]))
def rightclick():
windll.user32.mouse_event(RIGHTDOWN,0,0,0,0)
windll.user32.mouse_event(RIGHTUP,0,0,0,0)
def righthold():
windll.user32.mouse_event(RIGHTDOWN,0,0,0,0)
def rightrelease():
windll.user32.mouse_event(RIGHTUP,0,0,0,0)
def middleclick():
windll.user32.mouse_event(MIDDLEDOWN,0,0,0,0)
windll.user32.mouse_event(MIDDLEUP,0,0,0,0)
def middlehold():
windll.user32.mouse_event(MIDDLEDOWN,0,0,0,0)
def middlerelease():
windll.user32.mouse_event(MIDDLEUP,0,0,0,0)
if __name__ == '__main__':
while 1:
move(10,1)
+0
感謝看起來不錯 – nabizan 2011-05-07 12:36:39
+0
這完全是真棒!謝謝。 – IronManMark20 2015-04-13 23:10:33
相關問題
- 1. win32:模擬點擊而不模擬鼠標移動?
- 2. 模擬點擊不點擊
- 3. Linux - 用Python模擬鼠標點擊
- 4. 模擬Python上的鼠標點擊
- 5. 模擬點擊python中的JavaScript鏈接
- 6. 在Python中模擬按鈕點擊
- 7. 模擬tabhost點擊
- 8. WebView模擬點擊
- 9. 模擬點擊js
- 10. 模擬點擊酶
- 11. iPhone:UnitTest模擬點擊
- 12. 模擬點擊 - 酶
- 13. 模擬點擊web.whatsapp.com
- 14. 模擬點擊Chromebook
- 15. 通過模擬點擊
- 16. 模擬javascript點擊來點擊橫幅
- 17. 模擬標籤點擊按鈕點擊
- 18. 右鍵單擊模擬python
- 19. 模擬點擊按鈕
- 20. 模擬QA的Gmail點擊
- 21. 標籤錨模擬點擊
- 22. 模擬點擊x座標
- 23. 模擬點擊Geckofx vb,網
- 24. 模擬GearVR上的點擊
- 25. 模擬菜單項點擊
- 26. 使用jQuery模擬點擊
- 27. 在StageWebView中模擬點擊
- 28. 模擬鼠標點擊QWebEngineView
- 29. 模擬按鈕點擊
- 30. 模擬webrowser中的點擊
嗨,感謝您的快速回復。順便說一句,你可以說你有主窗口和一堆他的孩子窗戶也有他們的hwnds hwnd ...你知道我怎麼能找到一些特定的文字內容acrosss所有這些孩子?並確定正確的一個,所以我可以點擊這個文本(鏈接)? ... – nabizan 2010-06-03 08:11:15
@nabizan:看到我的更新。我希望它可以幫助 – luc 2010-06-03 12:03:20
嗯,如果我錯了,但是* .GetWindowText只返回窗口的標題。 這對我來說是新東西,我不知道它究竟是如何工作,但我認爲,只要你打開一個複雜的應用程序窗口(例如Photoshop),你會得到主HWWD和一堆兒童窗戶(他們沒有一個標題)所以起初你必須找到一個特定的子窗口,例如運行一些隨機生成的鏈接,然後你可以點擊它...我不知道它是否正確的方式即時失明 – nabizan 2010-06-03 14:40:39