''' 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()
4
Contributors
3
Replies
38
Views
3 Years
Discussion Span
Last Post by aha67
1

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... :-(

0

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

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.