1import tkinter as tk
2
3def key(event):
4 """shows key or tk code for the key"""
5 if event.keysym == 'Escape':
6 root.destroy()
7 if event.char == event.keysym:
8 # normal number and letter characters
9 print( 'Normal Key %r' % event.char )
10 elif len(event.char) == 1:
11 # charcters like []/.,><#$ also Return and ctrl/key
12 print( 'Punctuation Key %r (%r)' % (event.keysym, event.char) )
13 else:
14 # f1 to f12, shift keys, caps lock, Home, End, Delete ...
15 print( 'Special Key %r' % event.keysym )
16
17
18root = tk.Tk()
19print( "Press a key (Escape key to exit):" )
20root.bind_all('<Key>', key)
21# don't show the tk window
22root.withdraw()
23root.mainloop()