# Invoke a event in ruby/tk

**URL:** <https://www.ruby-forum.com/t/invoke-a-event-in-ruby-tk/169351>\
**Category:** Ruby\
**Created:** [June 2, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/invoke-a-event-in-ruby-tk/169351 "2009-06-02T18:46:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jesus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jesus](https://www.ruby-forum.com/u/jesus)\
**Post date:** [June 2, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/invoke-a-event-in-ruby-tk/169351/1 "2009-06-02T18:46:15Z")

</div>

here is the problem.

i have a TkEntry and A TkTable and i need to invoke the events of “Up”  
and “Down” of the TkTable To change the selection of cell as well as if  
it were selected. for reasons unnecessary to explain i need to hold the  
focus in the Tkentry.Please write a suggestion on how to do this.

---

<div class="post-metadata">

**Author:** ![jesus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jesus](https://www.ruby-forum.com/u/jesus)\
**Post date:** [June 3, 2009, 6:11am UTC](https://www.ruby-forum.com/t/invoke-a-event-in-ruby-tk/169351/2 "2009-06-03T06:11:46Z")

</div>

From: Jesus J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: invoke a event in ruby/tk  
Date: Wed, 3 Jun 2009 01:46:17 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> i have a TkEntry and A TkTable and i need to invoke the events of “Up”  
> and “Down” of the TkTable To change the selection of cell as well as if  
> it were selected. for reasons unnecessary to explain i need to hold the  
> focus in the Tkentry.Please write a suggestion on how to do this.

For example,

---

<div class="post-metadata">

**Author:** ![jesus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jesus](https://www.ruby-forum.com/u/jesus)\
**Post date:** [June 3, 2009, 7:00am UTC](https://www.ruby-forum.com/t/invoke-a-event-in-ruby-tk/169351/3 "2009-06-03T07:00:26Z")

</div>

Hidetoshi NAGAI wrote:

> From: Jesus J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Subject: invoke a event in ruby/tk  
> Date: Wed, 3 Jun 2009 01:46:17 +0900  
> Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > i have a TkEntry and A TkTable and i need to invoke the events of “Up”  
> > and “Down” of the TkTable To change the selection of cell as well as if  
> > it were selected. for reasons unnecessary to explain i need to hold the  
> > focus in the Tkentry.Please write a suggestion on how to do this.
> 
> For example,

require “tk”  
require ‘tkextlib/tktable’  
root=TkRoot.new  
entry=TkEntry.new(root).place(:x=\>0,:y=\>0)  
table=Tk::TkTable.new(root).place(:x=\>0,:y=\>40)  
table[‘selecttype’]=‘row’  
table.selection\_set(‘0,0’)  
entry.focus  
#here is my doubt  
#entry.bind(“Up”,proc{call Tktable.bind(“Up”)})???  
#remember that we cant to lose the focus from the entry  
#my intention is replic the event up and down when Tktable has the focus  
#my first idea was invoke a default event. in this case ‘up’ ‘Down’  
#i dont know if this is possble  
#do you have a better idea?  
Tk.mainloop

---

<div class="post-metadata">

**Author:** ![jesus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jesus](https://www.ruby-forum.com/u/jesus)\
**Post date:** [June 3, 2009, 10:11am UTC](https://www.ruby-forum.com/t/invoke-a-event-in-ruby-tk/169351/4 "2009-06-03T10:11:59Z")

</div>

From: Jesus J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: invoke a event in ruby/tk  
Date: Wed, 3 Jun 2009 14:00:35 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> #here is my doubt  
> #entry.bind(“Up”,proc{call Tktable.bind(“Up”)})???  
> #remember that we cant to lose the focus from the entry  
> #my intention is replic the event up and down when Tktable has the focus  
> #my first idea was invoke a default event. in this case ‘up’ ‘Down’  
> #i dont know if this is possble  
> #do you have a better idea?

I see.  
Usually, when want to send event, use “event\_generate” method.  
But Key events have no effect without focus.  
So, you will need a little tricky call.

## For example,

require “tk”  
require ‘tkextlib/tktable’  
root=TkRoot.new  
entry=TkEntry.new(root).place(:x=\>0,:y=\>0)  
table=Tk::TkTable.new(root).place(:x=\>0,:y=\>40)  
table[‘selecttype’]=‘row’  
table.selection\_set(‘0,0’)  
entry.focus

table.activate(‘0,0’) # without ‘active’ element, Up/Down key may not  
work  
entry.bind(‘KeyPress-Up’){  
table.focus # force mode “table.focus(true)” may be required  
table.event\_generate(‘KeyPress-Up’)  
entry.focus # force mode “entry.focus(true)” may be required  
}  
entry.bind(‘KeyPress-Down’){  
table.focus # force mode “table.focus(true)” may be required  
table.event\_generate(‘KeyPress-Down’)  
entry.focus # force mode “entry.focus(true)” may be required  
}

## Tk.mainloop

## If you use a TkVirtualEvent object,

require “tk”  
require ‘tkextlib/tktable’  
root=TkRoot.new  
entry=TkEntry.new(root).place(:x=\>0,:y=\>0)  
table=Tk::TkTable.new(root).place(:x=\>0,:y=\>40)  
table[‘selecttype’]=‘row’  
table.selection\_set(‘0,0’)  
entry.focus

virtual\_event = TkVirtualEvent.new(‘KeyPress-Up’, ‘KeyPress-Down’)

table.activate(‘0,0’) # without ‘active’ element, Up/Down key may not  
work  
entry.bind(virtual\_event, :keysym){|keysym|  
table.focus # force mode “table.focus(true)” may be required  
table.event\_generate(keysym) #or table.event\_generate(‘KeyPress-’ \<\<  
keysym)  
entry.focus # force mode “entry.focus(true)” may be required  
}

Tk.mainloop

---

<div class="post-metadata">

**Author:** ![jesus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jesus](https://www.ruby-forum.com/u/jesus)\
**Post date:** [June 4, 2009, 2:46am UTC](https://www.ruby-forum.com/t/invoke-a-event-in-ruby-tk/169351/5 "2009-06-04T02:46:21Z")

</div>

Thanks, is what I was expecting
