Search code examples
pythonpython-3.xtkinterkey-bindings

Tkinter Binding "<Control-Key-n>" vs. "<Control-n>"


I have a question:

When binding a function in tkinter, there are many ways to do it. Two of the ways are:

# Method 1
root.bind('<Control-n>', ExampleFunction)

# Method 2
root.bind('<Control-Key-n>', ExampleFunction)

What would be the difference between Control-n and Control-Key-n?


Solution

  • Functionally, there is no difference.

    Control-Key-n is an event specifier consisting of

    1. The modifier Control (the control key was held down when the event occurred.)
    2. The event type Key (short for KeyPress)
    3. The event detail n (specifically, the n key was pressed).

    From man n bind:

    If a keysym detail is given, then the type field may be omitted; it will default to KeyPress. For example, <Control-comma> is equivalent to <Control-KeyPress-comma>.

    So <Control-n> is just an abbreviated form of <Control-Key-n> (which itself is an abbreviated form of <Control-KeyPress-n>): n alone is equivalent to Key-n.