# Tk question - how do i bind to multiple events?

**URL:** <https://www.ruby-forum.com/t/tk-question-how-do-i-bind-to-multiple-events/117602>\
**Category:** Ruby\
**Created:** [October 18, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/tk-question-how-do-i-bind-to-multiple-events/117602 "2007-10-18T14:15:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 18, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/tk-question-how-do-i-bind-to-multiple-events/117602/1 "2007-10-18T14:15:11Z")

</div>

I have a text field, and i want to get the value out of it when the user  
presses Return or tabs to another field. Currently i’m doing it like  
this:

name\_field = TkEntry.new(root)  
var = TkVariable.new(“f”)  
name\_field.textvariable(name\_var)  
name\_field.pack()

name\_field.bind(“Key-Return”) { name\_field\_value = name\_var.value }  
name\_field.bind(“FocusOut”) { name\_field\_value = name\_var.value }

It seems like there should be a way to combine these, like

name\_field.bind(“Key-Return” || “FocusOut”) #doesn’t work  
name\_field.bind(“Key-Return”, “FocusOut”) #doesn’t work either

But these doesn’t work. Can anyone tell me how to do this please?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 18, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/tk-question-how-do-i-bind-to-multiple-events/117602/2 "2007-10-18T17:00:09Z")

</div>

From: Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: tk question - how do i bind to multiple events?  
Date: Thu, 18 Oct 2007 21:15:10 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> name\_field.bind(“Key-Return”) { name\_field\_value = name\_var.value }  
> name\_field.bind(“FocusOut”) { name\_field\_value = name\_var.value }
> 
> It seems like there should be a way to combine these, like
> 
> name\_field.bind(“Key-Return” || “FocusOut”) #doesn’t work  
> name\_field.bind(“Key-Return”, “FocusOut”) #doesn’t work either
> 
> But these doesn’t work. Can anyone tell me how to do this please?

## Please use TkVirtualEvent object.

virt\_ev = TkVirtualEvent.new(“Key-Return”, “FocusOut”)

# or use TkVirtualEvent#add(seq, …) / TkVirtualEvent#delete(seq, …)

name\_field.bind(virt\_ev) { name\_field\_value = name\_var.value }

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 18, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/tk-question-how-do-i-bind-to-multiple-events/117602/3 "2007-10-18T17:03:15Z")

</div>

Hidetoshi NAGAI wrote:

> ## Please use TkVirtualEvent object.
> 
> virt\_ev = TkVirtualEvent.new(“Key-Return”, “FocusOut”)
> 
> # or use TkVirtualEvent#add(seq, …) / TkVirtualEvent#delete(seq, …)
> 
> name\_field.bind(virt\_ev) { name\_field\_value = name\_var.value }

Great, thanks!
