Why does TEdit.OnChange work when I press Ctrl + A?

I am running a Delphi XE7 VCL application for Windows 7.

I noticed that the TEdit.OnChange event fires when you press Ctrl + A (select all). Why is this?

I need to reliably trigger the OnChange event only when the text in TEdit really changes. Unfortunately, there is no OnBeforeChange event, so I can compare the text before and after the change.

So, how to implement a reliable OnChange event for TEdit ?

+5
source share
1 answer

Yes, this is 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 does not take into account that “A,” which is the button that launches EN_CHANGE, currently comes with ctrl pressed.

What you can do is check if the Ctrl key is pressed or not:

 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; 

In order not to read the state of the entire keyboard, you can do what was suggested by 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; 
+3
source

Source: https://habr.com/ru/post/1264207/


All Articles