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

[MacPerl-Toolbox] forewarding events



My Problem is that I can't forward an event if I don't want
to handle it. To demonstrate the problem I wrote a tiny script in which
a
dialog box is displayed with nine editing fields. The keyDown event is
led to a soubroutine:

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

which makes it possible to redefine the cursor buttons, allowing you to
shift the activated editing field in all four directions.
The way I understand it, editing fields in a dialog box should still be
editable, automatically using the code at the bottom of the message, but
with the following soubroutine as replacement:


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

  if    ($char == 31){$field = ($field + 2) % 9 + 1 }
  elsif ($char == 30){$field = ($field - 4) % 9 + 1 }
  elsif ($char == 29){$field =  $field      % 9 + 1 }
  elsif ($char == 28){$field = ($field -2)  % 9 + 1 }
  else  {return 0}; # event not handled

  my $len=length($dlg->item_text($field) );
  SelectDialogItemText($dlg->window, $field,$len,$len);
};


But this doesn't work. The characters don't appear on the editing
fields. So I wrote a version of this soubroutine in which I handle all
key presses (Well, just the ones I intend to use in this dialog box).
This works, but there must be a better way and there are other cases
where it isn't possible to cheat that way. So I would be very glad if
someone could tell my how to do better. 

The working version of the script is shown below. It comes with a very
small resource file. I decided to
attach it, enabling you to run the script. 

														Jan Hofmann
#####################################################

#!perl

use Mac::Resources;
use Mac::Events;
use Mac::Dialogs;


$resources=OpenResFile('Bubu:Desktop Folder:Res4'); 
# The resource file has to be in the Desktop Folder
$dlg =  new MacDialog 128 or die $^E;

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

WaitNextEvent while $dlg->window;

dispose $dlg;

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

  if       ($char == 31){$field = ($field + 2)% 9 + 1 }
  elsif ($char == 30){$field = ($field - 4) % 9 + 1 }
  elsif ($char == 29){$field =  $field         % 9 + 1 }
  elsif ($char == 28){$field = ($field -2)  % 9 + 1 }
  elsif ($char==8){
       chop($text);
       $dlg->item_text($field, $text)}
  else {
       $dlg->item_text($field, $text . chr($char))
  };

  my $len=length($dlg->item_text($field) );
  SelectDialogItemText($dlg->window, $field,$len,$len);
};

application/applefile

Res4