- Edited
I have a keyboard and 2 xbox controllers connected to PC
the _Ready
function returns 2 controllers correctly:
public override void _Ready()
{
var joypads = Input.GetConnectedJoypads();
foreach (var item in joypads)
{
GD.Print("connected joypad: " + item);
}
}
Outputs:
connected joypad: 0
connected joypad: 1
I then try to handle the inputs from all 3 devices like so in _Input
method:
public override void _Input(InputEvent input)
{
if (input is InputEventKey && input.IsPressed()) // keyboard
{
var keyboardInput = input as InputEventKey;
GD.Print("keyboard pressed " + keyboardInput.Device + " " + keyboardInput.AsTextKeycode() + " " + keyboardInput.AsText() + " " + keyboardInput.AsTextKeyLabel());
}
else if (input is InputEventJoypadMotion && input.IsPressed()) // controller
{
var controllerInput = input as InputEventJoypadMotion;
var joyName = Input.GetJoyName(controllerInput.Device);
GD.Print("(1) controller pressed " + controllerInput.Device + " " + controllerInput.AsText() + " " + joyName);
}
else if (input is InputEventJoypadButton && input.IsPressed()) // also controller
{
var controllerInput = input as InputEventJoypadButton;
var joyName = Input.GetJoyName(controllerInput.Device);
GD.Print("(2) controller pressed " + controllerInput.Device + " " + controllerInput.AsText() + " " + joyName);
}
}
When keyboard button is pressed I get the correct 1-line log:
keyboard pressed 0 Right Right Right
However whether the first controller
(2) controller pressed 0 Joypad Button 0 (Bottom Action, Sony Cross, Xbox A, Nintendo B) XInput Gamepad
keyboard pressed 0 Enter Enter Enter
or the second controller buttons are pressed
(2) controller pressed 1 Joypad Button 1 (Right Action, Sony Circle, Xbox B, Nintendo A) XInput Gamepad
keyboard pressed 0 Space Space Space
I always see the event for both the controller AND the keyboard soon after! Why this this happening? I would like to see a single event when input happens, but currently whether controller button is pressed I get an additional event that belongs to a keyboard.