用一个数字存放当前是连续的第几个字符,再加上一个Timer即可,代码如下:
(在From上放一个Timer)
const
SeqKeys:array[0..5]of Char=('a','b','c','d','e','f');
var
CurKeyIdx:Integer=0;
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled:=false;
Timer1.Interval:=10000
//10秒
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
CurKeyIdx:=0
//如果超时,则将位置复零
Timer1.Enabled:=false;
end;
procedure TForm1.FormKeyPress(Sender: TObject
var Key: Char);
begin
if Key=SeqKeys[CurKeyIdx] then
begin
if CurKeyIdx=0 then //输入第一个正确字符时,激活Timer,
Timer1.Enabled:=true;
Inc(CurKeyIdx);
if CurKeyIdx>High(SeqKeys) then
begin
Beep;
Timer1.Enabled:=false;
CurKeyIdx:=0
//位置复零
end;
end
else begin //序列匹配失败
CurKeyIdx:=0
//位置复零
if Key=SeqKeys[0] then //如果匹配上第一个...
begin
Timer1.Enabled:=true;
Inc(CurKeyIdx);
end;
end;
end;