dogtail自帶鼠標和鍵盤的操作方法,使用方法如下:
環境配置
安裝dogtail:
sudo pip3 install dogtail
導入包
dogtail中,控制鼠標鍵盤的包是在rawinput里面:
from dogtail import rawinput
點擊
rawinput.click(x, y, button=1, check=True)
1.x, y 為所要點擊的坐標
2.button為鼠標左右鍵,1代表左鍵,2代表滾輪,3代表右鍵,默認參數,可以不傳
3.check為坐標檢查,如果為負數,會拋異常,默認參數,可以不傳
雙擊
rawinput.doubleClick(x, y, button=1, check=True)
點擊(按住不釋放)
rawinput.press(x, y, button=1, check=True)
鼠標釋放
rawinput.release(x, y, button=1, check=True)
鼠標移動(絕對位置)
rawinput.absoluteMotion(x, y, mouseDelay=None, check=True)
1.x, y 為所要移動到的坐標
2.mouseDelay為鼠標移動的延遲時間,默認參數,可以不傳
3.check為坐標檢查,如果為負數,會拋異常,默認參數,可以不傳
鼠標移動(相對位置)
rawinput.relativeMotion(x, y, mouseDelay=None)
1.x, y 為所要移動的相對位置,比如從當前位置移動100,100,注意和絕對位置的區別
2.mouseDelay為鼠標移動的延遲時間,默認參數,可以不傳
拖拽
rawinput.drag(fromXY, toXY, button=1, check=True)
1.fromXY 起始位置的坐標(x, y )
2.toXY 目標位置的坐標(x, y )
這個拖拽實際為絕對位置的拖拽,rawinput沒有提供相對位置的拖拽,但是我們可以通過:
rawinput.press(x, y )
rawinput.relativeMotion(x, y)
rawinput.release(x, y)
進行二次封裝實現相對位置的拖拽。
輸入文本
rawinput.typeText(string)
傳入要輸入的字符串即可
注意: 不支持中文輸入,經過分析源碼,輸入的功能實際是遍歷字符串里面的每一個字符然后調用的pressKey,也就是敲鍵盤的方式實現輸入,所以中文輸入的bug無解。在UOS上使用這個方法輸入中文的時候,系統直接崩潰(注銷)。
點擊鍵盤
rawinput.pressKey(keyName)
keyName = {
'enter': 'Return',
'esc': 'Escape',
'alt': 'Alt_L',
'control': 'Control_L',
'ctrl': 'Control_L',
'shift': 'Shift_L',
'del': 'Delete',
'ins': 'Insert',
'pageup': 'Page_Up',
'pagedown': 'Page_Down',
' ': 'space',
'\t': 'Tab',
'\n': 'Return'
}
組合按鍵
rawinput.keyCombo(comboString)
comboString組合按鍵,比如:
'<Control><Alt>p'
'<Control><Shift>PageUp'
'<Control>q'