[wxruby-users] key press event

Alex Fenton
Tue, 16 Jun 2009 11:55:12 -0000

Hi Brad

Brad Montgomery wrote:
> I got the code to work if I connected the event handler to a Wx::Panel,
> but that will not solve the problem.
> When the event handler is connected to the panel and the user touches
> another
> widget on the screen that widget will now have focus and the panel will
> no
> longer receive the key press events.
>

By default, only events that are CommandEvents (those associated with
defined actions on controls, like clicking a button, typing text in a
TextCtrl) propagate up the window hierarchy to the parent. Other types
of events, such as MouseEvents and KeyEvents are only received by the
window/widget that currently has focus. So if you have a Panel on top of
a Frame, once the Panel child has had the event, it won''t propagate
further.

http://wxruby.rubyforge.org/doc/eventhandlingoverview.html
> I need the event handler connected to the Frame/Window and listen for
> event
> even if it does not have focus.
> I am reading from a bar code scanner that sends the data as keyboard
> input.
>

The above normally makes sense for interaction, but I can see it''s a
bit
inconvenient for this less usual situation. There are various ways you
could work round this. If you have only a single frame receiving data,
and a small number of windows within it, you could capture the key press
event on all of them, and then manipulate the received event''s
propagation level with methods like resume_propagation. See this python
example:

http://wiki.wxpython.org/index.cgi/EventPropagation

If the bar code data is such that it is represented by only a small
number of different possible keys you could try using an
acceleratortable, which directs all key presses within a frame to menu items

http://wxruby.rubyforge.org/doc/acceleratortable.html
http://osdir.com/ml/lang.ruby.wxruby.user/2008-06/msg00036.html

hth
alex