Binding KeyPress on a Text widget 
Author Message
 Binding KeyPress on a Text widget

Hi all,

I use a Text widget and want to catch all the keys pressed in that
widget.

I use "bind" for <KeyPress>.

When the callback is executed, how can I know which key was pressed ?

Thanks for your help.

Now, part of my code :

        my $text = $mainWin->Scrolled(
                'Text',
                -scrollbars => 'osoe',
                -font => 'Courier 8',
        )->pack->place(
                -x => 0,
                -y => 0,
                -relwidth => 1.0,
                -relheight => 1.0,
        );

        $text->bind('<KeyPress>' => \&textChange);

        sub textChange {
                my $key = ???????????
        }

--
   _____________________________________
 /                                       \
|           Patrick RENAUD                |
|                                         |

 \_______________________________________/



Tue, 20 Nov 2001 03:00:00 GMT  
 Binding KeyPress on a Text widget

Quote:

> Hi all,

> I use a Text widget and want to catch all the keys pressed in that
> widget.

> I use "bind" for <KeyPress>.

> When the callback is executed, how can I know which key was pressed ?

> Thanks for your help.

> Now, part of my code :

>    my $text = $mainWin->Scrolled(
>            'Text',
>            -scrollbars => 'osoe',
>            -font => 'Courier 8',
>    )->pack->place(
>            -x => 0,
>            -y => 0,
>            -relwidth => 1.0,
>            -relheight => 1.0,
>    );

>    $text->bind('<KeyPress>' => \&textChange);

>    sub textChange {
>            my $key = ???????????
>    }

Use
        my $e = $mainWin->XEvent;
        $e->A; # for the Ascii character
        $e->K; # for the Keysym

See the Tk::bind manpage.

Regards,
        Slaven

--
use Tk;$c=tkinit->Canvas->pack;$x=45;for(split/_/,'KPI1_+09IPK_K;-OA1_+K!;A__1;
Q!7G_1+QK_3CLPI90,_+K!;A_+1!KQ!.N_K+1Q!.F_1+KN.Q__1+KN._K+1Q!.F_1+KN.Q_+1Q__+1!
KQ!.N_1;Q!7G_K3,09Q_+1!K.Q_K+1Q!.F_1+KN.Q'){s/\n//g;for(split/!/){$c->create(
'line',map{$a=-43+ord;($x+($a>>3)*2,123+($a&7)*2)}split//)}$x+=12}MainLoop



Tue, 20 Nov 2001 03:00:00 GMT  
 Binding KeyPress on a Text widget
lifting code directly from Text.pm

...
use Tk qw (Ev);

$mw->bind($class,'<KeyPress>',['InsertKeypress', Ev('A')]);

sub InsertKeypress {

...

the bind causes the method ->InsertKeypress to be called
on <KeyPress> events and passes the method the
key just pressed as the first parameter. ($char)

Greg

Quote:

> Hi all,

> I use a Text widget and want to catch all the keys pressed in that
> widget.

> I use "bind" for <KeyPress>.

> When the callback is executed, how can I know which key was pressed ?

> Thanks for your help.

> Now, part of my code :

>         my $text = $mainWin->Scrolled(
>                 'Text',
>                 -scrollbars => 'osoe',
>                 -font => 'Courier 8',
>         )->pack->place(
>                 -x => 0,
>                 -y => 0,
>                 -relwidth => 1.0,
>                 -relheight => 1.0,
>         );

>         $text->bind('<KeyPress>' => \&textChange);

>         sub textChange {
>                 my $key = ???????????
>         }

> --
>    _____________________________________
>  /                                       \
> |           Patrick RENAUD                |
> |                                         |

>  \_______________________________________/



Tue, 20 Nov 2001 03:00:00 GMT  
 Binding KeyPress on a Text widget
More explicitly, from Learning Perl/Tk page 277,

   use Tk;
   $top = new MainWindow;
   $text = $top->Text->pack;
   $text->bind('<Key>', [ sub { print "Key:  $_[1]\n" }, Ev('K') ]);
   MainLoop;

Enjoy!

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Collin Starkweather                                 (303) 492-4784

Department of Economics          http://ucsu.colorado.edu/~olsonco
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quote:


> > Hi all,

> > I use a Text widget and want to catch all the keys pressed in that
> > widget.

> > I use "bind" for <KeyPress>.

> > When the callback is executed, how can I know which key was pressed ?

> > Thanks for your help.

> > Now, part of my code :

> >       my $text = $mainWin->Scrolled(
> >               'Text',
> >               -scrollbars => 'osoe',
> >               -font => 'Courier 8',
> >       )->pack->place(
> >               -x => 0,
> >               -y => 0,
> >               -relwidth => 1.0,
> >               -relheight => 1.0,
> >       );

> >       $text->bind('<KeyPress>' => \&textChange);

> >       sub textChange {
> >               my $key = ???????????
> >       }

> Use
>         my $e = $mainWin->XEvent;
>         $e->A; # for the Ascii character
>         $e->K; # for the Keysym

> See the Tk::bind manpage.

> Regards,
>         Slaven

> --
> use Tk;$c=tkinit->Canvas->pack;$x=45;for(split/_/,'KPI1_+09IPK_K;-OA1_+K!;A__1;
> Q!7G_1+QK_3CLPI90,_+K!;A_+1!KQ!.N_K+1Q!.F_1+KN.Q__1+KN._K+1Q!.F_1+KN.Q_+1Q__+1!
> KQ!.N_1;Q!7G_K3,09Q_+1!K.Q_K+1Q!.F_1+KN.Q'){s/\n//g;for(split/!/){$c->create(
> 'line',map{$a=-43+ord;($x+($a>>3)*2,123+($a&7)*2)}split//)}$x+=12}MainLoop



Tue, 20 Nov 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. newbie help with anonymous subroutine syntax for text widget key binding

2. overriding default right mouse button binding for text widget

3. Text widget and mouse bindings

4. binding PageUp and PageDown to a text widget

5. Text widget bindings

6. Binding keys in Text widget

7. Binding the TAB key in the Text widget

8. Text Widget turning off default bindings

9. Text Widget Bind Problem

10. Binding a right-click in a text widget

11. Arrow key bindings or Text widgets

12. Binding the TAB key in a Text widget

 

 
Powered by phpBB® Forum Software