当TEdit没有时,TMemo如何吃逃生钥匙?

时间:2014-03-21 15:10:26

标签: delphi

我正在尝试阻止TMemo(以及TRichEdit)控件进入Escape键。

如果用户专注于TEdit,则按Escape将触发表单以执行用户按下转义时表单执行的操作。如果用户专注于TMemo,则TMemo会禁止按下转义。

当然我可以做黑客攻击:

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
    if Key = #27 then
    begin
       //figure out how to send a key to the form
    end;
end;

但这并不理想(我必须处理转义键,而不是让表单处理它)。

当然我可以做黑客攻击:

Form1.KeyPreview := True;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
   if Key = #27 then
   begin
      //Figure out how to invoke what the form was going to do when the user presses escape
   end;
end;

但这并不理想(我必须处理转义键,而不是让表单处理它)。

因此我们将回答问题而不是问题

相反,我们将借此机会学习一些东西。当TMemo没有时,TEdit甚至 接收 the escape key相关联的keyPress事件是怎样的呢? / p>
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
    if Key = #27 then
    begin
        //never happens
    end;
end;

TEditTMemo与Windows EDIT common control相同。

为什么逃避绕过表单的 KeyPreview

如果我打开表单的KeyPreview,并且用户在Escape框中聚焦时按TEdit,并且设置了按钮的Cancel属性,表单将关闭和

  • 未触发Edit1.KeyPress事件
  • 未触发Form1.KeyPress事件

如果创建了一个操作,其ShortcutEsc,则无论用户关注何种控件,都不会引发KeyPress事件。

tl; dr TMemo.WantEscape属性在哪里?

1 个答案:

答案 0 :(得分:13)

您观察到的行为由WM_GETDLGCODE消息的处理控制。对于如下所示的备忘录:

procedure TCustomMemo.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
  inherited;
  if FWantTabs then Message.Result := Message.Result or DLGC_WANTTAB
  else Message.Result := Message.Result and not DLGC_WANTTAB;
  if not FWantReturns then
    Message.Result := Message.Result and not DLGC_WANTALLKEYS;
end;

对于编辑控件,VCL不会对WM_GETDLGCODE实施特殊处理,而基础Windows编辑控件会对其进行处理。

在标准的Win32应用程序中,Windows对话管理器发送WM_GETDLGCODE消息。但是Delphi并不构建在对话管理器之上,因此VCL负责发送WM_GETDLGCODE。它在CN_KEYDOWN处理程序中执行此操作。代码如下所示:

Mask := 0;
case CharCode of
  VK_TAB:
    Mask := DLGC_WANTTAB;
  VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN:
    Mask := DLGC_WANTARROWS;
  VK_RETURN, VK_EXECUTE, VK_ESCAPE, VK_CANCEL:
    Mask := DLGC_WANTALLKEYS;
end;
if (Mask <> 0) and
  (Perform(CM_WANTSPECIALKEY, CharCode, 0) = 0) and
  (Perform(WM_GETDLGCODE, 0, 0) and Mask = 0) and
  (GetParentForm(Self).Perform(CM_DIALOGKEY,
  CharCode, KeyData) <> 0) then Exit;

请注意,VK_RETURNVK_EXECUTEVK_ESCAPEVK_CANCEL都是混在一起的。这意味着VCL控件必须决定是否自己处理这些键,或让表单在其CM_DIALOGKEY处理程序中处理它们。

TCustomMemo.WMGetDlgCode可以看出,您可以使用WantReturns属性影响该选择。因此,只需将备忘录上的WantReturns设置为False,即可说服VCL让表单处理 ESC 。但是这也会阻止 ENTER 键到达备忘录,并使备忘录的用户输入新行变得相当棘手。他们必须用 CTRL + ENTER 来做。

实际上WantReturns实际上应该被命名为WantReturnsAndEscapesAndExecutesAndCtrlBreaks。 VCL设计师可以实现WantEscapes属性,但它不存在。

所以你不得不以某种方式处理它。就个人而言,我使用自己的派生备忘录控件。它会覆盖KeyDown方法并执行此操作:

procedure TMyMemo.KeyDown(var Key: Word; Shift: TShiftState);
var
  Form: TCustomForm;
  Message: TCMDialogKey;
begin
  inherited;
  if (Key=VK_ESCAPE) and (Shift*[ssShift..ssCtrl])=[]) then begin
    Form := GetParentForm(Self);
    if Assigned(Form) then begin
      // we need to dispatch this key press to the form so that it can 'press' 
      // any buttons with Cancel=True
      Message.Msg := CM_DIALOGKEY;
      Message.CharCode := VK_ESCAPE;
      Message.KeyData := 0;
      Message.Result := 0;
      Form.Dispatch(Message);
    end;
  end;
end;

实现此目标的另一种方法是处理CM_WANTSPECIALKEYWM_GETDLGCODE。这是一个粗略的插入器,说明了这种技术:

type
  TMemo = class(StdCtrls.TMemo)
  protected
    procedure CMWantSpecialKey(var Msg: TCMWantSpecialKey); message CM_WANTSPECIALKEY;
    procedure WMGetDlgCode(var Msg: TWMGetDlgCode); message WM_GETDLGCODE;
  end;

procedure TMemo.CMWantSpecialKey(var Msg: TCMWantSpecialKey);
begin
  case Msg.CharCode of
  VK_ESCAPE:
    Msg.Result := 0;
  VK_RETURN, VK_EXECUTE, VK_CANCEL:
    Msg.Result := 1;
  else
    inherited;
  end;
end;

procedure TMemo.WMGetDlgCode(var Msg: TWMGetDlgCode);
begin
  inherited;
  Msg.Result := Msg.Result and not DLGC_WANTALLKEYS;
end;