问题
There are lot of similar questions on here, but I couldn't find an answer for my problem.
I have a TRichEdit
and want to implement some custom behaviour when the user presses Tab. I set the rich edit's WantTabs
property to True
and tried to add my custom behaviour in OnKeyDown
, which works fine, but unfortunately after that the "normal" tab behaviour is executed as well (inserting a tab character in the edit). I tried setting Key
to 0
in the event handler but that doesn't help.
How can I prevent the "normal" tab behaviour from being executed?
回答1:
Use the OnKeyPress
event instead:
procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = chr(VK_TAB) then
begin
beep;
Key := #0;
end;
end;
Alternatively, if you really need to use the OnKeyDown
event, simply remove the key messages:
procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
msg: TMsg;
begin
if Key = VK_TAB then
begin
beep;
while PeekMessage(msg, RichEdit1.Handle, WM_KEYFIRST, WM_KEYLAST,
PM_REMOVE) do;
end;
end;
来源:https://stackoverflow.com/questions/7104006/intercept-tab-key-in-richedit