How to read key input?

I am trying to have a key control camera. There is no Onkeypress for TForm, so how can I read this input from the keyboard?

procedure TForm2.FormKeyPress(Sender: TObject; var Key: Char); var ok: boolean; begin ok := true; case Key of 'a': camera1.Position.y:=camera1.Position.y+1; 'A': camera1.Position.y:=camera1.Position.y+1; 'd': camera1.Position.y:=camera1.Position.y-1; 'D': camera1.Position.y:=camera1.Position.y-1; 'w': camera1.Position.X:=camera1.Position.X-1; 'W': camera1.Position.X:=camera1.Position.X-1; 'x': camera1.Position.X:=camera1.Position.X+1; 'X': camera1.Position.X:=camera1.Position.X+1; 'q': camera1.RotationAngle.z := camera1.RotationAngle.z-1; 'Q': camera1.RotationAngle.z := camera1.RotationAngle.z-1; 'e': camera1.RotationAngle.z := camera1.RotationAngle.z+1; 'E': camera1.RotationAngle.z := camera1.RotationAngle.z+1; 'z': camera1.Position.z:=camera1.Position.z+1; 'Z': camera1.Position.z:=camera1.Position.z+1; 'c': camera1.Position.z:=camera1.Position.z-1; 'C': camera1.Position.z:=camera1.Position.z-1; else ok := false; end; {case} //if ok then // Invalidate; positionChange(camera1); RotationAngleChange(camera1); end; 
+4
source share
1 answer

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 := #0; end; 

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 := #0; end; 

(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 // Invalidate; KeyChar := #0; // Remove keystroke, because you've handled it end; positionChange(camera1); RotationAngleChange(camera1); end; 
+5
source

Source: https://habr.com/ru/post/1494492/


All Articles