RichEdit中的Intercept TAB键 [英] Intercept TAB key in RichEdit

查看:79
本文介绍了RichEdit中的Intercept TAB键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有很多类似的问题,但是我找不到我的问题的答案。

There are lot of similar questions on here, but I couldn't find an answer for my problem.

我有一个 TRichEdit ,并希望在用户按下 Tab 时实现一些自定义行为。我将丰富的编辑的 WantTabs 属性设置为 True ,并尝试在 OnKeyDown中添加我的自定义行为,它可以正常工作,但不幸的是,此后还执行了正常标签行为(在编辑中插入标签字符)。我尝试在事件处理程序中将 Key 设置为 0 ,但这无济于事。

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?

推荐答案

使用<而是使用code> OnKeyPress 事件:

procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = chr(VK_TAB) then
  begin
    beep;
    Key := #0;
  end;
end;

或者,如果您确实需要使用 OnKeyDown 事件,只需删除以下关键消息:

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;

这篇关于RichEdit中的Intercept TAB键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆