用到的模塊
pyHook
一、PyUserInput安裝
python3.5的PyMouse和PyKeyboard模塊都集成到了PyUserInput模塊中。在python3.5中,直接安裝PyUserInput模塊即可
PyUserInput模塊安裝前需要安裝pywin32和pyHook模塊
pywin32模塊默認已安裝
pyHook模塊可從這裏下載
http://www.lfd.uci.edu/~gohlke/pythonlibs/
//在Python官網找了很多個pyHook都不適用於python3.5版本
PyUserInput模塊
https://github.com/PyUserInput/PyUserInput
二、使用方法
//導入模塊
import pymouse,pykeyboard,os,sys
from pymouse import *
from pykeyboard import PyKeyboard
- 1
- 2
- 3
- 1
- 2
- 3
//分別定義一個實例
m = PyMouse()
k = PyKeyboard()
鼠標操作:
# import the module
from pymouse import PyMouse
# instantiate an mouse object
m = PyMouse()
# move the mouse to int x and int y (these are absolute positions)
m.move(200, 200)
# click works about the same, except for int button possible values are 1: left, 2: right, 3: middle
m.click(500, 300, 1)
# get the screen size
m.screen_size()
# (1024, 768)
# get the mouse position
m.position()
# (500, 300)
m.click(x,y,button,n) –鼠標點擊
x,y –是座標位置
buttong –1表示左鍵,2表示點擊右鍵
n –點擊次數,默認是1次,2表示雙擊
m.move(x,y) –鼠標移動到座標(x,y)
x_dim, y_dim = m.screen_size() –獲得屏幕尺寸
鍵盤操作:
k.type_string(‘Hello, World!’) –模擬鍵盤輸入字符串
k.press_key(‘H’) –模擬鍵盤按H鍵
k.release_key(‘H’) –模擬鍵盤松開H鍵
k.tap_key(“H”) –模擬點擊H鍵
k.tap_key(‘H’,n=2,interval=5) –模擬點擊H鍵,2次,每次間隔5秒
k.tap_key(k.function_keys[5]) –點擊功能鍵F5
k.tap_key(k.numpad_keys[5],3) –點擊小鍵盤5,3次
聯合按鍵模擬
例如同時按alt+tab鍵盤
k.press_key(k.alt_key) –按住alt鍵
k.tap_key(k.tab_key) –點擊tab鍵
k.release_key(k.alt_key) –鬆開alt鍵
___________________________________________
keycode對照表
keycode 8 = BackSpace 回格
keycode 9 = Tab
keycode 12 = Clear
keycode 13 = Enter 回車
keycode 16 = Shift_L
keycode 17 = Control_L
keycode 18 = Alt_L
keycode 19 = Pause
keycode 20 = Caps_Lock
keycode 27 = Escape
keycode 32 = space
keycode 33 = Prior
keycode 34 = Next
keycode 35 = End
keycode 36 = Home
keycode 37 = Left
keycode 38 = Up
keycode 39 = Right
keycode 40 = Down
keycode 41 = Select
keycode 42 = Print
keycode 43 = Execute
keycode 45 = Insert
keycode 46 = Delete
keycode 47 = Help
keycode 48 = 0 equal braceright
keycode 49 = 1 exclam onesuperior
keycode 50 = 2 quotedbl twosuperior
keycode 51 = 3 section threesuperior
keycode 52 = 4 dollar
keycode 53 = 5 percent
keycode 54 = 6 ampersand
keycode 55 = 7 slash braceleft
keycode 56 = 8 parenleft bracketleft
keycode 57 = 9 parenright bracketright
keycode 65 = a A
keycode 66 = b B
keycode 67 = c C
keycode 68 = d D
keycode 69 = e E EuroSign
keycode 70 = f F
keycode 71 = g G
keycode 72 = h H
keycode 73 = i I
keycode 74 = j J
keycode 75 = k K
keycode 76 = l L
keycode 77 = m M mu
keycode 78 = n N
keycode 79 = o O
keycode 80 = p P
keycode 81 = q Q at
keycode 82 = r R
keycode 83 = s S
keycode 84 = t T
keycode 85 = u U
keycode 86 = v V
keycode 87 = w W