Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

[Tk] use enter key iso command button

by Anonymous Monk
on Jan 25, 2020 at 15:21 UTC ( [id://11111879]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to make a script where it is possible to use the enter key same as pressing a command button, but only if the entry widget has focus. So that is what i am trying to determine. Here is my code, i just put this together because i am writing a larger program.

use strict; use warnings; use Tk; my $mw = new MainWindow; my $text = $mw->Scrolled("Text", -scrollbars => 'e')->pack(-side=>'bot +tom',-anchor=>'w'); my $label = $mw->Label(-text=>"hello")->pack(-side=>'left',-anchor=>'n +'); my $entry = $mw->Entry()->pack(-side=>'left',-anchor=>'n'); $mw->bind('<Key-Return>',\&verwijs); $entry->focus; MainLoop; sub verwijs{ my $who = $mw->focusCurrent; print %$who; }

The output = "_TkValue_.entry_XEvent_XEvent=SCALAR(0x1ffc334)". So this means the entry widget had focus, but how do i write this an test for it? I can't find any documentation. Thank you.

Replies are listed 'Best First'.
Re: [Tk] use enter key iso command button
by tybalt89 (Monsignor) on Jan 25, 2020 at 15:30 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11111879 use warnings; use Tk; my $mw = new MainWindow; my $text = $mw->Scrolled("Text", -scrollbars => 'e' )->pack(-side=>'bottom',-anchor=>'w'); my $label = $mw->Label(-text=>"hello")->pack(-side=>'left',-anchor=>'n +'); my $entry = $mw->Entry()->pack(-side=>'left',-anchor=>'n'); #$mw->bind('<Key-Return>',\&verwijs); $entry->bind('<Key-Return>',\&verwijs); $entry->focus; MainLoop; sub verwijs { my $who = $mw->focusCurrent; print %$who; }

      Much better, thank you.

      Secrets

      #!/usr/bin/perl -- # Tk-event-secrets.pl # # 2020-01-25-21:28:48 # # https://perlmonks.org/?node_id=11111879 # # #!/usr/bin/perl -- use strict; use warnings; use Tk; my $mw = new MainWindow; my $text = $mw->Scrolled( "Text", -scrollbars => 'e', )->pack( -side => 'bottom', -anchor => 'w', ); my $label = $mw->Label( -text => "hello" )->pack( -side => 'left', -anchor => 'n', ); my $entry = $mw->Entry()->pack( -side => 'left', -anchor => 'n', ); #~ $text->bind('<Key-Return>',\&verwijs); #~ $entry->bind('<Key-Return>',\&verwijs); $mw->bind( '<Key-Return>', \&verwijs ); $entry->focus; #~ $mw->WidgetDump; MainLoop; sub verwijs { printf "## %30s %s\n", '@_', "@_"; printf "## %30s %s\n", '$Tk::widget', $Tk::widget; printf "## %30s %s\n", 'Tk::event->W', $Tk::event->W; printf "## %30s %s\n", 'Tk::event', $Tk::event; printf "## %30s %s\n", '$mw->focusCurrent', $Tk::widget->toplevel->focusCurrent; printf "## %30s %s\n", $mw, $Tk::widget->toplevel; print "\n\n"; } __END__ $ perl Tk-event-secrets.pl ## @_ Tk::Entry=HASH(0x102ff2c) ## $Tk::widget Tk::Entry=HASH(0x102ff2c) ## Tk::event->W Tk::Entry=HASH(0x102ff2c) ## Tk::event XEvent=SCALAR(0x101099c) ## $mw->focusCurrent Tk::Entry=HASH(0x102ff2c) ## MainWindow=HASH(0xf3ad8c) MainWindow=HASH(0xf3ad8c) ## @_ Tk::Text=HASH(0xf20334) ## $Tk::widget Tk::Text=HASH(0xf20334) ## Tk::event->W Tk::Text=HASH(0xf20334) ## Tk::event XEvent=SCALAR(0x1010b9c) ## $mw->focusCurrent Tk::Text=HASH(0xf20334) ## MainWindow=HASH(0xf3ad8c) MainWindow=HASH(0xf3ad8c) ## @_ Tk::Scrollbar=HASH(0x10212d4) ## $Tk::widget Tk::Scrollbar=HASH(0x10212d4) ## Tk::event->W Tk::Scrollbar=HASH(0x10212d4) ## Tk::event XEvent=SCALAR(0x106d6e4) ## $mw->focusCurrent Tk::Scrollbar=HASH(0x10212d4) ## MainWindow=HASH(0xf3ad8c) MainWindow=HASH(0xf3ad8c)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11111879]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-23 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found