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

[MacPerl-Toolbox] forwarding events



Alan Fry writes Wed, 7 Jul 1999 19:33:48 +0100:

>There are really two issues here:
>   a)  advancing the focus from one edit field to another
>   b)  editing text in the field with the focus
>...
>The script below will probably do most of what you want. 
>Experiment with the Tab key, the Arrow keys, the Delete key, 
>text selection, 'Cut', 'Copy' and 'Paste' and mouse-clicks 
>to select fields or position the caret.
>

Thank you for the reply. But my intention is to change (:-) the behavior
of some key-presses without loosing the benefit of a MacDialog. 
This evening I found the command I was looking for in the dialogs.pm:

key KEY:	 Handle a keypress and return 1 if the key was handled. 

So I can use (take a look at the script below):

   $Mac::Events::Event[keyDown] = \&Handler;

to get all key-presses and handle the ones I want to. For the other keys
I can force the dialog to perform its "default action" using:

   $dialog->key($event->key)
   
I used this to write the script below. There I changed the behavior of
the cursor keys.
So one can "browse" quickly through large edit-arrays with the cursor
keys.
But there is a fatal bug. The System crashes after using a copy/paste
command. 
Alan Fry wrote:

>The other bug is more serious, namely if you run the Dialog
>non-modally (i.e. omitting the line '$dlg->modal') the MenuBar is not
>revised and use of 'Cut', 'Copy' or 'Paste' will cause a machine crash.

I wonder if the behaviour of my script belongs to the same bug or if I
forgot something?
Furthermore I was surprised because I couldn't even catch the crash
using:

   unless($event->modifiers == 128) {return};

in the body of my handler. I intended to return directly if any
modifying key was pressed. 
This prevents you from writing capital letters but not the system from
crashing after 
copy & paste actions. I wonder why?

There's also a very strange minor bug. The Handler doesn't handle the
very first event.
So the first written letter can be capital and if you press a curser key
as the very first key, 
nothing happens. Can someone tell me why? 

											
											Jan Hofmann

#########################################################################################
#!perl

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

$box = Rect->new(0, 0, 50, 15);
$win = Rect->new(20, 50, 230, 145);

$dlg = MacDialog->new($win,"", 1, 4, 1, (
    [ kEditTextDialogItem, OffsetRect($box,  10, 10), '' ],
    [ kEditTextDialogItem, OffsetRect($box,  80, 10), '' ],
    [ kEditTextDialogItem, OffsetRect($box, 150, 10), '' ],
    [ kEditTextDialogItem, OffsetRect($box,  10, 40), '' ],
    [ kEditTextDialogItem, OffsetRect($box,  80, 40), '' ],
    [ kEditTextDialogItem, OffsetRect($box, 150, 40), '' ],
    [ kEditTextDialogItem, OffsetRect($box,  10, 70), '' ],
    [ kEditTextDialogItem, OffsetRect($box,  80, 70), '' ],
    [ kEditTextDialogItem, OffsetRect($box, 150, 70), '' ],
  )
);

$dlg->modal;
$Mac::Events::Event[keyDown] = \&changeEditField;# set the handler
WaitNextEvent while $dlg->window;
dispose $dlg;

sub  changeEditField{
  my $ev    =   $_[0];
  my $field=  $dlg->window->editField + 1;
  my $char=  $ev->character;

 unless($ev->modifiers == 128) {return};# return if modifiers here
pressed

  # set $field to the item number new active editing field 
  # if a cursor-key was pressed:
  if    ($char == 31) {$field = ($field + 2)% 9 + 1 }
  elsif ($char == 30) {$field = ($field - 4) % 9 + 1 }
  elsif ($char == 29) {
     if ($field % 3==0) {$field-=2}
     else   {$field =  $field  % 9 + 1 }
  }
  elsif ($char == 28){
      if ($field%3==1){$field+=2}
      else {$field = ($field -2)  % 9 + 1 }
  }

  else {$dlg->key($ev->key); return};# get the default action of the
dialog

  my $len=length($dlg->item_text($field) );
  #change the active editing field:
  SelectDialogItemText($dlg->window, $field,$len,$len);
};

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