delphi - How to read key input? -


i trying have key control camera. there no onkeypress tform how can read input 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    // invalidate;   positionchange(camera1);   rotationanglechange(camera1); end; 

update latest version of xe2 (afaik update 4 hotfix 1) and

use tform.onkeydown or tform.onkeyup events instead. here's quick test used:

procedure tform1.formkeydown(sender: tobject; var key: word; var keychar: char;   shift: tshiftstate); begin   case keychar of     'a'..'z', 'a'..'z': caption := 'got alpha ' + keychar;     '0'..'9': caption := 'got number ' + keychar;   else     caption := 'got else ' + keychar;   end;   keychar := #0; end; 

according embarcadero forums post, in xe2 without update above need override tform.keydown event (added per @tlama's request; he's 1 located , posted in comments 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 alpha ' + keychar;     '0'..'9': caption := 'got number ' + keychar;   else     caption := 'got else ' + keychar;   end;   keychar := #0; end; 

(just note, can shorten 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   begin    // invalidate;    keychar := #0;  // remove keystroke, because you've handled   end;   positionchange(camera1);   rotationanglechange(camera1);       end; 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -