procedure TFORM1.label1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then
SelectNext(Sender as TWinControl, True, True);
end;
在每个需要相应enter键的控件的ONKEYPRESS事件中都填写如上代码。
如果是用enter鍵來代替tab鍵就用下面的這個函數。
procedure SetReturn(Sender: TObject; Key: Word);
begin
case Key of
Vk_Return:
begin
Sendkeys('{TAB}');
end;
Vk_UP:
begin
Sendkeys('~{TAB}');
end;
end;
end;
function SendKeys(S: String): TSendKeyError;
{ This is the one entry point. Based on the string passed in the S }
{ parameter, this function creates a list of keyup/keydown messages, }
{ sets a JournalPlayback hook, and replays the keystroke messages. }
begin
Result := sk_None; // assume success
try
if Playing then raise ESKAlreadyPlaying.Create('');
MessageList := TMessageList.Create; // create list of messages
ProcessKey(S); // create messages from string
StartPlayback; // set hook and play back messages
except
{ if an exception occurs, return an error code, and clean up }
on E:ESendKeyError do begin
MessageList.Free;
if E is ESKSetHookError then
Result := sk_FailSetHook
else if E is ESKInvalidToken then
Result := sk_InvalidToken
else if E is ESKAlreadyPlaying then
Result := sk_AlreadyPlaying;
end
else
{ Catch-all exception handler }
Result := sk_UnknownError;
end;
end;