用python在linux下模擬滑鼠
2012-02-09 14:41:22
分類: linux
需要安裝python-xlib庫
使用:1) 記錄滑鼠軌跡:
**:
python smartmouse.py -r
2) 重放滑鼠軌跡:
**:
python smartmouse.py -p
記錄檔案格式如下:
**:
6,8,sleep:2
6,8,click:1
6,8,sleep:3
315,4,click:1
315,4,sleep:2
800,662,click:3
800,662,sleep:2
780,662,click:1
前面兩個分別是螢幕的x和y座標,從第三個起是可選的,支援click, release, press 和 sleep
click, release, press 後邊跟「:」,「:」後面的數字 1 表示左鍵,2 是中鍵,3 是右鍵, 4 是向上滾滾輪, 5 是向下滾滾輪。
sleep後面數字表示在該座標點停留的時間,單位秒。
未完成部分,自動記錄滑鼠軌跡時候尚不能記錄滑鼠按鍵事件,有懂xlib的童鞋請提出建議或解決方法
原始碼:**:
#!/usr/bin/python
#-*- coding: utf-8 -*-
# name: smartmouse.py
# useage:
# 1) 記錄滑鼠軌跡
# python smartmouse.py -r
# 2) 重放滑鼠軌跡
# python smartmouse.py -p #
# coded by xiooli
# 2009.10.17
import xlib.display as ds
import xlib.x as x
import xlib.ext.xtest as xtest
class mouse():
'''mouse class which contains couple of mouse methods'''
def __init__(self):
self.display = ds.display()
def mouse_press(self,button):
'''button= 1 left, 2 middle, 3 right, 4 middle up, 5 middle down'''
xtest.fake_input(self.display,x.buttonpress, button)
self.display.sync()
def mouse_release(self,button):
'''button= 1 left, 2 middle, 3 right, 4 middle up, 5 middle down'''
xtest.fake_input(self.display,x.buttonrelease, button)
self.display.sync()
def mouse_click(self,button):
'''button= 1 left, 2 middle, 3 right, 4 middle up, 5 middle down'''
self.mouse_press(button)
self.mouse_release(button)
def goto_xy(self,x, y):
'''move to position x y'''
xtest.fake_input(self.display, x.motionnotify, x = x, y = y)
self.display.flush()
def pos(self):
'''get mouse position'''
coord = self.display.screen().root.query_pointer()._data
return (coord["root_x"],coord["root_y"])
def screen_size(self):
'''get screen size'''
width = self.display.screen().width_in_pixels
height = self.display.screen().height_in_pixels
return (width,height)
i=0def elapse():
'''get elapse time, gap is 0.1 second'''
global i
i+=0.1
return i
if __name__ == "__main__":
import sys
import time as tm
t=0m=mouse()
mouse_ev=""
ev=m=mouse()
evdic=
if sys.argv[1] == "-r":
try:
rctm=sys.argv[2]
except:
sys.exit(1)
try:
logfile=sys.argv[3]
except:
logfile="./mouse.log"
f=open(logfile,"w")
while t <= float(rctm):
t=elapse()
ps=m.pos()
f.write(str(ps[0])+","+str(ps[1])+"\n")
tm.sleep(0.1)
f.close()
elif sys.argv[1] == "-p":
try:
f=open(sys.argv[2])
except:
sys.exit(2)
coord=f.readlines()
f.close()
for pos_xy in coord:
try:
pos_x,pos_y=pos_xy.replace("\n","").split(",")
except valueerror:
pos_x,pos_y,mouse_ev=pos_xy.replace("\n","").split(",")
ev=mouse_ev.split(":")
m.goto_xy(int(pos_x),int(pos_y))
if ev and ev[1]:
if ev[0] != "sleep":
evdic[ev[0]](int(ev[1]))
else:
evdic[ev[0]](float(ev[1]))
ev=tm.sleep(0.1)
在linux環境下用python2操作mysql
有時候需要在伺服器上通過python指令碼運算元據庫,這時就有乙個問題,linux上預設安裝的python2.7,但是沒有操作mysql指令碼需要的相關模組。解決 wget 2 安裝pip 3 利用pip安裝運算元據庫的pymysql模組 安裝好了之後,就可以用python指令碼運算元據庫了。附上乙...
在Linux下安裝Python
在linux下安裝python的操作相當簡單,按如下步驟操作即可 2 解壓 root www.linuxidc.com python tar zxvf python 3.2.2.tgz 3 開啟安裝目錄,執行 root www.linuxidc.com python cd python 3.2.2 ...
在Linux下安裝Python
在linux下安裝python的操作相當簡單,按如下步驟操作即可 2 解壓 root www python tar zxvf python 3.2.2.tgz 3 開啟安裝目錄,執行 root www python cd python 3.2.2 root www python 3.2.2 conf...