Python監(jiān)聽模擬鼠標(biāo)鍵盤

1.監(jiān)聽鼠標(biāo)和鍵盤的輸入

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.鍵值對應(yīng)表
字母和數(shù)字鍵     數(shù)字小鍵盤的鍵       功能鍵         其它鍵 
      鍵   鍵碼      鍵   鍵碼          鍵   鍵碼       鍵      鍵碼 
      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()



#鼠標(biāo)操作: 
m.click(x,y,button,n) 鼠標(biāo)點(diǎn)擊 
#x,y 是坐標(biāo)位置 
#buttong 1表示左鍵,2表示點(diǎn)擊右鍵 
#n 點(diǎn)擊次數(shù),默認(rèn)是1次,2表示雙擊

m.move(x,y) –鼠標(biāo)移動(dòng)到坐標(biāo)(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”) –模擬點(diǎn)擊H鍵 
k.tap_key(‘H’,n=2,interval=5) –模擬點(diǎn)擊H鍵,2次,每次間隔5秒 
k.tap_key(k.function_keys[5]) –點(diǎn)擊功能鍵F5 
k.tap_key(k.numpad_keys[5],3) –點(diǎn)擊小鍵盤5,3次

#聯(lián)合按鍵模擬 
#例如同時(shí)按alt+tab鍵盤 
k.press_key(k.alt_key) –按住alt鍵 
k.tap_key(k.tab_key) –點(diǎn)擊tab鍵 
k.release_key(k.alt_key) –松開alt鍵
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文轉(zhuǎn)自(原文太雜亂,這里調(diào)整了格式及內(nèi)容):http://enkichen.com/2018/09/12/osx...
    topws1閱讀 19,300評(píng)論 0 27
  • ??JavaScript 與 HTML 之間的交互是通過事件實(shí)現(xiàn)的。 ??事件,就是文檔或?yàn)g覽器窗口中發(fā)生的一些特...
    霜天曉閱讀 3,681評(píng)論 1 11
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,621評(píng)論 1 32
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,298評(píng)論 0 17
  • ORA-00001: 違反唯一約束條件 (.) 錯(cuò)誤說明:當(dāng)在唯一索引所對應(yīng)的列上鍵入重復(fù)值時(shí),會(huì)觸發(fā)此異常。 O...
    我想起個(gè)好名字閱讀 5,930評(píng)論 0 9

友情鏈接更多精彩內(nèi)容