pynput使用简单说明

时间:2022-02-07 12:26:30

控制鼠标

 1 from  pynput.mouse import Button, Controller
 2 import time 
 3 
 4 mouse = Controller()
 5 print(mouse.position)
 6 time.sleep(3)
 7 print('The current pointer position is {0}'.format(mouse.position))
 8 
 9 
10 #set pointer positon
11 mouse.position = (277, 645)
12 print('now we have moved it to {0}'.format(mouse.position))
13 
14 #鼠标移动(x,y)个距离
15 #param int x: The horizontal offset.
16 #param int dy: The vertical offset.
17 mouse.move(5, -5)
18 print(mouse.position)
19 
20 mouse.press(Button.left)
21 mouse.release(Button.left)
22 
23 mouse.press(Button.right)
24 mouse.release(Button.right)
25 
26 #Double click
27 #param int count: The number of clicks to send.
28 mouse.click(Button.left, 2)
29 
30 #scroll two     steps down
31 #param int dx: The horizontal scroll. 
32 #param int dy: The vertical scroll.
33 mouse.scroll(0, 500)

监听鼠标

 1 '''
 2 :param callable on_move: The callback to call when mouse move events occur.
 3 
 4         It will be called with the arguments ``(x, y)``, which is the new
 5         pointer position. If this callback raises :class:`StopException` or
 6         returns ``False``, the listener is stopped.
 7 
 8     :param callable on_click: The callback to call when a mouse button is
 9         clicked.
10 
11         It will be called with the arguments ``(x, y, button, pressed)``,
12         where ``(x, y)`` is the new pointer position, ``button`` is one of the
13         :class:`Button` values and ``pressed`` is whether the button was
14         pressed.
15 
16         If this callback raises :class:`StopException` or returns ``False``,
17         the listener is stopped.
18 
19     :param callable on_scroll: The callback to call when mouse scroll
20         events occur.
21 
22         It will be called with the arguments ``(x, y, dx, dy)``, where
23         ``(x, y)`` is the new pointer position, and ``(dx, dy)`` is the scroll
24         vector.
25 
26         If this callback raises :class:`StopException` or returns ``False``,
27         the listener is stopped.
28 
29     :param bool suppress: Whether to suppress events. Setting this to ``True``
30         will prevent the input events from being passed to the rest of the
31         system.
32 '''
33 
34 from pynput import mouse
35 from  pynput.mouse import Button
36 
37 def on_move(x, y):
38     print('Pointer moved to {o}'.format((x,y)))
39 
40 def on_click(x, y , button, pressed):
41     button_name = ''
42     #print(button)
43     if button == Button.left:
44         button_name = 'Left Button'
45     elif button == Button.middle:
46         button_name = 'Middle Button'
47     elif button == Button.right:
48         button_name = 'Right Button'
49     else:
50         button_name = 'Unknown'
51     if pressed:
52         print('{0} Pressed at {1} at {2}'.format(button_name, x, y))
53     else:
54         print('{0} Released at {1} at {2}'.format(button_name, x, y))
55     if not pressed:
56         return False
57 
58 def on_scroll(x, y ,dx, dy):
59     print('scrolled {0} at {1}'.format(
60         'down' if dy < 0 else 'up',
61         (x, y)))
62 
63 while True:
64     with mouse.Listener( no_move = on_move,on_click = on_click,on_scroll = on_scroll,suppress = False) as listener:
65         listener.join()

控制键盘

 1 '''
 2 ['alt', 'alt_l', 'alt_r', 'backspace', 'caps_lock', 'cmd', 'cmd_r', 'ctrl', 'ctrl_l', 'ctrl_r', 'delete', 
 3 'down', 'end', 'enter', 'esc', 'f1', 'f10', 'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20', 
 4 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'home', 'insert', 'left', 'menu', 'num_lock', 'page_down', 'page_up', 'pause',
 5 'print_screen', 'right', 'scroll_lock', 'shift', 'shift_r', 'space', 'tab', 'up']
 6 '''
 7 
 8 from pynput.keyboard import Key, Controller
 9 
10 keyboard = Controller()
11 
12 #Press and release space
13 keyboard.press(Key.space)
14 keyboard.release(Key.space)
15 
16 #Type a lower case A ;this will work even if no key on the physical keyboard  is labelled 'A'
17 keyboard.press('a')
18 keyboard.release('a')
19 
20 #Type two  upper case As
21 keyboard.press('A')
22 keyboard.release('A')
23 # or 
24 #Executes a block with some keys pressed.    param keys: The keys to keep pressed.
25 with keyboard.pressed(Key.shift):    #组合按键
26     keyboard.press('a')
27     keyboard.release('a')
28 
29 #type 'hello world '  using the shortcut type  method
30 #This method will send all key presses and releases necessary to type all characters in the string.
31 #param str string: The string to type.
32 keyboard.type('hello world')
33 
34 keyboard.touch('&', True)
35 keyboard.touch('&', False)
36     
37 keyboard.press(Key.print_screen)
38 keyboard.release(Key.print_screen)
39 
40 with keyboard.pressed(Key.ctrl):    #组合按键
41     keyboard.press('s')
42     keyboard.release('s')

监听键盘

 1 from pynput import keyboard
 2 
 3 #alt_pressed、alt_gr_pressed、ctrl_pressed、shift_pressed
 4 
 5 
 6 def on_press(key):
 7     try:
 8         print('alphanumeric key     {0} pressed'.format(key.char))    #应该记录下之前有没有ctrl、alt、和shift按下
 9     except AttributeError:
10         print('special key {0} pressed'.format(key))
11 
12 def on_release(key):
13     print('{0} released'.format(key))
14     if key == keyboard.Key.esc:
15         return False
16 
17 while True:
18     with keyboard.Listener(
19         on_press = on_press,
20         on_release = on_release,
21         suppress = False) as listener:
22         listener.join()