pyHook模块可从这里下载
http://www.lfd.uci.edu/~gohlke/pythonlibs/示例代码如下:
def mouse_coordinates():
""" 获取鼠标坐标 """
import os,time
import pyautogui as pag
try:
while True:
print ("Press Ctrl-C to end")
x,y = pag.position() #返回鼠标的坐标
posStr="Position:"+str(x).rjust(4)+','+str(y).rjust(4)
print (posStr) #打印坐标
time.sleep(0.2)
os.system('cls') #清除屏幕
except KeyboardInterrupt:
print ('end....')
def keystroke():
""" 按键操作 """
from pymouse import PyMouse
from pykeyboard import PyKeyboard
m = PyMouse()
k = PyKeyboard()
x,y=1580,1025 # 鼠标坐标
m.click(x,y,1,1) # 左键点击一次
k.type_string('hello') # 输入hello
time.sleep(0.1)
k.tap_key(k.enter_key,1) # 回车
x,y=260,38
m.click(x,y,1,1) # 左击另一个坐标一次
def write_window(ct_name=None):
""" 向窗体的文本框写入字符串,
执行此方法需要传入窗口句柄或者打开一个记事本 """
from ctypes import windll as win32
WM_CHAR = 0x0102
ct_name = ct_name if ct_name else 'Notepad'
try:
hWnd = win32.user32.FindWindowW(ct_name, None)
assert hWnd
hEdit = win32.user32.FindWindowExW(hWnd, None, 'Edit', None)
assert hEdit
except AssertionError:
print('Notepad not found')
else:
for char in '我爱你, 世界':
win32.user32.SendMessageW(hEdit, WM_CHAR, ord(char), None)
# 回车
win32gui.PostMessage(hEdit, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
win32gui.PostMessage(hEdit, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
def get_window():
""" 获取所有Windows打开的窗体句柄 """
from win32gui import IsWindow,IsWindowEnabled,IsWindowVisible,GetWindowText,EnumWindows
titles = set()
def foo(hwnd, mouse):
# 去掉下面这句判断就能获取所有
if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
titles.add(GetWindowText(hwnd))
EnumWindows(foo, 0)
return titles