Update to the latest version of XE2 (AFAIK Update 4 Hotfix 1) and
use TForm.OnKeyDown or TForm.OnKeyUp events. Here is a quick test that I used:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState); begin case KeyChar of 'A'..'Z', 'a'..'z': Caption := 'Got an alpha ' + KeyChar; '0'..'9': Caption := 'Got a number ' + KeyChar; else Caption := 'Got something else ' + KeyChar; end; KeyChar :=
According to this Embarcadero forum forum , in XE2 without updating the above, you really need to redefine the TForm.KeyDown event (added for @TLama request, he is the one who posted it and posted it in the comments to my answer):
type TForm1 = class(TForm) Memo1: TMemo; private public procedure KeyDown(var Key: Word; var KeyChar: Char; Shift: TShiftState); override; end; implementation procedure TForm1.KeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState); begin case KeyChar of 'A'..'Z', 'a'..'z': Caption := 'Got an alpha ' + KeyChar; '0'..'9': Caption := 'Got a number ' + KeyChar; else Caption := 'Got something else ' + KeyChar; end; KeyChar :=
(As a note, you can shorten the code a bit):
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState); begin ok := true; case KeyChar of 'A', 'a': camera1.Position.y:=camera1.Position.y+1; 'D', 'd': camera1.Position.y:=camera1.Position.y-1; 'W', 'w': camera1.Position.X:=camera1.Position.X-1; 'X', 'x': camera1.Position.X:=camera1.Position.X+1; 'Q', 'q': camera1.RotationAngle.z := camera1.RotationAngle.z-1; 'E', 'e': camera1.RotationAngle.z := camera1.RotationAngle.z+1; 'Z', 'z': camera1.Position.z:=camera1.Position.z+1; 'C', 'c': camera1.Position.z:=camera1.Position.z-1; else ok := false; end; {case} if ok then begin
source share