Using Keyboard Input

The Keyboard Object

Each Kiwi.js game automatically creates and starts a Keyboard object on boot up which may or may not be used for input detection. By default the keyboard does not come with any Keys attached, therefore they need to be added. To see more examples on how to use the input component in kiwi.js take a look at our example repository on Github here.

Adding Keys

To be able to check specific key input we need to create new key objects. To create a Key object we simply add a new key to the game’s Keyboard using the addKey function. The function also returns a Key object which we can map to a variable for use later on.


Keycodes are variables representing numbers which correspond to different keys on the keyboard according to this table. However, since it can be confusing and difficult to remember all those number combinations we have custom Kiwi.js static variables for each and every keycode. You can find a handy list for all of these here.

Checking Key Input

Most of the time we are going to want to check if keys are being pressed down. To check this we get the keycode variable from earlier and use the isDown property to check if it is currently being pressed.

You can also check whether a specific key is not being pressed using the isUp property.

Just Pressed and Just Released

Each key also has a justPressed and justReleased function. This is an alternate method to checking input. The keyboard object has two properties called justPressedRate and justReleasedRate which are both set to a default of 200ms.

When a key’s justPressed function is called it checks if the key has been changed from isUp to isDown in the last 200ms (or whatever the current justPressedRate is). Another way to look at this is that when you press a key this function will return true for 200ms.

The justReleased function works exactly the same, however, it checks if the key has been released in the time frame rather than pressed, as the name would suggest.

Checking Shift + Key, Control+Key and Alt + Key

Sometimes you may want to use the Shift, Control and Alt keys to add new variations to your key input. Each key has an altKey, shiftKey and ctrlKey boolean. This is set to true if the alt/shift/control key was currently down at the time this key was pressed.


Share the joy
Comments 5
  1. MarsBars

    What if I want to detect that both A and D are down? When trying the below in the update function, it does not detect both keys:

    if(this.A.isDown && this.D.isDown){

  2. Hey MarsBars,

    I just tested what you were trying to do and it does work. So you might be having problems somewhere else. Have you made sure you have added the key to the state (e.g. this.A =;) in the create method. If you have not do this the game will not know what you mean by “this.A”.

  3. TJ Renninger

    if (

    When I hit space, it infinitely prints Interacting. How do I fix this?

  4. Hey TJ,

    The best way to do that currently is by attaching a callback to the ‘onKeyDown’ property on the ‘Keyboard’ class.

    We do have plans to support functionality like you have proposed above in the future!

    Hope it helps.

  5. TJ Renninger

    Thank you , that solved my problem. It is kind of interesting that the way I originally was doing it, worked for up, right, down, and left.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">