Intercept Mouse Input and ReIssue as Key / Action...

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zappo_II

Hi…

…have built a game that is controlled by ui_actions and key_press events…

I plan to export for mobiles, thus needing to implement something else to control the game…

Thought it would be a good idea to start off using mouse events which i intercept using _input() in a new node instance I add to the existing game…

Trouble is, it’s somehow working, but…(and I can’t figure out what’s going wrong)…it seems to me the Key/Action Events I generate are persisting forever…

The Debug Output the code generates states everything fine, but only the first Move Event (ether ui_left/ui_right) get’s triggered and never stops…same with ui_down, that action also never stops if triggered once…

As you can see from the comments I tried setting as action as well as key presses, no difference…

I also tried to consume the given event or trigger press / release…

When Triggering Press/Release in sequence, only debug output happens, no effect to the game happens…

As said before, no idea what’s going wrong or what else to try…

Any suggestions…???

Thanx and Cheers…

This is the code to Intercept Mouse and re-Issue Key/Action Events:

extends Node

func _ready():
    pass

func _input(event):
    var button = ""
    var sector = ""
    var action = null
    var screenWidth = get_viewport().get_visible_rect().size.x
    var screenHeight = get_viewport().get_visible_rect().size.y

if event is InputEventMouseButton:
	button = "-other-"

	if event.button_index == BUTTON_LEFT:
		button = "LEFT"
	if event.button_index == BUTTON_RIGHT:
		button = "RIGHT"
	
	#print("DEBUG: MouseButton - " + str(event.position.x) + "," + str(event.position.y) + " - " + button + "(" + str(event.button_index) + ")" + (" DOWN " if event.pressed else " UP "))
	if button == "LEFT" and not event.pressed:

		if event.position.x < screenWidth / 3:
			sector = "LEFT"
			action = "ui_left"
			#action = KEY_LEFT
		elif event.position.x > (screenWidth / 3) * 2:
			sector = "RIGHT"
			action = "ui_right"
			#action = KEY_RIGHT
		else:
			sector = "CENTER"
		
		if event.position.y < screenHeight / 3:
			sector = "UPPER" + sector
			action = "ui_up"
			#action = KEY_UP
		elif event.position.y > (screenHeight / 3) * 2:
			sector = "LOWER" + sector
			action = "ui_down"
			#action = KEY_DOWN
		else:
			sector = "MIDDLE" + sector
		if sector == "MIDDLECENTER":
			action = "ui_accept"
			#action = KEY_ENTER

		print("DEBUG: Left-Button-Clicked at " + sector)

		#get_tree().set_input_as_handled()
		if action != null:
			print("DEBUG: parse_input_event(" + str(action) + ")")
			var ev = InputEventAction.new()
			ev.set_action(action)
			#var ev = InputEventKey.new()
			#ev.scancode = action
			ev.set_pressed(true)
			Input.parse_input_event(ev)
			#ev.pressed = false
			#Input.parse_input_event(ev)
		
elif event is InputEventMouseMotion:
	#print("DEBUG: MouseMotion - " + str(event.position.x) + "," + str(event.position.y))
	pass
else:
	print("DEBUG: ??? - " + str(event))
pass

    
:bust_in_silhouette: Reply From: SIsilicon

There is a Node2D that does what you need. It’s called a TouchScreenButton, and when it is tapped, it can trigger a specified action.
No need for the over complicated script. Just one node (or several) to solve this issue.

That really does the trick, thanks mate…

Still wondering how one is supposed to generate KeyPress Events from code anyway…

Zappo_II | 2018-12-15 15:50