Python監聽模擬鼠標鍵盤

1.監聽鼠標和鍵盤的輸入

from pynput import keyboard,mouse

def on_press(key):
       print('alphanumeric key {0} pressed'.format(key.char))

def on_release(key):
    print('{0} released'.format(key))
    if key == keyboard.Key.esc:# Stop listener
        return False

def on_move(x, y):
    print('Pointer moved to {0}'.format((x, y)))

def on_click(x, y, button, pressed):
    print('{0} at {1}'.format('Pressed' if pressed else 'Released',(x, y)))

def on_scroll(x, y, dx, dy):
    print('Scrolled {0} at {1}'.format('down' if dy < 0 else 'up',(x, y)))

# Collect events until released

keyboard_listener=keyboard.Listener(on_press=on_press,on_release=on_release)
mouse_listener=mouse.Listener(on_click=on_click,on_move=on_move,on_scroll=on_scroll)
lst=[keyboard_listener,mouse_listener]

for t in lst:t.start()

for t in lst:t.join()

另一種方式

import PyHook3

def OnMouseEvent(event):
  print('MessageName:',event.MessageName)
  print('Message:',event.Message)
  print('Time:',event.Time)
  print('Window:',event.Window)
  print('WindowName:',event.WindowName)
  print('Position:',event.Position)
  print('Wheel:',event.Wheel)
  print('Injected:',event.Injected)
  print('---')

  # return True to pass the event to other handlers
  # return False to stop the event from propagating
  return True

def OnKeyboardEvent(event):
  print('MessageName:',event.MessageName)
  print('Message:',event.Message)
  print('Time:',event.Time)
  print('Window:',event.Window)
  print('WindowName:',event.WindowName)
  print('Ascii:', event.Ascii, chr(event.Ascii))
  print('Key:', event.Key)
  print('KeyID:', event.KeyID)
  print('ScanCode:', event.ScanCode)
  print('Extended:', event.Extended)
  print('Injected:', event.Injected)
  print('Alt', event.Alt)
  print('Transition', event.Transition)
  print('---')

  # return True to pass the event to other handlers
  # return False to stop the event from propagating
  return True

# create the hook mananger
hm = PyHook3.HookManager()
# register two callbacks
hm.MouseAllButtonsDown = OnMouseEvent
hm.MouseWheel = OnMouseEvent
hm.KeyDown = OnKeyboardEvent

# hook into the mouse and keyboard events
hm.HookMouse()
hm.HookKeyboard()

if __name__ == '__main__':
  import pythoncom
  pythoncom.PumpMessages()

2.模擬鍵盤的輸入

1.代碼
import win32api
import win32con
win32api.keybd_event(17,0,0,0)  #ctrl鍵位碼是17
win32api.keybd_event(86,0,0,0)  #v鍵位碼是86
win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0) #釋放按鍵
win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)

2.鍵值對應表
字母和數字鍵     數字小鍵盤的鍵       功能鍵         其它鍵 
      鍵   鍵碼      鍵   鍵碼          鍵   鍵碼       鍵      鍵碼 
      A   65          0   96            F1   112       Backspace    8 
      B   66          1   97            F2   113       Tab       9 
      C   67          2   98            F3   114       Clear      12 
      D   68          3   99            F4   115       Enter      13 
      E   69          4   100           F5   116      Shift      16 
      F   70          5   101           F6   117      Control     17 
      G   71         6   102           F7   118      Alt       18 
      H   72         7   103           F8   119      Caps Lock    20 
      I    73          8   104          F9   120      Esc       27 
      J    74          9   105          F10  121     Spacebar    32 
      K   75         *   106           F11  122      Page Up     33 
      L   76         +   107           F12  123      Page Down    34 
      M   77        Enter 108                          End       35 
      N   78         -   109                              Home      36 
      O   79         .   110                              Left Arrow   37 
      P   80         /   111                              Up Arrow    38 
      Q   81                                                Right Arrow   39 
      R   82                                                Down Arrow    40 
      S   83                                                Insert      45 
      T   84                                                Delete      46 
      U   85                                                Help       47 
      V   86                                                Num Lock     144   
      W  87                                                 win             91
      X   88      
      Y   89      
      Z   90      
      0   48      
      1   49      
      2   50       
      3   51       
      4   52       
      5   53       
      6   54       
      7   55       
      8   56       
      9   57

3.另一種方法

from pymouse import PyMouse
from pykeyboard import PyKeyboard

m = PyMouse()
k = PyKeyboard()



#鼠標操作: 
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鍵
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章