in reply to Keyboard input with Perl/Tk's -bind()
To address the first part of the question: Tk handles the callbacks different, based on the type of reference the programmer provides. If the reference is a CODE references (your second, working version), it simply calls it with the object to which the callback is tied as the first argument. That object is a blessed hash reference, which is why you saw that in the first example, as well. But in your second example, you never used the arg-list of the callback, rather, you directly called a second routine with the Ev() as the argument. When the reference passed to bind() is an ARRAY reference, it treats the first element as the coderef to call, and the rest as extra arguments. It still passes the object as the first argument, however. That is why your first attempt yielded a hashref.
You do want to use the first form, however. When Tk is building the argument list for the callback, it detects the presence of "Ev(x)" and evaluates them in the context of the current event before placing the values on the calling stack. Your closure might prevent this (I'm not 100% sure that it will, but I can't imagine Tk being able to pick apart the anon-sub). What your "mysub" needs to do is expect the first argument to be the object ref, and look to the second argument to be the key.
As for the codes for the arrow keys and such, it isn't as simple as plain ASCII codes for a given key. The keyboard (and thus X) considers the keypad numbers separately from the row across the top. If and when both yield the same ASCII characters is a matter of the application-- generally when you are typing text, it's fine to have them both generate the appropriate ASCII value. But as keys, that have codes that are known as keysyms associated with them. Your X environment should have a utility called "xev" installed. When you run it, it just spews info on X events to stdout. Try it, and look at what gets output for the arrows and the keypad keys. You'll find that the keypad numbers have keysyms like "KP_0", "KP_5", etc.
Welcome to the world of X and event-based programming!
--rjray
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Keyboard input with Perl/Tk's -bind()
by JPaul (Hermit) on Feb 12, 2002 at 00:54 UTC |