I have a form with 2 CheckBoxes (1 is for holding CTRL - 1 is for holding ALT).
Both CheckBoxes are disabled so the KeyDown Event
of the form works properly.
There is also a TTimer
that synchronizes every 10ms if the ALT/CTRL key is pressed.
My timer
:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
CheckBox1.Checked := ALTDOWN; // ALTDOWN IS GLOBAL
CheckBox2.Checked := CTRLDOWN; // CTRLDOWN IS GLOBAL
end;
My KeyDown Event
:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_MENU then begin
ALTDOWN := TRUE;
exit;
end;
if Key = VK_CONTROL then begin
CTRLDOWN := TRUE;
exit;
end;
end;
My KeyUP Event
:
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_MENU then begin
ALTDOWN := FALSE;
exit;
end;
if Key = VK_CONTROL then begin
CTRLDOWN := FALSE;
exit;
end;
end;
This works without any problems with the CTRL key. But the ALT key get's stuck sometimes or does not even show up at all. This happens when I press ONLY the ALT key (without any other keys in combination).
Why is that and how can I fix this?
If you are going to run a timer in that way, then you may as well just call GetAsyncKeyState
procedure TForm1.Timer1Timer(Sender: TObject);
begin
CheckBox1.Checked := GetAsyncKeyState(VK_MENU)<0;
CheckBox2.Checked := GetAsyncKeyState(VK_CONTROL)<0;
end;
And just get rid of your OnKeyDown
and OnKeyUp
event handlers. There's really no point in you trying to keep track of whether the key is up or down when the system already does so.
Your timer interval is quite short. The system won't fire them that frequently anyway. If I recall correctly the timer resolution is typically around 50ms.