Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make InputEventMouseButton.as_text() work similar to InputEventKey.as_text() #2221

Open
me2beats opened this issue Feb 2, 2021 · 3 comments

Comments

@me2beats
Copy link

me2beats commented Feb 2, 2021

Describe the project you are working on

Editor plugin with customizable input event map.

Describe the problem or limitation you are having in your project

I need to get recent mouse "shortcut" as text (like Control+BUTTON_LEFT, Control+Shift+BUTTON_RIGHT).

I've found InputEventKey.as_text() that works as I need (that is it returns Control+D, Shift+R etc) and I expected this to work for InputEventMouseButton the same way, but it doesn't.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Making InputEventMouseButton.as_text() work similar to InputEventKey would solve the problem

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

returning modifiers+button_index like Control+BUTTON_LEFT, Control+Shift+BUTTON_RIGHT would be fine.

If this enhancement will not be used often, can it be worked around with a few lines of script?

As a workaround I think a function could be created like

func as_text(InputEventMouseButton)->String:
	...

I see 2 tasks here:

  1. getting mouse button as text (BUTTON_LEFTT, BUTTON_RIGHT etc).
    This could be done like:
func get_mouse_button_as_text(event:InputEventMouseButton)->String:
	return event.as_text().split("button_index=")[1].split(',')[0]

and
2. getting modifiers as text, something like:

func get_modifiers_as_text(event:InputEventMouseButton)->String:
	var text = ""
	
	if event.control:
		text+="Control+"
	if event.command:
		text+="Command+"
	if event.meta:
		text+="Meta+"
	if event.shift:
		text+="Shift+"
	if event.alt:
		text+="Alt+"
	
	return text.trim_suffix("+")

but I just ran into some challenges like:

  • when I press Control key, event.command is also true (Linux);
    not sure how to correctly solve this
  • not sure that I won't have other surprises related to the peculiarities of different OS/keyboards

Is there a reason why this should be core and not an add-on in the asset library?

I think this could be useful for implementing any customizable input event system that allows using "mouse shortcuts" with modifiers

@EricEzaM
Copy link

EricEzaM commented Feb 2, 2021

Hey!

Good news, I think this is already done in 4.0. Check out my PR: godotengine/godot#43660 and godotengine/godot#44356

You can now use as_text() on any InputEvent to get a "Human Readable" string, suitable for UI display. Or, you can use to_string() for debug strings which show more structured debug-style information.

This likely won't be backported to 3.2 (not sure though, certainly no plans to at this stage) but you could copy my C++ code in that PR to your own utility class in GD script for the same effect.

@EricEzaM
Copy link

EricEzaM commented Feb 3, 2021

@me2beats Does that satisfy this proposal in your opinion?

@me2beats
Copy link
Author

me2beats commented Feb 3, 2021

@EricEzaM do I get it right that the PR does not use event.command because find_keycode_name() should deal with it?

But it seems I can't use this method from gdscript (it isn't exposed), so do I need to create my own one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants