[Matplotlib-users] Key Press and Key Release Events in TkInter

Joe Kington joferkington at gmail.com
Thu Feb 2 15:07:12 EST 2017


That's a common "gotcha" when trying to detect a held key on a keyboard.
Your operating system (or the actual hardware in your keyboard in some
cases) interprets a held key as multiple keypresses and fires off events
accordingly (e.g., that's why holding down "a" will give "aaaaaaaaa" when
typing).

I think Qt handles this automatically, but I could be wrong. Tk doesn't do
it automatically.

Usually, you'd detect a held key by keeping track of the either the last
key press event or the time since the last keypress event.  "Debouncing" is
the term you'll want to search for.  In tkinter, you can bind a function to
fire after idle (effectively checking if the key was ever released).
Alternatively, for the matplotlib side, I think you can bind to the idle
event and do the same thing without needing to rely on anything
tkinter-specific (Untested).

Hope that helps a bit, anyway.
-Joe

On Tue, Jan 31, 2017 at 3:05 PM, Gregory Key <gregk3172 at outlook.com> wrote:

> I am running Python 3.5.2 and MatPlotLib 2.0.0 on Ubuntu 16.10.
>
> I am developing a program that uses matplotlib embedded in tkinter to
> plot vibration data in a bode plot, phase and amplitude versus rotor
> speed. I adapted the data browser given in the matplotlib documentation
>             as example 91.2 data_browser.py to browse the data. The
> browser worked but I wanted to modify it so that holding down the next
> or previous key would scroll through the data instead of having to
> continously press and release the keys. My plan was to use the
> key_press_event to start a timer which would call a scroll method and
> then use the key_release event to stop the timer and stop scrolling. I
> couldn't make this scheme work so I did some investigating on the
> key_press_event and key_release_event in tkinter. I used example 91.5
> keypress_demo.py given in the matplotlib documentation but I added a
> key_release_event method to it. What I found was that the key events
> don't work the way I would expect them to work. If you press and
> release a key things work as expected. When a key is pressed the
> key_press_event fires and when it is released the key_release_event
> fires. If a key is pressed and held however the key_press_event fires
> followed immediately by a key_release_event even though the key has not
> been released. As long as the key is held down it will continue to fire
> key_press_event followed by key_release_event. Is this the way it is
> supposed to work?
>
> Thanks
> Greg Key
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170202/2c29f39b/attachment.html>


More information about the Matplotlib-users mailing list