Make Standalone Input Module ignore mouse input?
I have a system of buttons in a UI canvas, which are linked to an EventSystem using Standalone Input Module. By default, this module responds to Mouse and Keyboard Input. Mouse input can cause glitches with my UI and is unnecessary, so I would rather disable it. However the standalone Input Module seems to only have options for Keyboard only, Controller, and both Keyboard and Mouse; yet not Keyboard only.
How can I make the module ignore all mouse events? (Mouse hover, Mouse clicks, etc.)
Answer by Vodolazz · Sep 24, 2016 at 11:03 AM
I had same problem which I've just solved. Unity provides their 2 inputModules as an out-of-the-box decision for most common tasks. (https://docs.unity3d.com/Manual/InputModules.html)
Basically, you can inherit your own type from BaseInputModule and write new input module realization specially for your needs. As far as I know, Unity suggests this method by itself, but you will need rather good skill and time to do it. There is an easier way.
Just inherit your class from StandaloneInputModule and override Process() method.
Here is the code ( If you are confused, just create new C# script, copypaste the code and use this script instead of input module):
class OnlyKeyBoardInputModule : StandaloneInputModule
{
public override void Process()
{
bool usedEvent = SendUpdateEventToSelectedObject();
if (eventSystem.sendNavigationEvents)
{
if (!usedEvent)
usedEvent |= SendMoveEventToSelectedObject();
if (!usedEvent)
SendSubmitEventToSelectedObject();
}
}
}
If you want to turn on and off mouse events, you can do the following:
class OnlyKeyBoardInputModule : StandaloneInputModule
{
//variable to hold current settings
private bool isMouseInputActive = false;
//type interface to get actual mouse input status
public bool GetMouseState
{
get { return isMouseInputActive; }
}
//mouse switcher interface
public void MouseSwitcher()
{
isMouseInputActive = isMouseInputActive == false ? true : false;
}
//inherited event processing interface (called every frame)
public override void Process()
{
bool usedEvent = SendUpdateEventToSelectedObject();
if (eventSystem.sendNavigationEvents)
{
if (!usedEvent)
usedEvent |= SendMoveEventToSelectedObject();
if (!usedEvent)
SendSubmitEventToSelectedObject();
}
if (isMouseInputActive)
ProcessMouseEvent();
}
}
Great answer! Perfectly solved my problems after days of searching around. And it's easy to implement as well! Just made my day
Only one thing: Be sure to include the line using UnityEngine.EventSystems;
at the top of the script.
Answer by idbrii · May 03, 2018 at 04:25 PM
The simple answer is ensure that your StandaloneInputModule doesn't call ProcessMouseEvent. Whether that's modifying your existing InputModule or implementing a new one (for both, see Vodolazz's answer).
However, another note: if anything is using GraphicRaycaster, it's probably building its own PointerEventData and ignoring your InputModule as demonstrated in the docs. You should either convert it to IPointerEnter/ExitHandler, skip update when using gamepad, or make it pass in PointerEventData.position = Vector2.zero
(bottom-left corner of the screen) so it doesn't hover anything when using gamepad.