Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot get canvas widget to bind keyboard event

Status
Not open for further replies.

mbitzko

Programmer
Apr 5, 2005
13
US
I can get a canvas widget to honor a mouse event, but cannot get it to invoke a keyboard event. What I want to do is tie a function key <F4> to a zoom function in the canvas. But I cannot key a simple keypress or <F3> to work.

canvas $frame.worksp -width $cwidth -height $cheight \
-borderwidth 2 -relief sunken -background black
pack $frame.worksp -side bottom -expand yes -fill both
set worksp $frame.worksp
bind $worksp <ButtonPress-3> { puts "This works" }
bind $worksp <KeyPress> { puts "This fails" }
 
I think keypress events can only be bound in widgets where keyboard entries make sense; that is, in entry and text widgets. As an experiment, I can pack a frame: say .a, a label: .a.b, and an entry: .a.c.

If I bind: bind .a <Control-b> {.a.b configure -text done}, it won't matter whether my cursor is in the entry, and therefore in the frame, the binding does nothing.

If, on the other hand, I bind: bind .a.c <Control-b> {.a.b configure -text done}, then if I hit control&"b" when my cursor is in the entry, the label gets configured.

All of which is to say, I don't think it makes sense to bind a keypress event to a canvas.

_________________
Bob Rashkin
 
You need to give focus to the canvas before sending it key events:

Code:
  package require Tk
  grid [canvas .c]
  bind .c <F4> { tk_messageBox -message F4 }
  focus -force .c

Happy tecling

ulis
 
Have a look at the canvas man page :

and the bind section:

"The only events for which bindings may be specified are those related to the mouse and keyboard"

"Keyboard-related events are directed to the focus item, if any (see the focus widget command below for more on this)."

For bind and focus, see:

For more on each command:

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top