Any methods to identify the input event is Keyboard/Mouse or Joypad in c++?

Godot Version

4.2.1

Question

I am trying to get the related device (Keyboard/Mouse or Joypad) of InputEvent in _input method in c++ code. I found device_id method but it always returns 0.
Any way to identify the input source?

You identify it by the type of the event. InputEventMouse for mouse, InputEventKey for keyboard and InputEventJoypadMotion/InputEventJoypadButton for joypad.

I’m not 100% sure if this works but it’s something like this:

void MyClass::check_event_type(const Ref<InputEvent> p_event) {
	Ref<InputEventMouse> mouse_event = p_event;
	if(mouse_event.is_valid()) {
		// It's a mouse event
	}
	Ref<InputEventKey> keyboard_event = p_event;
	if(keyboard_event.is_valid()) {
		// It's a keyboard event
	}
	// ...
}

The device_id is to distinguish between devices of the same type like events from multiple joypads.

It works, Thanks!

Please help me, see my post about camera region

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.