【问题标题】:Intercept TAB key and suppress it拦截 TAB 键并抑制它
【发布时间】:2012-05-15 06:01:18
【问题描述】:

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

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

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

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

【问题讨论】:

标签: delphi keyboard delphi-7


【解决方案1】:

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

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.

【讨论】:

  • 亲爱的 TLama,这非常完美。有史以来最好的剪切和粘贴 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
  • 1970-01-01
  • 2021-08-04
相关资源
最近更新 更多