Alain V
Alain V

Reputation: 331

Delphi XE5 can not trig Space bar

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

Upvotes: 1

Views: 1407

Answers (1)

Ken White
Ken White

Reputation: 125689

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;.

Upvotes: 1

Related Questions