Tk mouse events and keys

  • Thread starter R. Mark Volkmann
  • Start date
R

R. Mark Volkmann

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?
 
H

Hidetoshi NAGAI

Hi,

From: "R. Mark Volkmann" <[email protected]>
Subject: Tk mouse events and keys
Date: Fri, 3 Dec 2004 21:05:09 +0900
Message-ID: said:
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. :)
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
 
R

R. Mark Volkmann

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

Hidetoshi NAGAI

Hi,

From: "R. Mark Volkmann" <[email protected]>
Subject: hide/show items on TkCanvas?
Date: Sat, 4 Dec 2004 11:11:28 +0900
Message-ID: said:
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.
 
M

Martin S. Weber

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?

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

Hth,

-Martin
 
H

Hidetoshi NAGAI

Hi,

From: "Martin S. Weber" <[email protected]>
Subject: Re: hide/show items on TkCanvas?
Date: Sat, 4 Dec 2004 17:17:18 +0900
Message-ID: said:
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)
 
M

Martin S. Weber

Hi,
Moin,

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,126
Latest member
FastBurnketoIngredients
Top