Checking multiple key pressing (v 3.2.3)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lsgrandchamp

Hi,
I’m working on a prototype for a 2D sidescroller game and I’m having some problems making the player shoot and run at the same time.
The overview is:

  1. While the player is holding the “Shift” key, the player starts to run. As soon the player releases the “Shift” key, it starts to walk instead. This part is working as intended.
  2. If the player presses the “KEY_KP_2” (Numberpad 2), it shoots a projectile. This is working as intended in every state but the “Running” state, where the player is also holding the “Shift” key.
    I’ve tried the solution described in Godot Documentation, but to no avail.

The code is written as follows:

	if event is InputEventKey and event.pressed:
	    print("pressed: " + String(event.scancode))
        if event.scancode == KEY_KP_2:
		    print("KP_2 pressed")
		    if event.shift:
			    print("Shift+KP_2 pressed!")
			    return true

The outputs:

  1. When I press the KEY_KP2 key, it prints “16777352”;
  2. When I press the Shift key, it prints “16777237”;
  3. When I press the Shift and KEY_KP2 at the same time, it prints “16777234”

As shown, I’m having a hard time to detect the “Shift+NP2” press. Is there a specific scancode for “Shift+KP_2”?

Regards,
Lucas Sene

:bust_in_silhouette: Reply From: magicalogic

Replace the last if block with this:

if Input.is_key_pressed("shift"):
         print("Shift+KP_2 pressed!")
                return true

Of course you have to add action “shift” in the project settings.

Thanks magicalogic, but the method is_key_pressed asks for a KeyList param, and does not accept a string. Nevertheless, I think that my problem may be a bug.

lsgrandchamp | 2020-12-01 13:19

Sorry for the misleading answer but i meant Input.is_action_pressed(“shift”)
having added action “shift” as action.

magicalogic | 2020-12-01 18:02

:bust_in_silhouette: Reply From: lsgrandchamp

I think that this problem may be a bug:

In the InputMap tab inside the Project Setting, when I try to create an action with Shift + any key, it works with any key but the number on the numeric keypad. When I try, for example, Shift + KP 2, the popout window shows the “Down” key as result. This happens with any of the number keys on the numeric keypad.

Assigning Shift + any key to the action. Working as normal.

Assigning Shift + any number in the numeric keypad. It shows “Down” instead of “Shift+KP2”. The engine cannot detect this combination of keys.