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