# Evt\_kill\_focus Problem

**URL:** <https://www.ruby-forum.com/t/evt-kill-focus-problem/166118>\
**Category:** wxRuby\
**Created:** [April 22, 2009, 1:29am UTC](https://www.ruby-forum.com/t/evt-kill-focus-problem/166118 "2009-04-22T01:29:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [April 22, 2009, 1:29am UTC](https://www.ruby-forum.com/t/evt-kill-focus-problem/166118/1 "2009-04-22T01:29:16Z")

</div>

I’m trying to process the focus changed event without success. I’m only  
interested in it if it is a particular control losing focus, but (so  
far) I haven’t been able to get it processed at all. In the event setup,  
I use the following:

evt\_kill\_focus() {|event| focus\_changed(event)}

in the focuschanged I do the following

def focus\_changed(event)  
if event.get\_window.id == @endbox.id#Test whether I’m  
interested  
------

best I can tell, the focus\_changed is never called. Could someone point  
out the right way to do this?

Thanks in advance  
—Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [April 22, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/evt-kill-focus-problem/166118/2 "2009-04-22T15:44:54Z")

</div>

I think evt\_kill\_focus is one of those events you have to define inside  
the  
class that will be using it, since it doesn’t take an id parameter. If  
you  
don’t have a specific class for that one control (which is most of the  
time), the somewhat-hacky solution I’ve used in the past would look  
something like:

class \<\< @endbox  
def setup\_focus\_event(&block)  
evt\_kill\_focus(&block)  
end  
end

@endbox.setup\_focus\_event { |event| focus\_changed(event) }

# …

def focus\_changed(event)  
# do stuff  
end

While it’s admittedly not a very pretty or probably very elegant  
solution,  
it works for me, and has the side-effect of no longer requiring that you  
check to make sure the event is firing on the control you’re interested  
in.

Hope this helps,  
–Steve

On Tue, Apr 21, 2009 at 7:29 PM, Michael S.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [April 22, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/evt-kill-focus-problem/166118/3 "2009-04-22T16:56:21Z")

</div>

Stephen B. wrote:

> I think evt\_kill\_focus is one of those events you have to define  
> inside the class that will be using it, since it doesn’t take an id  
> parameter. If you don’t have a specific class for that one control  
> (which is most of the time), the somewhat-hacky solution I’ve used in  
> the past would look something like:

Yes, see the discussion of the distinction between CommandEvents (which  
“bubble” upwards) and others (which are notified to their generator  
only).

[http://wxruby.rubyforge.org/doc/eventhandlingoverview.html](http://wxruby.rubyforge.org/doc/eventhandlingoverview.html)

There is also ChildFocusEvent, which can be used in Panels &c to check  
the focus of immediate children.

alex

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [April 30, 2009, 2:31pm UTC](https://www.ruby-forum.com/t/evt-kill-focus-problem/166118/4 "2009-04-30T14:31:06Z")

</div>

I’ve recently had to solve a similar problem in my own code (catching an  
evt\_motion on a Wx::ListBox to pop up a custom tooltip), and after  
writing  
out what I wrote here, I had a crazy idea which made me feel really  
ridiculous:

@endbox.focus\_event(:focus\_changed)

# …

def focus\_changed(event)  
# do stuff  
end

I’m not positive of the :focus\_changed part or if you’d still have to  
pass a  
block, but I know Command events you can pass method names like that. I  
think it’s a far more elegant solution than what I wrote previously.

–Stephen
