bind only "normal" keys in Tk

L

Larry

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.
 
A

Anno Siegel

Larry said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top