# Lose focus event?

**URL:** <https://www.ruby-forum.com/t/lose-focus-event/126568>\
**Category:** wxRuby\
**Created:** [January 19, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/lose-focus-event/126568 "2008-01-19T18:20:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 19, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/1 "2008-01-19T18:20:24Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

I’m new to wxruby, and I haven’t found any answer with google: Is  
there a way to capture a lose focus event on controls, such as a combo  
box? All my experiments show events when changing the text, but not  
when leaving the box.

I want to make a combobox that autocompletes if I press tab (or down  
arrow or something), but if I click out or hit enter it needs to  
prompt me to add a new value. I’m trying to duplicate a behaviour in  
an Access app - I’d really like to ditch Access!

On a higher level explanation, in case there’s a better way to do it…

I want it to search as I type, displaying possible matches, and let me  
complete with one of the matches, or if there is no match use the  
current search to create a new record.

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
removed\_email\_address@domain.invalid

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHkipRUy30ODPkzl0RAi1bAKDNUbpfevGbpbkDQ8SqXXLZdDcrwQCfcE9G  
3SLeVn92PcX8dgBXonwo0TA=  
=ndvX  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 19, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/2 "2008-01-19T18:40:20Z")

</div>

Hello David,

First off, welcome to the wxRuby community. Always nice to see new  
faces  
around, and more interest being sparked in wxRuby, especially since it  
is  
getting more stable with every release we make. To capture Focus Lost,  
you  
would need to use evt\_kill\_focus(), this detects when a control looses  
focus. It’s specifically for Windows, but since 99.9 percent of the  
classes  
we use in wxRuby, are derived from wxWindow, you should be able to  
utilize  
the evt\_kill\_focus() to detect when a control (Such as a combo box), has  
lost focus.

Don’t think we have a full example of evt\_kill\_focus() and it’s  
counterpart  
evt\_set\_focus(), but it’s pretty simple, and you can find the docs for  
it  
here: [http://wxruby.rubyforge.org/doc/focusevent.html](http://wxruby.rubyforge.org/doc/focusevent.html)

If you have any more questions, feel free to ask, we’re more then happy  
to  
help out.

L8ers,

Mario S.

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 19, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/3 "2008-01-19T22:27:58Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Jan 19, 2008, at 11:39 AM, Mario S. wrote:

> Don’t think we have a full example of evt\_kill\_focus() and it’s  
> counterpart evt\_set\_focus(), but it’s pretty simple, and you can  
> find the docs for it here: [http://wxruby.rubyforge.org/doc/focusevent.html](http://wxruby.rubyforge.org/doc/focusevent.html)
> 
> If you have any more questions, feel free to ask, we’re more then  
> happy to help out.

Thanks for the generous welcome. I’m usually a unix person, and web  
based, but got roped into working on an Access project. (Hence why I’d  
rather do this in ruby)

GUI stuff by hand is quite new to me. 🙂

I see that event firing, but it appears global, firing for everything  
that loses focus. I’ve tried to identify what object was responsible  
via #get\_event\_object, #get\_id, and #id, but no luck so far. How do  
I associate it with the combo box?

require ‘rubygems’

require “wx”  
include Wx

class HelloWorld \< App  
def on\_init  
helloframe = Frame.new(nil, -1, “Hello World”)

```
  button = Button.new(helloframe, :label => 'Close', :pos =>

```

[200,200])  
evt\_button(button) {exit}  
cbo = ComboBox.new(helloframe,  
:pos =\>Point.new(10,10),  
:choices =\> [“foo”,“bar”],  
:value =\> “asdasd”,  
:style =\> CB\_SORT)  
helloframe.show()  
evt\_set\_focus() {|w|  
# how do I tie this to cbo ?  
}  
end  
end  
HelloWorld.new.main\_loop

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHkms6Uy30ODPkzl0RAm6nAJ0QM8C4BJpIVduRleF1ci2F45cnsgCguqSU  
SD4j0BzY9WsCl+egTkwjxxU=  
=qXox  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 20, 2008, 1:23am UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/4 "2008-01-20T01:23:08Z")

</div>

evt\_focus does capture all focus changes. The thing to do, is utilize  
EvtFocus.get\_window, that will return the window that the focus is being  
called for.

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 20, 2008, 1:13am UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/5 "2008-01-20T01:13:46Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

> I see that event firing, but it appears global, firing for everything  
> that loses focus. I’ve tried to identify what object was responsible  
> via #get\_event\_object, #get\_id, and #id, but no luck so far. How do  
> I associate it with the combo box?

I found and example in  
[HTTP://wxruby.rubyforge.org/svn/trunk/wxruby2/samples/controls/](HTTP://wxruby.rubyforge.org/svn/trunk/wxruby2/samples/controls/)

but it doesn’t work. Did you mean MS Windows when you said it was  
related to windows, or WX windows?

class MyComboBox \< ComboBox  
def initialize(\*args)  
super(\*args)#, style)

```
 evt_set_focus {|event| onFocusGot(event)}

```

end

def onFocusGot(event)  
log\_message(“MyComboBox::OnFocusGot”)  
event.skip()  
end

end

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHkpIjUy30ODPkzl0RAj9MAKCRdrzr370rQyJ3TIkAzhe+WhLl8wCeK355  
Xc40BF9/81vJO1zZ6m1NXFU=  
=OGub  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 20, 2008, 1:40am UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/6 "2008-01-20T01:40:42Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Jan 19, 2008, at 6:22 PM, Mario S. wrote:

> evt\_focus does capture all focus changes. The thing to do, is  
> utilize EvtFocus.get\_window, that will return the window that the  
> focus is being called for.

test.rb:63:in `on_init': undefined method 'get_window' for #<Wx::FocusEvent:0x11d26a98> (NoMethodError) from test.rb:68:in `call’  
from test.rb:68:in `main\_loop’  
from test.rb:68

class HelloWorld \< App  
def on\_init  
helloframe = Frame.new(nil, -1, “Hello World”)

```
  button = Button.new(helloframe, :label => 'Close', :pos =>

```

[200,200])  
evt\_button(button) {exit}  
cbo = MyComboBox.new(helloframe,  
:pos =\>Point.new(10,10),  
:choices =\> [“foo”,“bar”],  
:value =\> “asdasd”,  
:style =\> CB\_SORT)  
helloframe.show()  
evt\_set\_focus() {|w|  
puts w.get\_window  
# how do I tie this to cbo ?  
}  
end  
end  
HelloWorld.new.main\_loop

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHkphlUy30ODPkzl0RAmhIAJ4oTI9fQSCMlotalaYNvNa1EXvkTQCgkdVf  
CgiQBdFCoEdUE1ZU6bC1Deo=  
=8NQI  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 21, 2008, 1:07am UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/7 "2008-01-21T01:07:41Z")

</div>

Hi David

David M. wrote:

> I see that event firing, but it appears global, firing for everything  
> that loses focus. I’ve tried to identify what object was responsible  
> via #get\_event\_object, #get\_id, and #id, but no luck so far. How do  
> I associate it with the combo box?

FocusEvents aren’t CommandEvents, so they don’t “bubble” upwards to  
parent widgets. So I think you need to call the evt\_kill\_focus method on  
each ComboBox instance. Have a look at the event handling tutorial in  
the main API docs for more info.

get\_event\_object should return the widget that triggered any event.

> ```
> cbo = ComboBox.new(helloframe,
> :pos =>Point.new(10,10),
> :choices => ["foo","bar"],
> :value => "asdasd",
> :style => CB_SORT)
> helloframe.show()
> evt_set_focus() {|w|
> # how do I tie this to cbo ?
> }
> end
> 
> ```

try:  
cbo.evt\_set\_focus { … }

Note that there’s a bug on OS X that means ComboBoxes aren’t firing  
evt\_kill\_focus for some reason. But it should work fine on Windows or  
Linux.

hth  
alex

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 20, 2008, 3:38am UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/8 "2008-01-20T03:38:50Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

From what I can tell from various wxwidget sources, it seems that  
most controls do not send the event up to the control level, and I saw  
one post that was attempting to bind an event… is this the  
EvtHandler#connect method?

On Jan 19, 2008, at 6:22 PM, Mario S. wrote:

> everything  
> related to windows, or WX windows?  
> log\_message(“MyComboBox::OnFocusGot”)
> 
> wxruby-users mailing list  
> [http://rubyforge.org/projects/wxride/](http://rubyforge.org/projects/wxride/)  
> [http://rubyforge.org/projects/vwmc/](http://rubyforge.org/projects/vwmc/)
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHkrQhUy30ODPkzl0RApl/AKDV5mFHh7YCGNKd8ji2TXKG+u3d+QCcDnG/  
GLo2hIMbyp7bi7uqLlAIZD0=  
=EAfD  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 21, 2008, 1:24am UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/9 "2008-01-21T01:24:06Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Jan 20, 2008, at 6:07 PM, Alex F. wrote:

> Note that there’s a bug on OS X that means ComboBoxes aren’t firing  
> evt\_kill\_focus for some reason. But it should work fine on Windows  
> or Linux.

Hah, I just fired off a message about that. lol. Is this a wxwidgets  
thing, or a deeper OS X thing?

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHk+S0Uy30ODPkzl0RAieJAJoCbnOf5jz4oDIstyj/1Hs119SLIACfeFhH  
QQGxQv8+NnQtC0eZAvOa/z0=  
=auOH  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 21, 2008, 1:36am UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/10 "2008-01-21T01:36:57Z")

</div>

David M. wrote:

> I uncommented that log\_message call in onFocusGot(). It works in  
> WinXP, but it does not work in OS X. In XP, I get a popup with that  
> message, but in OS X, nothing happens at all. Eventually the pp is  
> going to be used in XP, but I’d like to develop on OS X. Any ideas?

[http://rubyforge.org/tracker/index.php?func=detail&aid=5532&group\_id=35&atid=218](http://rubyforge.org/tracker/index.php?func=detail&aid=5532&group_id=35&atid=218)

It’s a bug I would very much like to fix as I want to do something  
similar to what you describe in my own app. But I suspect it’s a  
wxWidgets problem, and haven’t got around to writing a C++ test case to  
find out.

As a workaround you could use evt\_child\_focus, which definitely does  
work on OS X. You cna use get\_window to see what child received focus,  
and if you track what the last focus was, you could see when the CB lost  
focus.

alex

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 21, 2008, 1:16am UTC](https://www.ruby-forum.com/t/lose-focus-event/126568/11 "2008-01-21T01:16:29Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Starting over on this thread. I found an example in the sample code  
controls/controls.rb around line 110-149:

class MyComboBox \< ComboBox  
def initialize(parent,id,value = EmptyString,  
pos = DEFAULT\_POSITION,  
size = DEFAULT\_SIZE,  
choices = ,  
style = 0)  
super(parent, id, value, pos, size, choices)#, style)

```
 evt_char {|event| onChar(event)}
 evt_key_down {|event| onKeyDown(event)}
 evt_key_up {|event| onKeyUp(event)}

 evt_set_focus {|event| onFocusGot(event)}

```

end

# [snip]

def onFocusGot(event)  
log\_message(“MyComboBox::OnFocusGot”)  
event.skip()  
end  
end

I uncommented that log\_message call in onFocusGot(). It works in  
WinXP, but it does not work in OS X. In XP, I get a popup with that  
message, but in OS X, nothing happens at all. Eventually the pp is  
going to be used in XP, but I’d like to develop on OS X. Any ideas?

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHk+RHUy30ODPkzl0RAtXYAKDINuBBaAqfOEfJRUajNpPKedileACg06VR  
vxNQodbw6F0UErkeI5JuTKY=  
=tMwM  
-----END PGP SIGNATURE-----
