A simple key logger (Tkinter assisted)

vegaseat 3 Tallied Votes 2K Views Share

This simple key logger shows any character/special key when pressed.

''' tk_KeyLogger.py
show any character/special key when pressed
using Tkinter
'''

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def show_key(event):
    '''show event.keycode, event.keysym, event.char results'''
    if event.char == event.keysym:
        msg = 'Normal Key %r' % event.char
    elif len(event.char) == 1:
        msg = 'Punctuation Key %r (%r)' % (event.keysym, event.char)
    else:
        msg = 'Special Key %r' % event.keysym
    print(msg)
    if event.keysym == 'Escape':
        root.destroy()

root = tk.Tk()
print("Press a key (Escape key to exit): ")
root.bind_all('<Key>', show_key)
# don't show the tk window
root.withdraw()
root.mainloop()
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

And the purpose of this is? Keyloggers can be used to build a remote-control support application (I have done so in the past, and that is what VNC and other RC applications do), or it can be used for nefarious purposes. In any case, it can be a dangerous path to provide for those who don't know how to pwn systems already... :-(

HiHe 174 Junior Poster

I don't think this is that type of keylogger.

aha67 0 Newbie Poster

Thank's a lot!
That was exactly what I needed.
The only issue I havn't discovered is, why it doesn't work on my RasPi
if I hide the root-window via root.withdraw. If I show it, the code works properly.

some ideas?

best regards

Andy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.