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

Re: [MacPerl] key event handler in dialogs



At 22.30 1998.04.20, Kevin Lindsey wrote:
>I'm simply trying to intercept the key presses in a dialog for a perticular
>edit text box.  I'll store the characters for each key and display bullets in
>their place.  I'm pretty sure that I can handle the display part, but I can't
>get MacPerl to call my key hook.

Actually, Alan Fry did exactly this already.  I think I might have modified
his version slightly.  Notice that we are not actually capturing a key
click event here, though.  There are two text items, one visible, one
hidden.  In between WaitNextEvent() calls, the lengths of both are looked
at it; if one is longer, they are synched up.  And this synching up means
making the visible dialog the same number of bullets as the hidden dialog
has characters (and you are typing in the hidden dialog).

Now, this does not exactly answer your question; the answer is that you
could capture the key click with:

  $dlg->sethook(key => \&some_sub);

That passes the dialog object and the decimal ASCII number of the key that
was pressed to the sub in @_.

But this probably is not as effective as Alan's method, because it would be
more difficult to handle things like deletes/backspaces.

#!perl -w

use Mac::Events;
use Mac::Windows;
use Mac::Dialogs;

$bnd = new Rect(10, 50, 200, 126);
$dlg = new MacDialog $bnd, 'Password', 1, 0, 1, (
    [ kEditTextDialogItem, new Rect(10, 15, 180, 30), '' ],
    [ kEditTextDialogItem, new Rect(10, 15, 180, 30), '' ],
    [ kButtonDialogItem,   new Rect(120,45, 180, 65), 'Return']
);

SetDialogDefaultItem($dlg->window, 3);
HideDialogItem($dlg->window, 1);
SelectDialogItemText($dlg->window, 1);
$dlg->item_hit(3 => \&return);
$dlg->sethook( "click", \&get_click );

while ($dlg->window) {
    if (!$len || length $dlg->item_text(1) ne $len) { text() }
    WaitNextEvent;
}

sub text {
    $txt = $dlg->item_text(1);
    $len = length $txt;
    $dlg->item_text(2, chr(165) x $len );
}

sub return {
    print $txt, "\n";
    dispose $dlg
}

sub get_click {
    my ($me, $pt) = @_;
    $it = FindDialogItem($dlg->window, $pt);
    if ($it == 2) { &return }
}

__END__

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10  1FF7 7F13 8180 B6B6'])
#==               New Book:  MacPerl: Power and Ease               ==#
#==    Publishing Date: Early 1998. http://www.ptf.com/macperl/    ==#



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch