我想在我的表单中检测按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 C,这是一个不以Ctrl键开头的序列.如果这不符合有效的密钥组合,您可以借助标记保留一些密钥历史记录.以下应仅检测最初以Ctrl开头的序列:
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;