TEdit key press events in customdrawn LCLWidgetType
By 'lagprogramming' from forum.
Follow up to commit fa8f6ffa: LCL: Make AUTF8Char a var parameter in function LCLSendUTF8KeyPress. Issue...
lcl/interfaces/customdrawn/customdrawnprivate.pas has procedure CallbackKeyChar(AWindowHandle: TCDForm; AKeyData: Word; AChar: TUTF8Char);
The following code fixes the key events:
procedure CallbackKeyChar(AWindowHandle: TCDForm; AKeyData: Word; AChar: TUTF8Char);
var
lTarget: TWinControl;
lCharCode: Word;
begin
lTarget := AWindowHandle.GetFocusedControl();
{$ifdef VerboseCDEvents}
DebugLn(Format('CallbackKeyChar FocusedControl=%s:%s', [lTarget.Name, lTarget.ClassName]));
{$endif}
if lTarget = nil then Exit; // Fixes a crash
if Length(AChar) = 1 then
lCharCode := Byte(AChar[1])
else
lCharCode := 0;
if AChar<>'' then LCLSendUTF8KeyPress(lTarget, AChar, False);
if lCharCode <> 0 then LCLSendCharEvent(lTarget, lCharCode, AKeyData, True, False, True);
// If this is a interface control, send the message to the main LCL control too
if IsIntfControl(lTarget) then
begin
lTarget := lTarget.Parent;
if AChar<>'' then LCLSendUTF8KeyPress(lTarget, AChar, False);
if lCharCode <> 0 then LCLSendCharEvent(lTarget, lCharCode, AKeyData, True, False, True);
end;
end;
Regarding the modified procedure CallbackKeyChar, before the comment line "// If this is a interface control, send the message to the main LCL control too"
the value of AChar can be modified at line "if AChar<>'' then LCLSendUTF8KeyPress(lTarget, AChar, False);"
. Also the value of lCharCode can be modified too at line "if lCharCode <> 0 then LCLSendCharEvent(lTarget, lCharCode, AKeyData, True, False, True);"
.
After the comment line the message is sent to the main LCL control only if AChar<>'' for LCLSendUTF8KeyPress and only if lCharCode <> 0 for LCLSendCharEvent. Is this the right behavior?
This means that you can cancel sending the messages further to the LCL controls by modifying in one of the OnKey* events the value of AChar to '' or the value of lCharCode to 0. Look at the content of procedure TWinControl.KeyDown(var Key: Word; Shift: TShiftState); as it has that "if Key <> 0 then DoCallKeyEventHandler(chtOnKeyDown, Key, Shift);"
line. Also, function TWinControl.DoKeyPress(var Message : TLMKey): Boolean;
has an "if Char(CharCode) = #0 then Exit;"
line.
The following patch fixes the bug.
diff --git a/lcl/interfaces/customdrawn/customdrawnprivate.pas b/lcl/interfaces/customdrawn/customdrawnprivate.pas
index bcf33be060..7a95139583 100644
--- a/lcl/interfaces/customdrawn/customdrawnprivate.pas
+++ b/lcl/interfaces/customdrawn/customdrawnprivate.pas
@@ -288,7 +288,7 @@ end;
procedure CallbackKeyChar(AWindowHandle: TCDForm; AKeyData: Word; AChar: TUTF8Char);
var
lTarget: TWinControl;
- lCharCode: Word = 0;
+ lCharCode: Word;
begin
lTarget := AWindowHandle.GetFocusedControl();
{$ifdef VerboseCDEvents}
@@ -296,17 +296,20 @@ begin
{$endif}
if lTarget = nil then Exit; // Fixes a crash
- if Length(AChar) = 1 then lCharCode := Word(AChar[1]);
+ if Length(AChar) = 1 then
+ lCharCode := Byte(AChar[1])
+ else
+ lCharCode:=0;
-// if lCharCode <> 0 then LCLSendCharEvent(lTarget, lCharCode, AKeyData, True, False, True);
- LCLSendUTF8KeyPress(lTarget, AChar, False);
+ if AChar<>'' then LCLSendUTF8KeyPress(lTarget, AChar, False);
+ if lCharCode <> 0 then LCLSendCharEvent(lTarget, lCharCode, AKeyData, True, False, True);
// If this is a interface control, send the message to the main LCL control too
if IsIntfControl(lTarget) then
begin
lTarget := lTarget.Parent;
-// if lCharCode <> 0 then LCLSendCharEvent(lTarget, lCharCode, AKeyData, True, False, True);
- LCLSendUTF8KeyPress(lTarget, AChar, False);
+ if AChar<>'' then LCLSendUTF8KeyPress(lTarget, AChar, False);
+ if lCharCode <> 0 then LCLSendCharEvent(lTarget, lCharCode, AKeyData, True, False, True);
end;
end;