i've encountered an odd behavior with a Lazarus/FPC application that may be worthy of further investigation. and to answer the obvious question - yes - i have found a (sortof) workaround.
the application is rather large, something that i've been developing and maintaining for a number of years. a few days ago i made a change to the main form's
OnKeyPress handler that involved, when a certain set of conditions were met, changing the value of the variable
Key (of type char) that is passed in. changing the value of
Key was done for convenience, as there were a few dozen following lines of code that i didn't wish to edit; i could have equally well just used a locally defined variable called
NewKey instead.
to my surprise, i now found that on those occasions when the value of Key was changed, the OnKeyPress handler would be called TWICE in quick succession. this made absolutely no sense, and i could not for the life of me figure out the cause!
a bit of research online brought up a smattering of postings on the web (going back some number of years) about similar things happening, but with no clear/definitive explanation or solution. however, several of the postings mentioned a possible connection with the form also having a
TMemo on it. my application does have a
TMemo on the main form, but normally this component sits hidden (
Memo1.Visible=
false,
Memo1.Enabled=
false). only occasionally is the component enabled, displaying some content that the user can select and copy to clipboard before being hidden again.
after much experimentation, i found that the double-keypress events (there were also matching double-keydown events) could seemingly be prevented by adding the following few lines of code into my application's startup code:
procedure TForm1.ApplicationPropertiesActivate(Sender: TObject);
const startup:boolean=true;
begin
if startup then // startup things that can/should not be performed until the main form is 'live'
begin
startup:=false;
Memo1.Visible:=true; ///////////////////////////////////////////////////////////////
Memo1.Enabled:=true; // WITHOUT these lines (in particular the SetFocus) we get //
Memo1.SetFocus; // doubling up of Form1 KeyPress events. Have no idea WHY it //
Memo1.Visible:=false; // happens, but the SetFocus prevents it! //
Memo1.Enabled:=false
end ///////////////////////////////////////////////////////////////
end
note that, by design, on the main form of my application there are
NO components that would normally have focus. also, while the above seems to work, i am not convinced that i have got to the bottom of the problem. the
feeling i have is that it is a focus-related issue, but then when i tinker more with the code i'll sometimes find a fix that works, then when i rearrange the code it doesn't... then i'll go back to the original fix and it too doesn't work! needless to say, in my
OnKeyPress handler i now do
not change the value of
Key - that seems to be the one way of (100% ???) avoiding the problem.
has anyone else encountered this sort of behavior?
i'd be interested in hearing any theories that others may have. i must admit, few GUI applications would normally have
no focused component, but then it is not unheard of. my
OnKeyDown,
OnKeyPress and
OnKeyUp events are all triggered from the main form, while my mouse events are all triggered from a
TPanel that covers the entire area of the main form (the panel then contains a
TImage that the user interacts with).
cheers,
rob :-)