bind only "normal" keys in Tk

Discussion in 'Perl Misc' started by Larry, Dec 21, 2005.

  1. Larry

    Larry Guest

    In my Tk application, I would like to a routine to be called whenever a
    "normal" key is pressed in a certain Text control. By "normal", I mean
    a regular ASCII key, not an Alt or Shift key by itself. However, I
    can't figure out how to do that directly. The only thing I've figured
    out so far is (with no thanks to the Tk documentation, by the way):

    $txt->bind('<KeyPress>', \&doKey);

    sub doKey {
    my $c = shift;
    my $k = $c->XEvent->K;

    # code which looks at $k to see if it's "normal"
    }

    This works but it does not seem very efficient. I would like a way
    that "doKey" would not get called at all for Alt and Shift keys.
    Larry, Dec 21, 2005
    #1
    1. Advertising

  2. Larry

    Anno Siegel Guest

    Larry <> wrote in comp.lang.perl.misc:
    > In my Tk application, I would like to a routine to be called whenever a
    > "normal" key is pressed in a certain Text control. By "normal", I mean
    > a regular ASCII key, not an Alt or Shift key by itself. However, I


    ASCII vs. non-ASCII is the wrong distinction here. A "normal" key
    would be one that delivers a character (no matter which code), as opposed
    to the modifier keys Alt, Shift, etc. that don't.

    > can't figure out how to do that directly. The only thing I've figured
    > out so far is (with no thanks to the Tk documentation, by the way):
    >
    > $txt->bind('<KeyPress>', \&doKey);
    >
    > sub doKey {
    > my $c = shift;
    > my $k = $c->XEvent->K;
    >
    > # code which looks at $k to see if it's "normal"
    > }
    >
    > This works but it does not seem very efficient. I would like a way
    > that "doKey" would not get called at all for Alt and Shift keys.


    Have you read all the documentation for ->bind? You can restrict key
    bindings to any key you want. Just bind all the keys you want a binding
    for and leave others alone. To bind all alphanumeric keys (untested):

    $txt->bind( "<KeyPress-$_>", sub { print "it happened\n" }) for
    'A' .. 'Z', 'a' .. 'z', 0 .. 9;

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 21, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,582
    mr2_93
    Oct 2, 2005
  2. Replies:
    4
    Views:
    381
    Guillaume
    Nov 20, 2005
  3. sandeep Kanwal

    serial keys/validation keys

    sandeep Kanwal, Oct 29, 2004, in forum: C++
    Replies:
    1
    Views:
    577
    Mike Wahler
    Oct 29, 2004
  4. Replies:
    0
    Views:
    579
  5. Eric
    Replies:
    1
    Views:
    299
Loading...

Share This Page