Using the latest WebCore, I try to intercept some keypresses in a TWebStringGrid component (by checking the Key variable and then setting Key:=#0 to suppress some characters). The event is fired correctly, but this assignment is not working - i.e. the suppression doesn’t work.
In a normal TWebEdit control, this is possible. It would be nice to have this functionality also in the TWebStringGrid component.
Best regards,
Meik Stoll
PS: I tried a solution by assigning a standard TWebEdit control to the grid (using the OnGetEditControl event), but this isn’t allowing a smooth editing (enter a value, press ENTER to go to the next line…) like it is when using the standard grid editing.
function TCustomStringGrid.HandleEditKeyPress(Event: TJSKeyboardEvent): boolean;
var
c: char;
begin
Event.cancelBubble := true;
Event.stopPropagation;
Result := true;
if IsKeyCharacter(Event.Key) then
begin
c := Chr(GetKeyCode(Event.Key, false));
if Assigned(OnKeyPress) then
OnKeyPress(Self, c);
//ADD THIS CODE
if c = #0 then // <- HERE
PreventDefault; // <- HERE
end;
end;
Yes, I modified the file WEBLib.Grids.pas in the Core Source folder and inserted your lines to it, and additionally added a call to console.log() to check whether the lines get executed.
They do, but the key can not be suppressed. I tested with Chrome and with Firefox.
function TCustomStringGrid.HandleEditKeyPress(Event: TJSKeyboardEvent): boolean;
var
c: char;
begin
Event.cancelBubble := true;
Event.stopPropagation;
Result := true;
if IsKeyCharacter(Event.Key) then
begin
c := Chr(GetKeyCode(Event.Key, false));
if Assigned(OnKeyPress) then
OnKeyPress(Self, c);
if c = #0 then
begin
Event.stopPropagation;
Event.preventDefault;
end;
end;
end;