Tk mouse events and keys

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

    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?

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.

    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. :slight_smile:
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.

  Rule1: Only one binding is called on each bindtag (NOT each widget).
  Rule2: More detailed sequence description has higher priority.
  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)
    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',
          proc{|b, x, y| p_st(b, x, y, false, false, false) }, '%b', '%x','%y')
root.bind('Control-Button',
          proc{|b, x, y| p_st(b, x, y, true, false, false) }, '%b', '%x','%y')
root.bind('Shift-Button',
          proc{|b, x, y| p_st(b, x, y, false, true, false) }, '%b', '%x','%y')
root.bind('Alt-Button',
          proc{|b, x, y| p_st(b, x, y, false, false, true) }, '%b', '%x','%y')
root.bind('Shift-Alt-Button',
          proc{|b, x, y| p_st(b, x, y, false, true, true) }, '%b', '%x','%y')
root.bind('Control-Alt-Button',
          proc{|b, x, y| p_st(b, x, y, true, false, true) }, '%b', '%x','%y')
root.bind('Control-Shift-Button',
          proc{|b, x, y| p_st(b, x, y, true, true, false) }, '%b', '%x','%y')
root.bind('Control-Shift-Alt-Button',
          proc{|b, x, y| p_st(b, x, y, true, true, true) }, '%b', '%x','%y')

Tk.mainloop
-------------------------------------------------------------------
--
                                  Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

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.

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)

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 ..

  # ....
  canvas .c
  set id [.c create rectangle 50 50 200 200]
  .c itemconfigure $id -state hidden
  # .. lateron:
  .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 :slight_smile:

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?

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. (^_^;

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

In Ruby/Tk,

  # ....
  c = TkCanvas.new
  id = TkcRectangle.new(c, 50, 50, 200, 200)
  id.state(:hiden)
  # .. lateron:
  id.state(:normal)

--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

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 :slight_smile:

Regards,

-Martin

···

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