Python uses pyhook for keyboard monitoring.
Pyhook download: http://sourceforge.net/projects/pyhook/files/pyhook/1.5.1/
PyhookAPI reference: http://pyhook.sourceforge.net/doc_1.5.0/
The above website provides several examples. In addition, after installing pyhooks, there will also be an example file. So I learned about it. During the first running, I was prompted that pywin32 was installed without the pythoncom module. After installation, it can run normally, but it will cause the machine to issue a card, especially after the program is interrupted, the mouse will shake freely for a period of time. After finding the cause for half a day, the main reason is that the event frequency is too high, and the program will often be stuck in pythoncom. pumpMessages ().
I searched the internet for half a day and saw a Post saying it was pythoncom. pumpMessages (n), n indicates the delay time, so I tried to change it and found it had some effect, but it was not obvious. Then I wondered if the program was not terminated because, the program is always stuck, so the program termination statement win32api is added. postQuitMessage (). The results are satisfactory.
#-*-Coding: cp936-*-import pythoncom import pyHook import timeimport win32apit = ''asciistr ='' keystr = ''def onKeyboardEvent (event): global t, asciistr, keystr filename = 'd: // test.txt 'wrfile = open (filename, 'AB') "handle Keyboard Events" if t = str (event. windowName): asciistr = asciistr + chr (event. ascii) keystr = keystr + str (event. key) else: t = str (event. windowName) if asciistr = ''and keystr ='': wrfile. writelines ("\ nWindow: % s \ n" % str (event. window) wrfile. writelines ("WindowName: % s \ n" % str (event. windowName) # write the name of the current form wrfile. writelines ("MessageName: % s \ n" % str (event. messageName) wrfile. writelines ("Message: % d \ n" % event. message) wrfile. writelines ("Time: % s \ n" % time. strftime ('% Y-% m-% d % H: % M: % s', time. localtime () else: wrfile. writelines ("Ascii_char: % s \ n" % asciistr) wrfile. writelines ("Key_char: % s \ n" % keystr) wrfile. writelines ("\ nWindow: % s \ n" % str (event. window) wrfile. writelines ("WindowName: % s \ n" % str (event. windowName) # write the name of the current form wrfile. writelines ("Time: % s \ n" % time. strftime ('% Y-% m-% d % H: % M: % s', time. localtime () asciistr = chr (event. ascii) keystr = str (event. key) if str (event. key) = 'f12': # terminate wrfile after F12 is pressed. writelines ("Ascii_char: % s \ n" % asciistr) wrfile. writelines ("Key_char: % s \ n" % keystr) wrfile. close () win32api. postQuitMessage () return True if _ name _ = "_ main _": # create a hook handle hm = pyHook. hookManager () # monitor the keyboard hm. keyDown = onKeyboardEvent hm. hookKeyboard () # obtain the message pythoncom cyclically. pumpMessages (10000)
In python, how does one perform keyboard listening? In some documents, "hooks" are used. How do third-party libraries pywin32 and PyHook use high-definition
I found you an ion ..
Oldj.net/article/python-hook/
How to simulate keyboard operations with Python
You can use the pywin module to call the win32 function.
Import win32api
Import win32con
Win32api. keybd_event (17,0,) # ctrl key bit code is 17
Win32api. keybd_event (,) # The v key bit code is 86.
Win32api. keybd_event (, win32con. KEYEVENTF_KEYUP, 0) # Release the button
Win32api. keybd_event (17,0, win32con. KEYEVENTF_KEYUP, 0)
Attached key bit code table:
Letter and number keys-key function keys for the numeric keypad-other keys
Key/key/Key
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 --... the remaining full text>