python2实现对鼠标和键盘的监控

工作中因为测试需要,研究了在python2中使用pynput实现对鼠标和键盘的监控,根据pynput给出的接口,进一步封装了单击、双击等操作的实现,并写成了类接口

1.安装pynput可使用:

pip install pynput

2.类文件auto_test_input.py如下:

import pynput
from pynput import keyboard,mouse
from pynput.mouse import Button, Controller
import time
import threading



class Press(object):
    def __init__(self):
        self.x       = 0
        self.y       = 0
        self.button  = 0
        self.pressed = 0

    def press_set(self,x,y,button,pressed):
        self.x       = x
        self.y       = y
        self.button  = button
        self.pressed = pressed


class AutoTestInput(object):
    def __init__(self,on_keyboard,on_mouse_scroll,on_mouse_leftclick,
                        on_mouse_rightclick,on_mouse_doubleclick,on_mouse_move):
        self.on_keyboard          = on_keyboard
        self.on_mouse_scroll      = on_mouse_scroll
        self.on_mouse_leftclick   = on_mouse_leftclick
        self.on_mouse_doubleclick = on_mouse_doubleclick
        self.on_mouse_rightclick  = on_mouse_rightclick
        self.on_mouse_move        = on_mouse_move

        self.keyboard_listener = 0
        self.mouse_listener    = 0
        self.one_press         = Press()
        self.timer             = False
        self.thread_obj        = 0

    def on_release(self,key):
        self.on_keyboard(key)

    def on_scroll(self,x,y,dx,dy):
        self.on_mouse_scroll(x,y,dx,dy)


    def on_timeout(self,x,y):
        self.timer = False
        self.on_mouse_leftclick(x,y)

    def on_click(self,x,y,button,pressed):
        if pressed:
            self.one_press.press_set(x,y,button,pressed)
        if not pressed:
            if x == self.one_press.x and y == self.one_press.y:
                if button == Button.left:
                    if self.timer == False:
                        self.timer = threading.Timer(0.25,self.on_timeout,(x,y))
                        self.timer.start()
                    else:
                        self.timer.cancel()
                        self.timer = False
                        self.on_mouse_doubleclick(x,y)
                else:
                    self.on_mouse_rightclick(x,y)
            else:
                self.on_mouse_move(self.one_press.x,self.one_press.y,x,y)




    def thread_fun(self):
        self.keyboard_listener = keyboard.Listener(on_release=self.on_release)
        self.mouse_listener    = mouse.Listener(on_click=self.on_click,on_scroll=self.on_scroll)
        self.keyboard_listener.start()
        self.mouse_listener.start()
        self.keyboard_listener.join()
        self.mouse_listener.join()
        return



    def start(self):
        self.thread_obj = threading.Thread(target=self.thread_fun)
        self.thread_obj.start()
        return


    def stop(self):
        pynput.mouse.Listener.stop(self.mouse_listener)
        pynput.keyboard.Listener.stop(self.keyboard_listener)
        #self.thread_obj.join()
        return



3.使用示例如下:

import pynput
from pynput import keyboard,mouse
from pynput.mouse import Button, Controller
import time
import threading
from auto_test_input import AutoTestInput

testobj = 0

def on_keyboard(key):
    global testobj
    print key
    if key == keyboard.Key.esc:
        if testobj != 0:
            testobj.stop()

def on_mouse_scroll(x,y,dx,dy):
    print "scroll","position",x,y,"info",dx,dy

def on_mouse_leftclick(x,y):
    print "left click",x,y

def on_mouse_rightclick(x,y):
    print "right click",x,y

def on_mouse_doubleclick(x,y):
    print "double click",x,y

def on_mouse_move(x1,y1,x2,y2):
    print x1,y1,"->",x2,y2


if __name__ == '__main__':
    testobj = AutoTestInput(on_keyboard,on_mouse_scroll,on_mouse_leftclick,
                            on_mouse_rightclick,on_mouse_doubleclick,on_mouse_move)
    testobj.start()
    print "hello world"

测试结果如下:

E:\python>python test.py
hello world
left click 844 156
right click 843 155
double click 809 147
double click 837 17
807 158 -> 1114 292
scroll position 1114 292 info 0 1
scroll position 1114 292 info 0 -1
u'f'
u'f'
u'f'
u'f'
u'f'
Key.space
Key.space
Key.space
Key.delete
Key.delete
Key.delete
Key.esc

E:\python>

按esc可以退出,类start函数直接返回。

你可能感兴趣的:(python2实现对鼠标和键盘的监控)