Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tkinter keypress events are a mess for numpad keys

357 views
Skip to first unread message

Irmen de Jong

unread,
Aug 28, 2017, 7:20:39 PM8/28/17
to
Hi,

Using tkinter in python3, I was trying to intercept individual keypresses (and releases)
of keys on the numeric keypad. I want to use this as a simple joystick simulation.
While you can bind the <KeyPress> event, actually doing something sensible with it in a
cross platform way seems utterly impossible.

The distinguishing attribute of the event object is different depending on what OS
you're using (keysym, keycode, keysym_num) and on Windows registering some keys doesn't
even seem to work (or they're confused with the same keys on the normal keyboard area).
The keysym names/values in the documentation are not correct either
(I'm mainly looking at http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/key-names.html)


My original question with the details on stackoverflow:
https://stackoverflow.com/questions/45869902/better-way-to-deal-with-tks-keyboard-events-mess-for-numpad-keys-in-pythontkin

Unfortunately there hasn't been a useful response or answer.

A gist with a small example program is here:
https://gist.github.com/irmen/2c9d6bb0afb16b464805410c108a2885

Does anyone here have a clue perhaps?
Or have any idea why this is so messy?


Thank you!
Irmen

Terry Reedy

unread,
Aug 29, 2017, 12:33:11 AM8/29/17
to
On 8/28/2017 7:20 PM, Irmen de Jong wrote:
> Hi,
>
> Using tkinter in python3, I was trying to intercept individual keypresses (and releases)
> of keys on the numeric keypad. I want to use this as a simple joystick simulation.
> While you can bind the <KeyPress> event, actually doing something sensible with it in a
> cross platform way seems utterly impossible.
>
> The distinguishing attribute of the event object is different depending on what OS
> you're using (keysym, keycode, keysym_num) and on Windows registering some keys doesn't
> even seem to work (or they're confused with the same keys on the normal keyboard area).
> The keysym names/values in the documentation are not correct either
> (I'm mainly looking at http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/key-names.html)

*The* documentation (for 8.6) is the tcl.tk/man doc set:
https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm
For the level of detail you are looking at, they are essential.

The nmt docs for 8.5 are neither complete (intentionally not) nor always
correct nor always up to date. The tk docs may also have errors, just
as our do, but I presume one can file a report somewhere and possibly
get a fix.

> My original question with the details on stackoverflow:
> https://stackoverflow.com/questions/45869902/better-way-to-deal-with-tks-keyboard-events-mess-for-numpad-keys-in-pythontkin
>
> Unfortunately there hasn't been a useful response or answer.
>
> A gist with a small example program is here:
> https://gist.github.com/irmen/2c9d6bb0afb16b464805410c108a2885
>
> Does anyone here have a clue perhaps?
> Or have any idea why this is so messy?


--
Terry Jan Reedy

Irmen de Jong

unread,
Aug 29, 2017, 1:16:55 PM8/29/17
to
On 29/08/2017 06:32, Terry Reedy wrote:

> *The* documentation (for 8.6) is the tcl.tk/man doc set:
> https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm
> For the level of detail you are looking at, they are essential.
>
> The nmt docs for 8.5 are neither complete (intentionally not) nor always correct nor
> always up to date.  The tk docs may also have errors, just as our do, but I presume one
> can file a report somewhere and possibly get a fix.

Hi Terry thanks for pointing me to that resource.
I'll have a look at https://www.tcl.tk/man/tcl8.6/TkCmd/keysyms.htm
but I don't have high hopes because I already tried empirically to figure out the
distinguishing attributes of the keypress event object, on various platforms (Mac OS,
Linux, Windows)...

Irmen.

Stephan Houben

unread,
Sep 3, 2017, 5:17:17 AM9/3/17
to
Op 2017-08-29, Irmen de Jong schreef <irmen....@xs4all.nl>:

> I'll have a look at https://www.tcl.tk/man/tcl8.6/TkCmd/keysyms.htm
> but I don't have high hopes because I already tried empirically to
> figure out the distinguishing attributes of the keypress event object,
> on various platforms (Mac OS, Linux, Windows)...

I would suggest trying to reproduce the issue in a small Tcl/Tk script.
If the issue can be reproduced in Tcl I would suggest filing a bug
report at https://core.tcl.tk/tk/reportlist .

Stephan

Rick Johnson

unread,
Sep 3, 2017, 7:31:35 AM9/3/17
to
Irmen de Jong wrote:
> Using tkinter in python3, I was trying to intercept
> individual keypresses (and releases) of keys on the numeric
> keypad. I want to use this as a simple joystick simulation.
> While you can bind the <KeyPress> event, actually doing
> something sensible with it in a cross platform way seems
> utterly impossible.

Personally, none of my applications have needed to
differentiate between the "number-keys-on-the-keypad" and
the "number-keys-on-the-main-keyboard", so i'm not sure if
there is a clean solution here, much less a clean cross-
platform solution...

And although my initial reaction to your inquiry (assuming
there is no clean solution here...) was to lay blame on
tkinter for not observing its own cross-platform mandate,
the more i think about this, the more i wonder if tkinter
_should_ differentiate between these duplicate number keys?

Hmm...

Considering that (at a very high level) the sole purpose of
a keyboard is to send character information to a machine by
mapping keypresses to characters -- and not so much
concerning itself with key locations or duplicate keys -- i
think what you are doing is expecting a keyboard to function
in a manner for which it was not intented to function, and
while i'm a big fan of multi-purpose tools, i understand
also the practical importance of simplistic, single-purpose
design.

What are your thoughts on this?

> The distinguishing attribute of the event object is
> different depending on what OS you're using (keysym,
> keycode, keysym_num) and on Windows registering some keys
> doesn't even seem to work (or they're confused with the
> same keys on the normal keyboard area). The keysym
> names/values in the documentation are not correct either

Barring a clean cross-platform solution, i would suggest
one of the following:

(1) Appeal to the Tcl/Tk folks to standardize these "event
codes". After all, _they_ are the ones who market TK as a
cross platform GUI.

or

(2) Create your own mapping that can translate the keycodes,
keysyms, or keysym_nums to something consistent between the
platforms.

I have a feeling that #2 will be more efficient.

The good new is, that if the number keys (0-9) are your only
concern, then we're only talking about ten keys over three
major platforms here -- so (10 x 3 = 30) mappings --
shouldn't be too difficult. Of course, with solution #2 your
code will be at the mercy of future changes that are beyond
your control (aka: brittle), however, i doubt these codes
would change very much over time, if at all. A simple
template might look something like this:


evtMap = {...}

w.bind("<KeyPress>", _evtKeyPress)

def _evtTrans(event):
...

def _evtKeyPress(event):
_evtTrans(event)
# Process event here...


Sucks to do this, but sometimes you have no choice.

PS: If i had nickle for every time i had to take a big
hammer and pound dents out of tkinter's hull to achieve a
more elegant work flow, well, you know the story. Tkinter
may be a rusty old tub, but she gets the job done if you're
willing to periodically scrape the barnacles from her belly,
patch the holes and apply a new coat of lead paint. As any
old salt will testify, she's a labour of love.

0 new messages