Chris
Chris

Reputation: 361

Binding tkinter keys to custom keyboard keys (macros)

I have 6 macro keys on my keyboard, G1 through to G6. My question is simple, how can I use:

from tkinter import *
master = Tk()
master.bind('<G1>', #trigger some event>

To actually trigger an event? Obviously, at the moment an error appears as "G1" isnt recognised.

Upvotes: 2

Views: 494

Answers (1)

patthoyts
patthoyts

Reputation: 33223

You can see if you can get the key code by binding <Key> as shown below. If this doesn't produce anything then your window system is not handling these keys and there is nothing tkinter can do. On my system, holding AltGr and O together generates an ø and I see oslash as the printed output. Adding a new binding for <oslash> then works for that key input.

If this doesn't show a keysym for your keys, you will need to specify the windowing system in use as getting inputs from special keys will be different on X Windows, MacOS and Windows. Tk relies on the window system input queue to provide these keyboard inputs.

import tkinter as tk
root = tk.Tk()
e = ttk.Entry(root)
e.place(x=1,y=1)
e.bind('<Key>', lambda ev: print(ev.keysym))
root.mainloop()

Upvotes: 1

Related Questions