Disable mouse input UI

Hi, I’m working on a textbox / dialogue system with the new Unity UI. My game does not make use of the mouse, so I am trying to control the UI exclusively through the keyboard. I have a basic Event System with a Standalone Input Module, with the Submit Button set to a keyboard key (“S”). The UI seems to automatically accept mouse hover and clicks, however, which isn’t ideal in this case. More importantly, while you can use the Up and Down arrows to navigate between dialogue choices and use the Submit Button to press those buttons, the same as if you had clicked them, if you happen to click outside of the UI, the buttons lose focus and you can no longer use the keyboard until you click a button with the mouse.

I have tried locking/hiding the cursor, but the UI still seems to register click events and causes the UI to lose focus, rendering the keyboard controls useless. I suppose I could write a script to return focus to the previously focused button whenever a mouse click is detected, but it seems like a bit of a hack. Is there no way to decouple the mouse from the UI Submit or to disable mouse input entirely (while still retaining the submit functionality with the keyboard)?

I have also tried sending the submitHandler event with a key press instead of using the Standalone Input Module Submit Button, but the behaviour differs very slightly and so is not ideal. I’m referring to this: ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents.submitHandler);

Disable the “Graphic Raycaster” component of Canvas gameobject.

Put a Canvas Group on the base Canvas via the Add Component button.

Switch OFF (un-tick) Blocks Raycast and make sure that navigation on your UI elements is setup correctly (it should be automatic unless you want specific navigation).

On the Event System drag in the first selected element (e.g. the first Button) and you should be good to go.

EDIT - well that’s annoying - this loses focus as well. That script @KuR5 pointed to is pretty easy to get working though.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class ISaidNoMouse : MonoBehaviour {

	private GameObject selectedObj;

	void Start()
	{selectedObj = EventSystem.current.currentSelectedGameObject;}

	void Update()
	{
		if (EventSystem.current.currentSelectedGameObject == null)
			EventSystem.current.SetSelectedGameObject(selectedObj);
	
		selectedObj = EventSystem.current.currentSelectedGameObject;
	}
}

You need to disable the GraphicRaycaster component AND also uncheck “Deselect On Background Click” in the InputSystemUIInputModule. This will retain the keyboard focus.