# Mouse motion example - help

**URL:** <https://www.ruby-forum.com/t/mouse-motion-example-help/133453>\
**Category:** wxRuby\
**Created:** [March 31, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/mouse-motion-example-help/133453 "2008-03-31T19:06:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajithkumar\_Warrier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@Ajithkumar\_Warrier](https://www.ruby-forum.com/u/Ajithkumar_Warrier)\
**Post date:** [March 31, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/mouse-motion-example-help/133453/1 "2008-03-31T19:06:30Z")

</div>

Hi,  
I am trying out this example adapted from the wxpython book, but the  
mouse motion event does not seem to be captured right. What am I doing  
wrong?

Thanks  
warrior

# code below

require ‘wx’

class MyFrame \< Wx::Frame  
def initialize  
super(nil, -1, “My Frame”, :size =\> [300,300])  
@my\_panel = Wx::Panel.new(self, -1)  
evt\_motion(){ |event| on\_move(event)}  
Wx::StaticText.new(@my\_panel, -1, :label =\> “Pos:”, :pos =\> [10,  
12])  
@posCtrl = Wx::TextCtrl.new(@my\_panel, -1, “”,:pos =\> [40, 10])  
show  
end

def on\_move(event)  
@pos = event.get\_position  
@posCtrl.change\_value("#{@pos}")  
end  
end  
Wx::App.run{MyFrame.new}

---

<div class="post-metadata">

**Author:** ![Ajithkumar\_Warrier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@Ajithkumar\_Warrier](https://www.ruby-forum.com/u/Ajithkumar_Warrier)\
**Post date:** [March 31, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/mouse-motion-example-help/133453/2 "2008-03-31T22:29:46Z")

</div>

Ajithkumar Warrier wrote:

> I am trying out this example adapted from the wxpython book, but the  
> mouse motion event does not seem to be captured right.  
> The mouse movement event is directed to the topmost window. So you  
> either need to specify that mouse events are captured by the Frame, or  
> call the event handler upon the panel which is topmost.

If you think this is different to wxPython, could you post/point to the  
example you mention please?

> # code below
> 
> require ‘wx’
> 
> class MyFrame \< Wx::Frame  
> def initialize  
> super(nil, -1, “My Frame”, :size =\> [300,300])  
> @my\_panel = Wx::Panel.new(self, -1)  
> evt\_motion(){ |event| on\_move(event)}

Either:

@my\_panel.evt\_motion { | e | on\_move(e) }

or

capture\_mouse  
evt\_motion :on\_move

> Wx::App.run{MyFrame.new}  
> cheeers  
> alex

---

<div class="post-metadata">

**Author:** ![Ajithkumar\_Warrier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@Ajithkumar\_Warrier](https://www.ruby-forum.com/u/Ajithkumar_Warrier)\
**Post date:** [March 31, 2008, 10:41pm UTC](https://www.ruby-forum.com/t/mouse-motion-example-help/133453/3 "2008-03-31T22:41:34Z")

</div>

Thanks, your suggestion worked. I missed out on the fact that I had to  
bind the mouse motion to the panel and not to MyFrame. So,  
@my\_panel.evt\_motion worked exactly as needed.
