[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Capturing keystrokes with Mac::Windows andMac::Events



At 09.40 -0400 1999.04.25, Quentin Smith wrote:
>Hi. I'd like to know how to find out what key was pressed from a key event
>handler, as in
>
>$win->sethook('key' => \&key_press);

This is discussed in the MacPerl book, in the GUI chapter.  The handler is
passed two values, the event's object ($win in your example) and the value
of the key that is passed.  So if ord('v') is the value of the second
parameter, then 'v' was pressed.  You can get the actual character
(sometimes) with:

  sub key_press {
    my($obj, $value) = @_;
    print chr($value), "\n";
  }

I say sometimes because some keys are not printable (like the arrow keys).
I do tests like:

  sub key_press {
    my($obj, $value) = @_;
    if ($value == ord('a')) {
      do_something($obj);
    } elsif ($value == 28) { # left arrow key, i think
      do_something_else($obj);
    }
  }

Also, each sub here should return 1 if successful to signify that the key
event was handled.

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org