Paulo Souza Paulo Souza - 4 months ago 26
Linux Question

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

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

For Windows exist pyHook library but I dont know how to do it in Linux.

Answer

You can use 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