So, I'm writing a smallish adventure game in Python, which operates by means of commands being typed into a tkinter textbox, hitting enter or pressing a button to submit said commands, then there being a readout box that gives the results of whatever the action was.

Now, what I'd like to do is bind some autocommands to the numpad - particularly for navigation, so hitting the 8 feeds "NORTH" into the engine and so on.

This has mostly been successful, except for the bindings, which I can't get working; I can bind to other keys, just not the ones I want.

  • with numpad on, the keys act the same as the standard number keys, type into the textbox, and ignore any bindings I place thereon. doesn't seem to bind anywhere, and <8> binds to both the numpad 8 and the standard 8 - in both cases they still type, which is suboptimal
  • with numpad off it works a bit better, but I'd like to avoid duplicating to the other keys which is what seems to happen (so for example I can only seem to get the 8 to work by binding as , which also binds the standard up arrow - I get errors if I try and use KP_Up). I also can't for the life of me work out how to bind to the middle key (5).

Is there any way I can just bind the numpad keys? The documentation I've found all advises this use of KP but that just doesn't seem to be working. NB that I'm using windows.

Did you check this code which apparently implements playing with the arrow keys and the numpad to move the cursor in a text widget ? It could be a starting point. tkinter.unpythonic.net is a good site :)

The 5 is KP_Begin.

That's sort of what I'm saying - I tried KP_Begin and the other KP_(something) key binds (which are what that code uses as well, though I can't get the code to run right now) and they don't seem to work :/

This code from Vegaseat tells you which key is pressed. You can use a similar function that tests for cetain keys and responds accordingly. I think the general binding is

widget.bind("<1>", call_back) (for the number 1).

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

def key_in(event):
    ##shows key or tk code for the key
    if event.keysym == 'Escape':
        root.quit()
    if event.char == event.keysym:
        # normal number and letter characters
        label_str.set('Normal Key ' + event.char)
    elif len(event.char) == 1:
        # charcters like []/.,><#$ also Return and ctrl/key
        label_str.set('Punctuation Key %r (%r)' % (event.keysym, event.char) )
    else:
        # everything else
        # F1 to F12, shift keys, caps lock, Home, End, Delete, Arrows ...
        label_str.set('Special Key %r' % (event.keysym))

root = tk.Tk()
label_str=tk.StringVar()
label_str.set(" ")
tk.Label(root,textvariable=label_str, fg="blue").grid()
tk.Label(root, text="Press a key (Escape key to exit):" ).grid(row=1)

ent=tk.Entry(root)
##ent.grid(row=0, column=1)   ##Not packed so will take any entry
ent.bind_all('<Key>', key_in)
ent.focus_set()

root.mainloop()
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.