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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bind keyboard <enter> to button

Status
Not open for further replies.

wilsoj2

Programmer
Dec 12, 2003
6
US
I have a tk app with an input field that users will type text into. They will then press "send" with the mouse or <Enter> with the keyboard and the message will go to a "log" field and display the text they typed.

They should also be able to press <Enter> without having to use the mouse to select the send button. In the following code when a user inputs text I can't seem to get keyboard bindings to do anything.

I tried this on linux, aix and windows...so I think it's probably a basic issue with how I have it coded. You can copy the entire code below and execute to replicate the issue I'm having, which is when you press enter/return on the keyboard nothing happens, but using the mouse to select "send" will print the text to the log section.

#! /usr/bin/env wish
wm title . "tmp"

# frame(s)
frame .a1 -borderwidth 10 ; # top -- log field
frame .b1 -borderwidth 10 ; # top-1 -- Type text message below:
frame .b2 -borderwidth 10 ; # top-2 -- text editor field
frame .c1 -borderwidth 10 ; # top-3 -- send/close

# vertical stacking order of frames:
pack .a1 .b1 .b2 .c1 -side top -fill both -expand true

# log/display window
#
set log [text .a1.log -width 80 -height 20 \
-borderwidth 2 -relief raised -setgrid true \
-yscrollcommand {.a1.scroll set} ]
scrollbar .a1.scroll -command {.a1.log yview}
pack .a1.scroll -side right -fill y
pack .a1.log -side left -fill both -expand true

# text entry field
#
label .b1.intextbox_char -text "Type text message below:" -padx 0 -pady 0
entry .b2.intextbox -width 80 -relief sunken -textvariable input_data
pack .b1.intextbox_char -side left -fill both -expand true
pack .b2.intextbox -side left -fill both -expand true

# send/close buttons
#
button .c1.send -text Send -command { send }
button .c1.close -text Close -command { exit }
pack .c1.close .c1.send -side right

# key bindings
#
bind .c1.send <Return> {puts "hit enter"}
bind .c1.close <Control-c> exit
focus .b2.intextbox

proc send {} {
global log input_data
$log insert end $input_data
}
 
bind .b2.intextbox <Return> {send}

Bob Rashkin
rrashkin@csc.com
 
Thank you -- very simple problem I just wasn't seeing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top