简体   繁体   English

为什么在按下Ctrl + A时会触发TEdit.OnChange?

[英]Why does TEdit.OnChange trigger when Ctrl+A is pressed?

I am running a Delphi XE7 VCL app on Windows 7. 我在Windows 7上运行Delphi XE7 VCL应用程序。

I have observed that the TEdit.OnChange event triggers when Ctrl+A (select all) is pressed. 我观察到当按下Ctrl + A (全选)时触发TEdit.OnChange事件。 Why is that? 这是为什么?

I need to reliably trigger the OnChange event only when the text in the TEdit really changes. 只有当TEdit的文本真正发生变化时,才需要可靠地触发OnChange事件。 Unfortunately, no OnBeforeChange event is available so I can compare the text before and after a change. 不幸的是,没有OnBeforeChange事件可用,因此我可以比较更改前后的文本。

So, how to implement a reliable OnChange event for TEdit ? 那么,如何为TEdit实现可靠的OnChange事件?

Yes, it's not a bad base implementation: 是的,这不是一个糟糕的基础实现:

procedure TCustomEdit.CNCommand(var Message: TWMCommand);
begin
  if (Message.NotifyCode = EN_CHANGE) and not FCreating then Change;
end;

This message comes not taking in consideration that the 'A' that is the button that is firing the EN_CHANGE, currently comes together with the state of ctrl pressed. 此消息没有考虑到'A'是触发EN_CHANGE的按钮,当前与按下的ctrl状态一起出现。

What you can do is check if Ctrl is pressed or not: 您可以做的是检查是否按下了Ctrl:

procedure TForm44.edt1Change(Sender: TObject);

  function IsCtrlPressed: Boolean;
  var
    State: TKeyboardState;
  begin
    GetKeyboardState(State);
    Result := ((State[VK_CONTROL] and 128) <> 0);
  end;
begin
  if IsCtrlPresed then
    Exit;

  Caption := 'Ctrl is not pressed';
end;

To avoid reading the state of the whole key board, you can do what was suggested by David Heffernan: 为了避免阅读整个键盘的状态,你可以做David Heffernan的建议:

procedure TForm44.edt1Change(Sender: TObject);

  function IsCtrlPresed: Boolean;
  begin
    Result := GetKeyState(VK_CONTROL) < 0;
  end;
begin
  if IsCtrlPresed then
    Exit;

  Caption := 'Ctrl is not pressed';
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM