Can I change what triggers Button.pressed()?

Godot Version

3.5.3

Question

I am using Godot 3.5.3, and I can’t find a way to change what triggers Button.pressed() signal (also, it should change the theme while it is being pressed).

By default, the left-click mouse button, enter and space keys trigger the pressed signal. I would like to have an Input Map entry, let’s call it custom_pressed, where I can define what should trigger Button.pressed(). Suppose I wanted to use the left click and the enter key, but not the space key. And I might also want some other keys to be able to press the button. All of that would be in custom_pressed. Is there a way to do this, or are those key bindings (left click, enter and space) hard-coded?

I know I could use a separate signal (other than pressed), which would do this, but that would mean rewriting a lot of Button’s functionality, and that would be in GDScript, not in C++, which would make it slower (but probably not noticeable).

To define which mouse buttons will work, each Button has a button_mask property, which you can either set in the inspector while the node is selected or from code.

If the Button is focused (e.g. by clicking on it or calling its grab_focus method), all input events mapped to the ui_accept input action will also trigger it. This is why, by default, both Enter and Space will work. Just add further input events or remove the unwanted ones in the ProjectSettings.

1 Like

Thank you very much!

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