Gnocl Cookbook‎ > ‎

Controlling input focus

Ordinarily we want a dialog widget to grab the input focus but what if we want to break the rule? Its not straight forward, but here's how.

A non-modal dialog will allow the user to interactively set which widget will take the focus for keyboard input, getting to do the task procedurally requires a little circumvention of the rules.  The script below shows how to do it without touching the mouse. First of all, set which widget in the main window  has focus prior to opening the dialog, create a non-modal dialog, that does not take focus, then return focus to the main window.

# test-grabFocus.tcl
# Created by William J Giddings

# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require Gnocl

set txt [gnocl::text -wrapMode word]
$txt lorem
$txt configure -hasFocus 1
set win [gnocl::window -child $txt -setSize 0.5]


proc responseProc { val } {
    if { "Yes" == $val } {
        return -code break
    } else {
        gnocl::dialog \
            -type question \
            -text "Are you sure?" \
            -buttons %#No

gnocl::dialog \
    -acceptFocus 0 \
    -type question \
    -text "Do you like Gnocl?" \
    -buttons "%#No {{%_I _like it} %#Yes}" \
    -onResponse "responseProc %v"

$win grabFocus