I looked at the list of key commands in ‘castlekeysmouse’ and used some.
But I could not use the Numpad keys 'if Event.IsKey(keyNumpad1) then… does nothing.
Do I have to add addtional code to make the numpad work?
And I am looking for a smart way that if an event occurs after pressing key it should not repeat again and again while player, beta-testing my game annoyingly decides to keep the key pressed.
Any ideas?
Update: I found out I had to switch on numeric lock on my numpad.
But then I would like to use the numeric keys without switching this on.
There are two different values, depending on if NumLock is on or off. If you want to have a value despite the NumLock then simply:
case Event.Key of
keyNumpadUp, keyNumpad8: MoveUp;
keyNumpadDown, keyNumpad2: MoveDown;
keyNumpadLeft, keyNumpad4: MoveLeft;
keyNumpadRight, keyNumpad6: MoveRight;
end;
P.S. I didn’t test the code above but it should work without issues on all Desktop platforms (Windows/Linux).
This is a bit more complicated, as those are events generated by the operation system. Therefore you have to keep track of which keys were/are pressed something like:
var
EnterPressed: Boolean;
function TMyState.Press(const Event: TInputPressRelease): Boolean;
begin
Result := inherited;
if Event.IsKey(keyEnter) and (not EnterPressed) then
begin
EnterPressed := true;
DoEnterPress;
end;
end;
function TMyState.Release(const Event: TInputPressRelease): Boolean;
begin
Result := inherited;
if Event.IsKey(keyEnter) then
EnterPressed := false;
end;
However, depending on what exactly you are trying to achieve - usually you only need:
procedure TMyStateUpdate(const SecondsPassed: Single; var HandleInput: Boolean);
begin
inherited;
if Container.Pressed[keyArrowRight] then
MoveRight(SecondsPassed * Speed);
end;
Thanks! This even makes it possible to add more keys to the action. (I added KeyArrowUp).
case Event.Key of
keyNumpadUp, keyNumpad8, keyArrowUp: GoUp;
end;
Thanks, I will try out both these options!
Note: if you detect key press by TMyState.Press
(not observing the key in Update
) then you can “filter out” the repeated key presses by looking at TInputPressRelease.KeyRepeated
: Castle Game Engine: CastleKeysMouse: Object TInputPressRelease . So instead of tracking EnterPressed
as @eugeneloza described, you can also just do
function TMyState.Press(const Event: TInputPressRelease): Boolean;
begin
Result := inherited;
if Event.IsKey(keyEnter) and not Event.KeyRepeated then
DoEnterPress;
end;
1 Like
Thanks!
This is actually funny, because when I tried out an old Sierra adventure game, when I keep an arrow key pressed the sprite is triggered constantly instead of moving. So they had not figured this out in the past how to avoid.
But with KeyRepeated the movement is now fine for my game.
if Event.IsKey(keyArrowLeft) and not Event.KeyRepeated then
begin
if Player.Stand then GoLeft else StandLeft;
end;