Godot Key Bindings Tutorial

Most games let you assign custom keys to each action. For example, one person likes to use the ‘c’ button to crouch, and the other wants to use ‘ctrl’. Of course, you can make your own custom key bindings interface in Godot. And in the Godot Key Bindings Tutorial, we will go through it.

Input Map in Godot

First, if you haven’t already, you should take a look at the Input Map. It allows you to assign one or more keys to an action. Even if you don’t plan to make custom key bindings, it is good practice to use the Input Map, since it lets you easily add, remove or change an associated action to any input from the keyboard, the mouse, a joystick, etc.

You can find the Input Map under Project > Project Settings > Input Map. Type in an action name and click add. Then on the plus sign, you can assign an input to the created action.

Starting the Godot Key Bindings Tutorial

For our Godot Key Bindings Tutorial, we create a new Szene in our Godot Engine and create a Control node in it. Then we also add a Panel, a ScrollContainer and a VboxContainer to it. We also add a Script to our Control node. So, your tree should look something like this so far.

Next, we add a HboxContainer for each action, that we want to use. In our example, we will use three actions: JUMP, CROUCH and KISS. So we add the first HboxContainer, and add a Label and a Button to it. Also, let us make the button toggle-able.

Then we duplicate it two times. Voila. We also name them to HBoxCont_JUMP, HBoxCont_CROUCH and HBoxCont_KISS. You can play around with the different alignments and sizes so that you get a nice little panel with labels and buttons in it.

Next, let us add those actions to our Input Map. Go to Project > Project Settings > Input Map and add JUMP, CROUCH and KISS as actions. Then click the Plus Sign and add a Key. Use whatever keyboard key you like.

You should align the different nodes in your Control node in a way so that you can see everything. Also, add the text Jump, Crouch and Kiss to the Labels inside the HBoxContainers.

At last some code!

Now we open the Script attached to the Control node and add the following lines:

The first line we use to determine if we currently want to change the button or not. Action_string will save the name of the action we want to change (whenever we want to change it) and the enum ACTIONS has all the actions in it, that we want to be customizable. Since we have toggle-able buttons let us also add the following:

This way we make sure, that no button appears pressed and that every action has a button assigned to it. Otherwise, it will tell you that there is no button assigned. Be aware, that in order to make this tutorial work, you have to use the exact same names and spelling for the enums, the HBoxCont_* and the actions defined in the Input Map. Run the scene and you should see something like this:

Next, we add a signal to each button, to trigger an event. If you want to know more about signals, check here. We have three buttons so we add three pressed() signals and also this code:

Whenever a button is pressed, we call _mark_button() and send them the name of the action as a string. Our _mark_button method looks like this:

Changing the keys

So we want, that we can change the key and assign the string of the key we want to change to our action_string. Then we set all other buttons to false, in case we have selected one before. Next, we add an input event, to get the new input.

We only want to call _change_key if can_change_key is true. And after we called _change_key we set can_changekey to false again. Our _change_key looks as follows.

First, we check, if there is already a key assigned. In this case, it deletes the key. Then we check, if this key is assigned somewhere else, and if yes, we delete this key as well. And last we add a new key, which consists of the event from our input, and add it to the action, which still is our action_string. If you now run the scene, you should be able to click on a button, and then on a key. The key should then be assigned to the button.

Add a second scene to Godot Custom Key Bindings Tutorial

So far, so good. Now we have to check if it actually works. Let us create a second scene for this. In it, we just create an empty node and add a script to it. In the script, we write the following:

We also add a button to the scene, create a signal and in it, we navigate back to our scene 1.

Then we go back to scene 1, add a button to the bottom, create a signal and in it, we navigate to scene 2.

And we are ready for testing. You can assign different keys to the actions in scene 1, then go to scene 2 and test them. Everytime you press or click the assigned key, it should print the correct key to the console.

If you find this useful, let me know in the comments below. Also feel free to check out other Godot tutorials and topics.

Originally published at www.gotut.net.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Free Time Dev

Free Time Dev

In my spare time I work on Timeless Adventure — a fantasy adventure game