# Tk mouse events and keys

**URL:** <https://rubytalk.org/t/tk-mouse-events-and-keys/15122>\
**Category:** ruby-talk\
**Created:** [3 December 2004 12:05 UTC](https://rubytalk.org/t/tk-mouse-events-and-keys/15122 "2004-12-03T12:05:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Volkmann](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Mark\_Volkmann](https://rubytalk.org/u/Mark_Volkmann)\
**Post date:** [3 December 2004 12:05 UTC](https://rubytalk.org/t/tk-mouse-events-and-keys/15122/1 "2004-12-03T12:05:09Z")

</div>

I'm using the following code to detect a left mouse click.

&nbsp;&nbsp;&nbsp;&nbsp;canvas.bind('1', proc {|event| doPress(event)})

Inside my doPress method, is there a way I can ask the event object whether  
the ctrl key was down when the mouse button was pressed?

Is proc what I should be using in my bind call?

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [3 December 2004 14:23 UTC](https://rubytalk.org/t/tk-mouse-events-and-keys/15122/2 "2004-12-03T14:23:33Z")

</div>

Hi,

> **···**
>
> From: "R. Mark Volkmann" \<mark@ociweb.com\>  
> Subject: Tk mouse events and keys  
> Date: Fri, 3 Dec 2004 21:05:09 +0900  
> Message-ID: \<01be01c4d930$52e58e50$0200a8c0@MarkDesktop\>
> 
> > I'm using the following code to detect a left mouse click.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;canvas.bind('1', proc {|event| doPress(event)})
> > 
> > Inside my doPress method, is there a way I can ask the event object whether  
> > the ctrl key was down when the mouse button was pressed?
> 
> Please see Tcl/Tk's "bind" manual. 🙂  
> Modifiers of event sequence are required.  
> When a widget has 'Shift-Button-1' and 'Button-1' binding and  
> the button 1 is pressed \*WITHOUT\* Shift-key on the widget,  
> both of the sequences can mathe the event.  
> Then, the binding which is called for the event is decided  
> based on the following rules.
> 
> &nbsp;&nbsp;Rule1: Only one binding is called on each bindtag (NOT each widget).  
> &nbsp;&nbsp;Rule2: More detailed sequence description has higher priority.  
> &nbsp;&nbsp;Rule3: The binding defined at last has highest priority.
> 
> The following is a test script. I think the script will help you.  
> -------------------------------------------------------------------  
> #!/usr/bin/env ruby  
> require 'tk'
> 
> root = Tk.root
> 
> $ctrl\_st = false  
> $shift\_st = false  
> $alt\_st = false
> 
> def p\_st(btn, x, y, ctrl, shift, alt)  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "btn#{btn}(#{x},#{y}) ctrl=\>[#{$ctrl\_st},#{ctrl}] shift=\>[#{$shift\_st},#{shift}] alt=\>[#{$alt\_st},#{alt}]"  
> end
> 
> root.bind('KeyPress-Control\_L', proc{puts 'press ctrl'; $ctrl\_st = true})  
> root.bind('KeyRelease-Control\_L', proc{puts 'release ctrl'; $ctrl\_st = false})  
> root.bind('KeyPress-Control\_R', proc{puts 'press ctrl'; $ctrl\_st = true})  
> root.bind('KeyRelease-Control\_R', proc{puts 'release ctrl'; $ctrl\_st = false})
> 
> root.bind('KeyPress-Shift\_L', proc{puts 'press shift'; $shift\_st = true})  
> root.bind('KeyRelease-Shift\_L', proc{puts 'release shift'; $shift\_st = false})  
> root.bind('KeyPress-Shift\_R', proc{puts 'press shift'; $shift\_st = true})  
> root.bind('KeyRelease-Shift\_R', proc{puts 'release shift'; $shift\_st = false})
> 
> root.bind('KeyPress-Alt\_L', proc{puts 'press alt'; $alt\_st = true})  
> root.bind('KeyRelease-Alt\_L', proc{puts 'release alt'; $alt\_st = false})  
> root.bind('KeyPress-Alt\_R', proc{puts 'press alt'; $alt\_st = true})  
> root.bind('KeyRelease-Alt\_R', proc{puts 'release alt'; $alt\_st = false})
> 
> root.bind('Button',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc{|b, x, y| p\_st(b, x, y, false, false, false) }, '%b', '%x','%y')  
> root.bind('Control-Button',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc{|b, x, y| p\_st(b, x, y, true, false, false) }, '%b', '%x','%y')  
> root.bind('Shift-Button',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc{|b, x, y| p\_st(b, x, y, false, true, false) }, '%b', '%x','%y')  
> root.bind('Alt-Button',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc{|b, x, y| p\_st(b, x, y, false, false, true) }, '%b', '%x','%y')  
> root.bind('Shift-Alt-Button',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc{|b, x, y| p\_st(b, x, y, false, true, true) }, '%b', '%x','%y')  
> root.bind('Control-Alt-Button',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc{|b, x, y| p\_st(b, x, y, true, false, true) }, '%b', '%x','%y')  
> root.bind('Control-Shift-Button',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc{|b, x, y| p\_st(b, x, y, true, true, false) }, '%b', '%x','%y')  
> root.bind('Control-Shift-Alt-Button',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc{|b, x, y| p\_st(b, x, y, true, true, true) }, '%b', '%x','%y')
> 
> Tk.mainloop  
> -------------------------------------------------------------------  
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

---

<div class="post-metadata">

**Author:** ![Mark\_Volkmann](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Mark\_Volkmann](https://rubytalk.org/u/Mark_Volkmann)\
**Post date:** [4 December 2004 02:11 UTC](https://rubytalk.org/t/tk-mouse-events-and-keys/15122/3 "2004-12-04T02:11:28Z")

</div>

If I have an item on a TkCanvas such as a Rectangle, is there a way to hide it temporarily and then show it later?  
Hopefully I don't have to delete it and recreate it later.

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [4 December 2004 03:36 UTC](https://rubytalk.org/t/tk-mouse-events-and-keys/15122/4 "2004-12-04T03:36:14Z")

</div>

Hi,

> **···**
>
> From: "R. Mark Volkmann" \<mark@ociweb.com\>  
> Subject: hide/show items on TkCanvas?  
> Date: Sat, 4 Dec 2004 11:11:28 +0900  
> Message-ID: \<039d01c4d9a6$8e9e8b10$0200a8c0@MarkDesktop\>
> 
> > If I have an item on a TkCanvas such as a Rectangle, is there a way to hide  
> > it temporarily and then show it later?  
> > Hopefully I don't have to delete it and recreate it later.
> 
> Probably, the simplest way is to move the item to out of  
> the canvas's scrollregion.  
> For exampke, when the scrollregion of the canvas is  
> (min\_x, min\_y, max\_x, max\_y), you can hide the item by  
> item.move(-(max\_x - min\_x), -(max\_y - min\_y)) and show  
> it by item.move(max\_x - min\_x, max\_y - min\_y).  
> Of course, if the part of the item exists on the out of  
> the scrollregion, you never forget the out\_of\_range part  
> when decide the moving delta.  
> Then, bbox method (and canvas tags) may be useful.  
> --  
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

---

<div class="post-metadata">

**Author:** ![Martin\_S\_Weber](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Martin\_S\_Weber](https://rubytalk.org/u/Martin_S_Weber)\
**Post date:** [4 December 2004 08:17 UTC](https://rubytalk.org/t/tk-mouse-events-and-keys/15122/5 "2004-12-04T08:17:18Z")

</div>

I don't know Ruby/Tk, but I do know Tk, and the canvas supports this via  
itemconfigure -state {normal, hidden}. For mental guidance, the equivalent  
tcl code would be sth like ..

&nbsp;&nbsp;# ....  
&nbsp;&nbsp;canvas .c  
&nbsp;&nbsp;set id [.c create rectangle 50 50 200 200]  
&nbsp;&nbsp;.c itemconfigure $id -state hidden  
&nbsp;&nbsp;# .. lateron:  
&nbsp;&nbsp;.c itemconfigure $id -state normal

Of course if you're using tags etc. you can have whole groups disappear at  
once and reappear later etc. I suggest reading the canvas manpage, it's huge  
but has a lot of info too 🙂

Hth,

-Martin

> **···**
>
> On Sat, Dec 04, 2004 at 11:11:28AM +0900, R. Mark Volkmann wrote:
> 
> > If I have an item on a TkCanvas such as a Rectangle, is there a way to hide  
> > it temporarily and then show it later?

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [4 December 2004 08:37 UTC](https://rubytalk.org/t/tk-mouse-events-and-keys/15122/6 "2004-12-04T08:37:20Z")

</div>

Hi,

> **···**
>
> From: "Martin S. Weber" \<Ephaeton@gmx.net\>  
> Subject: Re: hide/show items on TkCanvas?  
> Date: Sat, 4 Dec 2004 17:17:18 +0900  
> Message-ID: \<20041204081507.GA3290@circe.entropie.net\>
> 
> > I don't know Ruby/Tk, but I do know Tk, and the canvas supports this via  
> > itemconfigure -state {normal, hidden}. For mental guidance, the equivalent  
> > tcl code would be sth like ..
> 
> You are right. 'state' option is supported on Tk8.3+ (is it right?).
> 
> # I'd forgotten the option. And I checked the manual of Tk8.0. (^\_^;
> 
> > &nbsp;&nbsp;# ....  
> > &nbsp;&nbsp;canvas .c  
> > &nbsp;&nbsp;set id [.c create rectangle 50 50 200 200]  
> > &nbsp;&nbsp;.c itemconfigure $id -state hidden  
> > &nbsp;&nbsp;# .. lateron:  
> > &nbsp;&nbsp;.c itemconfigure $id -state normal
> 
> In Ruby/Tk,
> 
> &nbsp;&nbsp;# ....  
> &nbsp;&nbsp;c = TkCanvas.new  
> &nbsp;&nbsp;id = TkcRectangle.new(c, 50, 50, 200, 200)  
> &nbsp;&nbsp;id.state(:hiden)  
> &nbsp;&nbsp;# .. lateron:  
> &nbsp;&nbsp;id.state(:normal)
> 
> --  
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

---

<div class="post-metadata">

**Author:** ![Martin\_S\_Weber](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Martin\_S\_Weber](https://rubytalk.org/u/Martin_S_Weber)\
**Post date:** [4 December 2004 08:57 UTC](https://rubytalk.org/t/tk-mouse-events-and-keys/15122/7 "2004-12-04T08:57:50Z")

</div>

> Hi,

Moin,

> \> (... me suggesting item's 'state' option ...)  
> You are right. 'state' option is supported on Tk8.3+ (is it right?).

Dunno, it's been there ever since I've needed it 🙂

Regards,

-Martin

> **···**
>
> On Sat, Dec 04, 2004 at 05:37:20PM +0900, Hidetoshi NAGAI wrote:
