demongolem
demongolem

Reputation: 9708

Is there anyway to override space bar behavior in wxPython on Windows?

I am working with wxPython. I have a wx.Dialog with a bunch of buttons and also a wx.TextCtrl. Each of the buttons is a wx.Button with a bound event to it via wx.EVT_BUTTON. I push a few buttons and they do their thing. I then wish to hit the space bar. Interestingly enough, I have a button labeled SPC that corresponds to the space bar behavior as an added convenience. I want the following to occur:

  1. You push the space bar button and it recognizes a space bar event (works)
  2. You push the space bar on the keyboard and it recognizes a space bar event

    a. You push the space bar on the keyboard with focus on the wx.TextCtrl ... (works)

    b. You push the space bar on the keyboard with focus on a wx.Button because that is the last event you processed and a space bar event is recognized(problem)

I have done:

self.typingWindow.Bind(wx.EVT_KEY_DOWN, self.popup)
self.Bind(wx.EVT_KEY_DOWN, self.popup)

The first line allows case 2a to work

The second line has no effect, but I think it should? I saw this old link which addresses the exact same issue and it seems to say that doing something like my second code line would allow case 2b to work. Anyhow, what does happen in case 2b is that a command event is generated for the last button pressed. This supposedly is a default behavior for Windows.

So my question is can my case 2b work? That is, can I have a key down event generated but not a command event from the last button pressed even if the focus is currently on the last button pressed.

Upvotes: 0

Views: 357

Answers (2)

VZ.
VZ.

Reputation: 22688

Binding to wxEVT_KEY_DOWN at the dialog level can't work because the dialog is never going to get this event, the key press events are sent to the focused window and the dialog never is, the focus is always on one of its children. Also, notice that unlike command event key events are not propagated upwards the window hierarchy.

So you can either handle this event at each button level or use the special wxEVT_CHAR_HOOK event which is sent to the top level parent, i.e. the dialog, exactly to allow this kind of window-global keyboard processing logic.

Notice that in any case it's going to be very confusing to the user if pressing Space does anything else than activating the currently focused button because this is the usual behaviour that people are used to. So I'd strongly advise to avoid doing this in the first place.

Upvotes: 1

Mike Driscoll
Mike Driscoll

Reputation: 33071

You would probably have to bind key events to each individual button and check to see if the space button is being pressed. If so, call the method that the spacebar button event calls. Otherwise, ignore the event. You might be able to use a wx.AcceleratorTable, but I'm not sure.

Upvotes: 0

Related Questions