# Key press event

**URL:** <https://www.ruby-forum.com/t/key-press-event/170305>\
**Category:** wxRuby\
**Created:** [June 15, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/key-press-event/170305 "2009-06-15T19:31:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [June 15, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/key-press-event/170305/1 "2009-06-15T19:31:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [June 16, 2009, 7:36am UTC](https://www.ruby-forum.com/t/key-press-event/170305/2 "2009-06-16T07:36:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [June 16, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/key-press-event/170305/3 "2009-06-16T12:29:19Z")

</div>

Dimas C. 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 results

Dimas 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

---

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [June 16, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/key-press-event/170305/4 "2009-06-16T14:02:17Z")

</div>

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](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](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://wxruby.rubyforge.org/doc/acceleratortable.html)  
[http://osdir.com/ml/lang.ruby.wxruby.user/2008-06/msg00036.html](http://osdir.com/ml/lang.ruby.wxruby.user/2008-06/msg00036.html)

hth  
alex
