2013-07-31 31 views
4

我想要一個關鍵的控制攝像頭。對於TForm沒有Onkeypress,所以我怎樣才能從鍵盤讀取這個輸入?如何閱讀按鍵輸入?

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; 
+1

TForm的事件具有onKeyDown和的onkeyup(XE3, XE4)。 – slotomo

+0

使用xe2生病添加該標記:( –

+0

OnKeyDown在事件中也缺失 –

回答

5

更新到XE2(據我所知更新4修補程序1)和

使用TForm.OnKeyDownTForm.OnKeyUp事件,而不是最新版本。這裏有一個快速測試我使用:

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; 

根據這一Embarcadero forums post,在XE2沒有更新上面實際上你需要重寫TForm.KeyDown事件(每@加TLama的請求,他是誰設,並張貼在一個評論我的回答):

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; 

(就像一張紙條,你可以一定程度上縮短你的代碼):

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; 
+0

Treid,剛剛,同樣的事情...我已經添加了一個showmessage()來查看該事件是否被調用,而不是。也沒有看到事件中的對象檢查器中的formkeydown或formkeyup –

+0

然後你還有其他事情正在進行,因爲(正如我所說),我測試了我發佈的代碼,並觀看了字幕更新,因爲我輸入了一個字母,一個數字,一個'@'符號。 –

+0

你用fmx和xe2測試過嗎? –