繁体   English   中英

如何在Linux中捕获按键(密钥记录)?

[英]How can I capture a key press (key logging) in Linux?

如何在Linux中捕获按键(密钥记录)?

对于Windows存在pyHook库,但我不知道如何在Linux中执行此操作。

你可以使用pyxhook:

#!/usr/bin/env python

import pyxhook

def OnKeyPress(event):
    print (event.Key)


    if event.Ascii == 32:
        exit(0)

hm = pyxhook.HookManager()
hm.KeyDown = OnKeyPress

hm.HookKeyboard()

hm.start()

sudo apt-get install python-xlib https://github.com/JeffHoogland/pyxhook

#!/usr/bin/env python    
import pyxhook
import time

#This function is called every time a key is presssed
def kbevent( event ):
   #print key info
    print event

#If the ascii value matches spacebar, terminate the while loop
if event.Ascii == 32:
    global running
    running = False

#Create hookmanager
hookman = pyxhook.HookManager()
#Define our callback to fire when a key is pressed down
hookman.KeyDown = kbevent
#Hook the keyboard
hookman.HookKeyboard()
#Start our listener
hookman.start()

#Create a loop to keep the application running
running = True
while running:
time.sleep(0.1)

#Close the listener when we are done
hookman.cancel()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM