The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+6 votes

Something like JoyToKey, that translates the press of a button on the controller to the press of a button on the keyboard. Is it possible?

in Engine by (1,118 points)

You want to send the event back to the OS?

Yes, that's it.

I don't think that's possible (not out of the box). Also, if you want to make a JoyToKey clone, Godot might be an overkill.

Not a JoyToKey clone, just to give the idea of what I want.

1 Answer

+4 votes

You can create and dispatch an InputEvent for cases like this. Here's the script for a function I use to simulate a click event at a given position, for example:

func fake_click(position, flags=0):
var ev = InputEvent()
ev.type = InputEvent.MOUSE_BUTTON
ev.pressed = true
ev.global_pos = position
ev.meta = flags

For a keypress, you might have something like this:

func simulate_key(which_key):
  var ev = InputEvent()
  ev.type = InputEvent.KEY
  ev.scancode = which_key

Note that this example would expect a scancode like KEY_F, not a simple string like F; those are all pretty much what you'd expect, but you can check the class reference for @GlobalScope if you need to look one up.

You should also take a look at the documentation and the demo for using the InputMap - which might be more what you're looking for, depending on your use case. It provides a way to bind multiple types of input to one 'action' code, which can in turn be hooked up to whatever functions you want it to call.

by (338 points)

I want to send key events to the OS. Thanks for your time though.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.