# Numeric CastleKeys not working?

**URL:** https://forum.castle-engine.io/t/numeric-castlekeys-not-working/349
**Category:** Uncategorized
**Created:** [May 29, 2021, 9:43am UTC](https://forum.castle-engine.io/t/numeric-castlekeys-not-working/349 "2021-05-29T09:43:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)
#### Post date: [May 29, 2021, 9:43am UTC](https://forum.castle-engine.io/t/numeric-castlekeys-not-working/349/1 "2021-05-29T09:43:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)
#### Post date: [May 29, 2021, 12:00pm UTC](https://forum.castle-engine.io/t/numeric-castlekeys-not-working/349/2 "2021-05-29T12:00:03Z")

</div>

> [@Carring](#):
>
> 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:

```Pascal
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).

> [@Carring](#):
>
> event occurs after pressing key it should not repeat again and again

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:

```Pascal
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:

```Pascal
procedure TMyStateUpdate(const SecondsPassed: Single; var HandleInput: Boolean);
begin
  inherited;
  if Container.Pressed[keyArrowRight] then
    MoveRight(SecondsPassed * Speed);
end;

```

---

<div class="post-metadata">

### Author: ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)
#### Post date: [May 29, 2021, 1:05pm UTC](https://forum.castle-engine.io/t/numeric-castlekeys-not-working/349/3 "2021-05-29T13:05:25Z")

</div>

> [@eugeneloza](#):
>
> ```auto
> case Event.Key of
> keyNumpadUp, keyNumpad8: MoveUp;
> 
> ```

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;

---

<div class="post-metadata">

### Author: ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)
#### Post date: [May 29, 2021, 1:08pm UTC](https://forum.castle-engine.io/t/numeric-castlekeys-not-working/349/4 "2021-05-29T13:08:33Z")

</div>

Thanks, I will try out both these options!

---

<div class="post-metadata">

### Author: ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)
#### Post date: [May 31, 2021, 12:05pm UTC](https://forum.castle-engine.io/t/numeric-castlekeys-not-working/349/5 "2021-05-31T12:05:15Z")

</div>

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](https://castle-engine.io/apidoc-unstable/html/CastleKeysMouse.TInputPressRelease.html#KeyRepeated) . So instead of tracking `EnterPressed` as @eugeneloza described, you can also just do

```auto
function TMyState.Press(const Event: TInputPressRelease): Boolean;
begin
  Result := inherited;
  if Event.IsKey(keyEnter) and not Event.KeyRepeated then
    DoEnterPress;
end;
```

---

<div class="post-metadata">

### Author: ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)
#### Post date: [May 31, 2021, 1:44pm UTC](https://forum.castle-engine.io/t/numeric-castlekeys-not-working/349/6 "2021-05-31T13:44:38Z")

</div>

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.  
😉

```auto
 if Event.IsKey(keyArrowLeft) and not Event.KeyRepeated then
  begin
    if Player.Stand then GoLeft else StandLeft;
  end;
```
