Hello all I am new to fxruby and coming from php-gtk and rubygnome I can not seem to capture key press events with this code the code was modified from an online example ruby 1.8.7, ubuntu 9.04, wxruby2.0.0 nothing is echoed to the command line i also tried exit in the block and nothing thanks in advance include Wx class MyFrame < Frame def initialize super(nil, -1, "Test Key Press Event") evt_key_down { |ev| puts ''a key was pressed'' } end end class MinimalApp < App def on_init MyFrame.new.show end end MinimalApp.new.main_loop happy coding brad -- Posted via http://www.ruby-forum.com/.
Brad, your code worked fine for me. Just added: require ''rubygems'' require ''wx'' before include Wx and changed { |ev| puts ''a key was pressed'' } to { |ev| message_box(''a key was pressed'') } just to make it easier to see the results -- Posted via http://www.ruby-forum.com/.
Dimas Cyriaco wrote:> Brad, your code worked fine for me. > > Just added: > require ''rubygems'' > require ''wx'' > before include Wx > > and changed { |ev| puts ''a key was pressed'' } > to { |ev| message_box(''a key was pressed'') } > just to make it easier to see the resultsDimas thanks for the reply I neglected to put those lines in my post but they are in the code what OS are you using, ruby version, & wxruby version ? 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. 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. thanks & happy coding brad -- Posted via http://www.ruby-forum.com/.
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