From: Robert M. <rm...@po...> - 2006-01-09 23:24:40
|
This was menat to go to the lit as well as to Glenn M. Regards, Rob. Robert May wrote: > Glenn W Munroe wrote: >=20 >> It would be useful to be able to define handlers for key presses on a=20 >> per-control basis. For example, it is common to end text entry with a=20 >> press of the =93Enter=94 key; the action on that key press could be=20 >> different depending on the control. I have tried to define different=20 >> accelerator tables for different controls, but that doesn=92t work. Is= =20 >> this something that is broken, hasn=92t yet been implemented or is tha= t=20 >> way by design? I see that there is an {-accel} key for every control=92= s=20 >> hash, but I haven=92t figured out how to use it. >=20 > I've had a quick look through the code, and it looks like it was never=20 > intended that the -accel option was used on anything other than a=20 > top-level window. >=20 > I don't think it would be hard to have it work on a per control basis=20 > (although I would assume that we would want it to fall through to the=20 > top level windows, so that we don't have to define menu accelerators fo= r=20 > each control accelerator table). >=20 > If you think this would be useful, could you raise an RFE. >=20 >> I have also tried to handle the =93KeyUp/KeyDown=94 events, but they d= on=92t=20 >> ever seem to fire. To which controls do those events apply? >=20 > Pretty much all of them. The code below shows them working for a butto= n=20 > controls. >=20 >> The only way I have found to do this is to define a window-level=20 >> accelerator table and perform the relevant action depending on which=20 >> control has focus. That=92s ugly and it=92s a pain to get the control=92= s=20 >> name from its handle (perhaps that would be a useful method to build=20 >> into the module). Even that is not trivial in the case of, say, a=20 >> combobox, where it isn=92t the combobox itself that has focus, but the= =20 >> dynamically created child edit control. >> >> Has anybody come up with an elegant solution for this? >=20 >=20 > Is this elegant enough for you? >=20 > Regards, > Rob. >=20 > #!perl -w > use strict; > use warnings; >=20 > use Win32::GUI qw(WM_SETFOCUS); >=20 > my $mw =3D Win32::GUI::Window->new( > -title =3D> "Accel Tables", > -pos =3D> [100,100], > -size =3D> [400,300], > #-dialogui =3D> 1, > ); > $mw->Hook(WM_SETFOCUS, \&setAccel); >=20 > # When -dialoui =3D> 1 is specified on the window, then button > # will stop recieiving WM_CHAR (onChar) events; read about > # WM_GETDLGCODE for why. > my $but =3D $mw->AddButton( > -text =3D> "Button", > -pos =3D> [10,10], > -tabstop =3D> 1, > -notify =3D> 1, > -onGotFocus =3D> \&setAccel, > -onKeyDown =3D> sub {print "Button onKeyDown, $_[2]\n"}, > -onChar =3D> sub {print "Button onChar, $_[2]\n"}, > -onKeyUp =3D> sub {print "Button onKeyUp $_[2]\n"}, > ); >=20 > my $com =3D $mw->AddCombobox( > -text =3D> "Default", > -pos =3D> [10,40], > -size =3D> [100,100], > -tabstop =3D> 1, > -onGotFocus =3D> \&setAccel, > ); >=20 > # A set of accelerator tables, keyed by the stringified object referenc= e, > # allows for simple lookup in a common GotFocus handler > my %accels =3D ( > $mw =3D> Win32::GUI::AcceleratorTable->new( > "A" =3D> sub{print "Accel a pressed\n";1;}, > ), > $but =3D> Win32::GUI::AcceleratorTable->new( > "B" =3D> sub{print "Accel b pressed\n";1;}, > ), > $com =3D> Win32::GUI::AcceleratorTable->new( > "C" =3D> sub{print "Accel c pressed\n";1;}, > ), > ); >=20 > $mw->Show(); > Win32::GUI::Dialog(); >=20 > exit(0); >=20 > sub setAccel > { > my ($self) =3D @_; >=20 > # Really should cope with there not being a handler in the hash > # better. > $mw->Change(-accel =3D> $accels{$self}) if exists $accels{$self}; >=20 > return 1; > } > __END__ |