1

With Delphi XE5, The SpaceBar Can not be trig when using FormKeyUp or KeyDown Method.

The Key value is 0 (instead of 32) if you it the spacebar. This was working on XE2.

procedure TfrmMaster.KeyDown(var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
begin
  if Key = vkSpace then
  begin
    //custom handling
    //if SomeTest then Exit; //don't do default handling
  end;
  inherited; //do default handling
end;

Type is Desktop HD Target is Windows 32/64 bits and Mac OS

Alain V
  • 311
  • 3
  • 17
  • Thanks for the edit. It makes the question much more clear. I've removed my close vote and posted an answer. :) – Ken White Nov 06 '13 at 01:31

1 Answers1

1

I can reproduce your problem in a new FMX HD application. A quick test shows that vkSpace is never sent.

  • Create a new Firemonkey HD Desktop application
  • On the Events tab in the Object Inspector, double-click the OnKeyDown event, and add the following code:
    procedure TForm4.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
      Shift: TShiftState);
    begin
      {
      if KeyChar = #32 then
      begin
        ShowMessage('Got space bar');
        KeyChar := #0;
      end;
      }
      if Key = vkSpace then
      begin
        ShowMessage('Got space bar');
        Key := 0;
      end
      else
        ShowMessage('Received key ' + IntToStr(Key));
    end;
  • Run the application, and press Space.
  • The second ShowMessage executes, and indicates it Received key 0.

There's a simple workaround. I tested using the following code:

    procedure TForm4.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
      Shift: TShiftState);
    begin
      if KeyChar = #32 then
      begin
        ShowMessage('Got space bar');  // Display message
        KeyChar := #0;                 // Discard keystroke
      end;
    end;

To allow the default processing of the keystroke after the ShowMessage call, simply remove the KeyChar := #0;.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Thanks, It's working on my set too by using KeyChar instead of Key parameter. On XE2 I was unsing Key parameters and it was working, when I have oppenned the project in XE5 the Key parameters seem to no longer working. – Alain V Nov 06 '13 at 02:04