我想检测在我的表单中按下 3 个键,例如Ctrl+ C+ N...我需要检测的输入表单将始终以两个字母开头,Ctrl然后是两个字母。
我该怎么做?
在其中一把钥匙到达时,您可以查看另一把钥匙是否已经被按下。例如:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Shift = [ssCtrl] then begin
case Key of
Ord('C'):
if (GetKeyState(Ord('N')) and $80) = $80 then
ShowMessage('combo');
Ord('N'):
if (GetKeyState(Ord('C')) and $80) = $80 then
ShowMessage('combo');
end;
end;
end;
然而,这也会检测到例如N++ ,一个不以键开头的Ctrl序列。如果这不符合有效键组合的条件,您可以在标志的帮助下保留一些键历史记录。以下应仅检测最初以 开头的序列:CCtrlCtrl
type
TForm1 = class(TForm)
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
private
FValidKeyCombo: Boolean;
end;
...
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if FValidKeyCombo and (Shift = [ssCtrl]) then
case Key of
Ord('C'):
if (GetKeyState(Ord('N')) and $80) = $80 then
ShowMessage('combo');
Ord('N'):
if (GetKeyState(Ord('C')) and $80) = $80 then
ShowMessage('combo');
end;
FValidKeyCombo := (Shift = [ssCtrl]) and (Key in [Ord('C'), Ord('N')]);
end;
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
FValidKeyCombo := False;
end;
还有更简单的方法:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
If (GetKeyState(Ord('Q'))<0) and (GetKeyState(Ord('N'))<0) and (GetKeyState(VK_CONTROL)<0)
Then ShowMessage('You did it :)');
End;