4

I am trying to have a key control camera. There is no Onkeypress for TForm so how can i read this input from 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; 
8
  • 1
    TForm have event OnKeyDown and OnKeyUp (XE3, XE4). Commented Jul 31, 2013 at 12:40
  • using xe2 ill add that tag :( Commented Jul 31, 2013 at 12:41
  • OnKeyDown is missing in events also Commented Jul 31, 2013 at 12:52
  • 1
    @TLama I think the documentation is very true ... and tells about events that are public, but they have to be published to be visible inside OI Commented Jul 31, 2013 at 13:41
  • 2
    Ok XE2 Update 4 Hotfix 1 and there are a lot more events visible in OI OnKeyDown,OnKeyUp,OnMouse... so this question targets a fixed bug Commented Jul 31, 2013 at 13:44

1 Answer 1

5

Update to the latest version of XE2 (AFAIK Update 4 Hotfix 1) and

use TForm.OnKeyDown or TForm.OnKeyUp events instead. Here's a quick test 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 forums post, in XE2 without the update above you need to actually override the TForm.KeyDown event (added per @TLama's request; he's the one who located it and posted 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; 

(Just as a note, you can shorten your code somewhat):

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; 
Sign up to request clarification or add additional context in comments.

10 Comments

Treid that just now , same thing... and i have added a showmessage() to see if that event is even being called and its not. Also dont see formkeydown or formkeyup in the object inspector under events
Then you have something else going on, because (as I said) I tested with the code I posted, and watched the caption update as I typed a letter, a number, and an @ sign.
did you test with fmx and xe2?
Wait. I stand corrected. It's not in XE2 (I missed that tag, and mistakenly used XE4). I'll delete my answer shortly, unless I can find a workaround.
I had help from TLama and Sir Rufo putting the pieces together. :-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.