# Key event down

**URL:** <https://www.ruby-forum.com/t/key-event-down/219679>\
**Category:** wxRuby\
**Created:** [May 11, 2012, 9:24am UTC](https://www.ruby-forum.com/t/key-event-down/219679 "2012-05-11T09:24:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 11, 2012, 9:24am UTC](https://www.ruby-forum.com/t/key-event-down/219679/1 "2012-05-11T09:24:06Z")

</div>

i cannot do work key down event pls give me a hand thanks this is my  
code:

require “rubygems”  
require “wx”  
include Wx

class MyFrame \< Frame

def initialize  
super(nil, #Parent  
:title =\> “RadioBox Example”, #Displays on top of window  
:pos =\> [1030, 25], #or Wx::DEFAULT\_POSITION  
:size =\> [250, 500] #or Wx::DEFAULT\_SIZE

```
      #For the position and size arguments, you don't need to 

```

specify Point and Size  
#objects anymore. See: wxRuby Overview on the doc page  
wxruby\_intro.html  
)  
evt\_key\_down() { | ev | @text\_position.label = “working key down” }

```
panel = Wx::Panel.new(self) #Parent = self = this Frame

drink_choices = ["coffee", "tea", "juice", "milk"] #labels for 

```

radio buttons

```
radios = Wx::RadioBox.new(
    panel, #Parent
    :label => "Drinks", #Label for box surrounding radio 

```

buttons  
:pos =\> [20, 5],  
:size =\> Wx::DEFAULT\_SIZE,  
:choices =\> drink\_choices, #The labels for the radio buttons  
:major\_dimension =\> 1, #Max number of columns(see next  
line)–try changing it to 2  
:style =\> Wx::RA\_SPECIFY\_COLS #:major\_dimension value applies to  
columns

```
    #The :major_dimension and :style in combination determine the 

```

layout of the  
#RadioBox. In this case, the maximum number of columns is 1,  
which means  
#there can only be one radio button per line. Because there are  
4 radio buttons,  
#that means there will be 4 lines, with one radio button per  
line.

```
)
evt_radiobox(radios.get_id()) {|cmd_event| 

```

on\_change\_radio(cmd\_event)}

```
@text_widget = Wx::StaticText.new(
    panel, #Parent
    :label => "coffee",
    :pos => [150, 25],
    :size => Wx::DEFAULT_SIZE

    #Store a widget in an instance variable when other
    #methods, like the one below, need access to it.
)

boton = Wx::Button.new(
  panel,
  :label => "Posicion",
  :pos => [20,120],
  :size => Wx::DEFAULT_SIZE,
  :style => 0,
  :validator => Wx::DEFAULT_VALIDATOR,
  :name => "button"

  )
evt_button(boton) {|cmd_event| on_click(cmd_event)}

 @text_position = Wx::StaticText.new(
    panel, #Parent
    :label => "Position",
    :pos => [150, 120],
    :size => Wx::DEFAULT_SIZE

    #Store a widget in an instance variable when other
    #methods, like the one below, need access to it.
)

#When there is an event of type evt_radiobox on the widget with the 

```

id radios.get\_id(),  
#the block is called and an “event object” is passed to the block.  
The block calls  
#the method on\_change\_radio(), defined below, relaying the event  
object to the  
#method. The event object contains useful information about the  
radio button  
#that was selected.

```
show #equivalent to self.show, makes the frame visible

```

end

def on\_change\_radio(cmd\_event)  
selected\_drink = cmd\_event.string #Selected radio’s label

```
#Instead of calling cmd_event.get_string() or 

```

cmd\_event.set\_string(), you can  
#now call an accessor method with the same name as the property you  
are trying  
#to get or set. See: wxRuby Overview on the doc page  
wxruby\_intro.html

```
@text_widget.label = selected_drink

```

end  
def on\_click(cmd\_event)

```
 @text_position.label = "testeando"

```

end

end

class MinimalApp \< Wx::App  
def on\_init  
MyFrame.new  
end  
end

MinimalApp.new.main\_loop #main\_loop() tells wxruby to start reacting  
#to radio button selections, button clicks,  
etc.  
#on your App.
