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