Binding the Return key not working

253 views Asked by At

I am trying to bind my Enter button on my keyboard. I am writing in Perl Tk.

I want one of the next to things to happen.

  1. When I hit "Enter" on my keyboard, I want a button within my program to operate.
  2. When I hit "Enter" on my keyboard, I want a subroutine to run. Either one would satisfy my program seeing that my button opens the subroutine.

Here is my relevant code:

# Button
my $enterbut = $find_sub->Button(
    -command => \&find_substations,
    -text => 'Find Displays',
    -background => 'gray'
)->pack(
    -side => 'left',
    -fill => 'none',
    -ipadx => 8,
    -ipady => 1
);

# Accept "Enter" key as input
$enterbut->bind('<Return>', \&find_substations);

# Output Substation ID to Pane
sub find_substations {
    print;
}

I have tried a few different ways of using the bind command and none of them works.

I am getting no errors but when I press the button my sub does not operate. I begin to believe Return may not be the correct button on my keyboard. Maybe due to driver language or something. Maybe coding error.

1

There are 1 answers

0
Palec On BEST ANSWER

You are binding Enter on your button. It is captured only when your button is focused. If you bind it on the whole window, pressing Enter anywhere in the window should execute the handler function.