拦截 TAB 键并抑制它 [英] Intercept TAB key and suppress it

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

问题描述

我需要截取 TEdits 上的 TAB 键盘敲击并以编程方式抑制它们.在某些情况下,我不希望焦点切换到下一个控件.

I need to intercept the TAB keyboard stroke on TEdits and suppress them programmatically. In certain cases I don't want the focus to change to the next control.

我尝试使用 KeyPreview=true 在 TEdit 级别和 TForm 上处理 KeyPress、KeyDown.我偷看了以下建议:

I tried to handle KeyPress, KeyDown both on TEdit level and on TForm with KeyPreview=true. I've peeked advices from:

但它没有用.事件被触发,比方说,Enter 键但不是 TAB 键.

But it didn't work. The events are fired for, let's say, the Enter key BUT not for the TAB key.

我正在使用 Delphi 7.感谢您的帮助.

I'm using Delphi 7. Thanks for your help.

推荐答案

如果你想拦截TAB键行为,你应该捕获CM_DIALOGKEY消息.在此示例中,如果您将 YouWantToInterceptTab 布尔值设置为 True,则 TAB 键将被吃掉:

If you want to intercept the TAB key behavior, you should catch the CM_DIALOGKEY message. In this example, if you set the YouWantToInterceptTab boolean value to True, the TAB key will be eaten:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
  private
    YouWantToInterceptTab: Boolean;
    procedure CMDialogKey(var AMessage: TCMDialogKey); message CM_DIALOGKEY;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CMDialogKey(var AMessage: TCMDialogKey);
begin
  if AMessage.CharCode = VK_TAB then
  begin
    ShowMessage('TAB key has been pressed in ' + ActiveControl.Name);

    if YouWantToInterceptTab then
    begin
      ShowMessage('TAB key will be eaten');
      AMessage.Result := 1;
    end
    else
      inherited;        
  end
  else
    inherited;
end;

end.

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

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