Where are key events generated?

Hi everybody,

forgive me if this turns out to be a very basic question, but where exactly do key events in wxPython originate from?

With other types of events, it seems pretty clear: If I click on something, the something I clicked on (e.g. a button) will generate the click event; if I select a menu item, it will generate the event in question. But what happens if I have a program with any number of panels, buttons, widgets, dialogs etc. - and then the user presses (or releases) a key? Which component of my program does the key event come from? Conversely, if I am missing a key event that gets “lost” somewhere, where do I start looking for it?

Many thanks,

Ingrid

Generally in GUI programming, keyboard events go to the control with the
focus. How exactly a control gets the focus varies from platform to
platform. In Windows, you generally have to click on it, or navigate to it
using Tab keys or similar. There are also functions in wxPython to assign
the focus to any given control.

···

On Tue, Sep 17, 2013 at 7:13 PM, Ingrid <happyplate09@gmail.com> wrote:

Hi everybody,

forgive me if this turns out to be a very basic question, but where
exactly do key events in wxPython originate from?

With other types of events, it seems pretty clear: If I click on
something, the something I clicked on (e.g. a button) will generate the
click event; if I select a menu item, it will generate the event in
question. But what happens if I have a program with any number of panels,
buttons, widgets, dialogs etc. - and then the user presses (or releases) a
key? Which component of my program does the key event come from?
Conversely, if I am missing a key event that gets "lost" somewhere, where
do I start looking for it?

Many thanks,

Ingrid

For more options, visit https://groups.google.com/groups/opt_out.

--
Best Regards,
Michael Moriarity

Hi Michael,

thanks for your answer. The problem that prompted my question is that I have a key release event that seems to get “eaten up” by a wx.MessageBox. (At least, that is my hypothesis at the moment, since if I wait with the key release until I have closed the dialog everything works fine, and the key release event ends up where it is supposed to.) So I assumed that since the dialog is open when I release the key, it will have the focus and get the event. So to catch the key release event, I changed the wx.MessageBox to a wx.Dialog and bound wx.EVT_KEY_UP to a handler inside that dialog. However, the handler never seems to get called, even when I make sure to release the key while the dialog window is open. Could it be that the focus is not on the dialog, but on the “OK/Cancel” buttons, and that the key release event never goes beyond those buttons? Or is there any way that I can globally redirect key events to some part of my program?

I would greatly appreciate any input on this.

Thanks,

Ingrid

PS: I have tried SetFocus(), FindFocus() - it hasn’t helped so far…

···

Am Dienstag, 17. September 2013 19:39:46 UTC-7 schrieb Data...@gmail.com:

On Tue, Sep 17, 2013 at 7:13 PM, Ingrid happyp...@gmail.com wrote:

Hi everybody,

forgive me if this turns out to be a very basic question, but where exactly do key events in wxPython originate from?

With other types of events, it seems pretty clear: If I click on something, the something I clicked on (e.g. a button) will generate the click event; if I select a menu item, it will generate the event in question. But what happens if I have a program with any number of panels, buttons, widgets, dialogs etc. - and then the user presses (or releases) a key? Which component of my program does the key event come from? Conversely, if I am missing a key event that gets “lost” somewhere, where do I start looking for it?

Many thanks,

Ingrid

For more options, visit https://groups.google.com/groups/opt_out.

Generally in GUI programming, keyboard events go to the control with the focus. How exactly a control gets the focus varies from platform to platform. In Windows, you generally have to click on it, or navigate to it using Tab keys or similar. There are also functions in wxPython to assign the focus to any given control.


Best Regards,
Michael Moriarity

Hello Ingrid:

It is impossible to tell what your problem is from descriptions. Please
make a runnable, small as possible, sample application that demonstrates
the problem, and attach it to a message. http://wiki.wxpython.org/**
MakingSampleApps <http://wiki.wxpython.org/MakingSampleApps&gt; Then I, and
others here who know much more than I do, can play with it, and see why it
fails. By the way, in many cases, the exercise of making the sample app
results in discovering the answer, even before it is sent to the list for
examination.

···

On Wed, Sep 18, 2013 at 6:57 PM, Ingrid <happyplate09@gmail.com> wrote:

Hi Michael,

thanks for your answer. The problem that prompted my question is that I
have a key release event that seems to get "eaten up" by a wx.MessageBox.
(At least, that is my hypothesis at the moment, since if I wait with the
key release until I have closed the dialog everything works fine, and the
key release event ends up where it is supposed to.) So I assumed that
since the dialog is open when I release the key, it will have the focus and
get the event. So to catch the key release event, I changed the
wx.MessageBox to a wx.Dialog and bound wx.EVT_KEY_UP to a handler inside
that dialog. However, the handler never seems to get called, even when I
make sure to release the key while the dialog window is open. Could it be
that the focus is not on the dialog, but on the "OK/Cancel" buttons, and
that the key release event never goes beyond those buttons? Or is there
any way that I can globally redirect key events to some part of my program?

I would greatly appreciate any input on this.

Thanks,

Ingrid

PS: I have tried SetFocus(), FindFocus() - it hasn't helped so far...

--
Best Regards,
Michael Moriarity

Here is a sample that demonstrates the problem. I have described it in greater detail on Stackoverflow:

http://stackoverflow.com/questions/18805956/matplotlib-pick-event-returns-incorrect-pressed-keys/18860751#18860751

(I did find some kind of preliminary workaround; though it still doesn’t give me the correct behavior I want.)

Thanks for your input!!

matplotlib_pickevent_key_problem.py (1.61 KB)

Here is a sample that demonstrates the problem. I have described it in
greater detail on Stackoverflow:

plot - matplotlib pick_event returns incorrect pressed keys - Stack Overflow

(I did find some kind of preliminary workaround; though it still doesn't
give me the correct behavior I want.)

Thanks for your input!!

--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Hi,
I am not sure, what is the needed behaviour, but you could try to
replace the event type in matplotlib:

self.figure.canvas.mpl_connect('pick_event', self.onClick)

change to:

self.figure.canvas.mpl_connect('button_release_event', self.onClick)

and few lines later the event reference:

pressedKey = event.mouseevent.key ## key pressed while clicking

change to:
pressedKey = event.key ## key pressed while clicking

In this form the app does respond in some way (event without the
manual_key_up_event part), you have to check, whether this is what you
want.
(I am a bit surprised, that wx.MessageBox works directly this way
(without calling ShowModal() and Destroy() or the "with ..." clause,
but apparently it does work in this form too.)

hth,
  vbr

···

2013/9/19 Ingrid <happyplate09@gmail.com>:

I’m sorry, but I’ve never used matplotlib, and it is not even installed on my system. I hope someone else can help you.

···

On Wed, Sep 18, 2013 at 8:18 PM, Ingrid happyplate09@gmail.com wrote:

Here is a sample that demonstrates the problem. I have described it in greater detail on Stackoverflow:

http://stackoverflow.com/questions/18805956/matplotlib-pick-event-returns-incorrect-pressed-keys/18860751#18860751

(I did find some kind of preliminary workaround; though it still doesn’t give me the correct behavior I want.)

Thanks for your input!!

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


Best Regards,
Michael Moriarity

Ingrid wrote:

Hi Michael,

thanks for your answer. The problem that prompted my question is that I
have a key release event that seems to get "eaten up" by a
wx.MessageBox. (At least, that is my hypothesis at the moment, since if
I wait with the key release until I have closed the dialog everything
works fine, and the key release event ends up where it is supposed to.)
So I assumed that since the dialog is open when I release the key, it
will have the focus and get the event. So to catch the key release
event, I changed the wx.MessageBox to a wx.Dialog and bound
wx.EVT_KEY_UP to a handler inside that dialog. However, the handler
never seems to get called, even when I make sure to release the key
while the dialog window is open. Could it be that the focus is not on
the dialog, but on the "OK/Cancel" buttons, and that the key release
event never goes beyond those buttons?

There are two factors at play here. First, things like panels and dialogs do not keep the focus if they have a child that can accept it. Second, since key events are not command events they do not propagate to their parent if not handled in the child.

Or is there any way that I can
globally redirect key events to some part of my program?

I would greatly appreciate any input on this.

The typical way to deal with things like this is to delay showing the dialog until after you've seen the key_up event, however since you're just getting the key values from a mouse event (via MPL) then I'm not sure that would fit in your situation.

BTW, if all you need is info about the modifier keys then you could use wx.GetMouseState to get the current state of the mouse and the modifiers, regardless of where the focus is.

···

--
Robin Dunn
Software Craftsman

Hi Robin,

I just read your answer; thank you so much! The wx.GetMouseState was indeed what I needed, since I only care about which modifier keys are pressed while clicking. I ended up using wx.GetKeyState(), which did the trick perfectly.

Many thanks again to all who responded to my question!

Ingrid

···

Am Mittwoch, 25. September 2013 12:21:04 UTC-7 schrieb Robin Dunn:

Ingrid wrote:

Hi Michael,

thanks for your answer. The problem that prompted my question is that I

have a key release event that seems to get “eaten up” by a

wx.MessageBox. (At least, that is my hypothesis at the moment, since if

I wait with the key release until I have closed the dialog everything

works fine, and the key release event ends up where it is supposed to.)

So I assumed that since the dialog is open when I release the key, it

will have the focus and get the event. So to catch the key release

event, I changed the wx.MessageBox to a wx.Dialog and bound

wx.EVT_KEY_UP to a handler inside that dialog. However, the handler

never seems to get called, even when I make sure to release the key

while the dialog window is open. Could it be that the focus is not on

the dialog, but on the “OK/Cancel” buttons, and that the key release

event never goes beyond those buttons?

There are two factors at play here. First, things like panels and
dialogs do not keep the focus if they have a child that can accept it.
Second, since key events are not command events they do not propagate to
their parent if not handled in the child.

Or is there any way that I can

globally redirect key events to some part of my program?

I would greatly appreciate any input on this.

The typical way to deal with things like this is to delay showing the
dialog until after you’ve seen the key_up event, however since you’re
just getting the key values from a mouse event (via MPL) then I’m not
sure that would fit in your situation.

BTW, if all you need is info about the modifier keys then you could use
wx.GetMouseState to get the current state of the mouse and the
modifiers, regardless of where the focus is.


Robin Dunn

Software Craftsman

http://wxPython.org