TWebStringGrid keypress not interceptable

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.

Can you try following, edit WEBLib.Grids.pas

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;

to see if this helps for your case.

I tried that modification, but unfortunately it didn’t work.

You effectively implemented the grid.OnKeyPress event handler, it was triggered and you return Key as #0 when the key needs to ignored?

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.

We rechecked and suggest to change the code like:

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;

We double-checked and it works fine here.

Thank you very much! I can confirm it works now. Will you include this into the regular release of WebCore?

Yes, will be in the next release