Intercept TAB key in RichEdit

孤街醉人 提交于 2019-12-23 16:50:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!