How can I disable mouse input completely?
This feels like it should be really simple, but all suggestions I've come across have mentioned rewriting the input module or changing something in the event system. I just want a simple script or option to disable mouse input.
Basically: a mouse click won't steal focus from a GUI button, the cursor is hidden, a mouse click doesn't do anything at all. I just want to disable the mouse so the player needs to use a keyboard or controller. This has been driving me crazy, so any help is appreciated.
Same problem here, I'm sure there is a simple and easy way but can't find it
I've converted your "answer" to a comment. Please do not add comments as answers.
So you want to have UI that you cant interact with using any mouse input?
$$anonymous$$y game is made specifically to be played with a keyboard or controller. There is no use for a mouse and it causes issue with s$$anonymous$$ling focus from GUI elements so I just want to disable mouse input completely so the player needs to use their keyboard or controller. There are many games on S$$anonymous$$m with this exact functionality.
Answer by astracat111 · Feb 08, 2019 at 12:49 AM
Okay, so anyone who looks this up, here is the way to disable your mouse. This script only disables left click because it's a semi-dangerous thing to do. This disables it just for your games application window:
Firstly, make sure you understand that you can stop your application in the editor with Ctrl + P. That's kind of important if you're disable mouse buttons.
If you want to specify which buttons to disable, you edit this part here:
if (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
return (IntPtr)1;
}
When that part returns 1; It's stopping the mouse event message from getting to your applications window, and thusly the application doesn't process a mouse click.
public class Mouse
{
#if UNITY_STANDALONE_WIN
public WindowsLowLevelHook windowsLowLevelHook = new WindowsLowLevelHook();
public void DisableClicks()
{
windowsLowLevelHook.Enable();
}
public void EnableClicks()
{
windowsLowLevelHook.Disable();
}
public class WindowsLowLevelHook
{
public bool enabled = false;
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
public HookProc hookProc;
public delegate void MouseHookCallback(MOUSEHOOKSTRUCT mouseHookStruct);
#region Events
public event MouseHookCallback LeftButtonDown;
public event MouseHookCallback LeftButtonUp;
public event MouseHookCallback RightButtonDown;
public event MouseHookCallback RightButtonUp;
public event MouseHookCallback MouseMove;
public event MouseHookCallback MouseWheel;
public event MouseHookCallback DoubleClick;
public event MouseHookCallback MiddleButtonDown;
public event MouseHookCallback MiddleButtonUp;
#endregion
public IntPtr hookID = IntPtr.Zero;
public void Enable()
{
hookProc = MouseProc;
hookID = SetHook(hookProc);
enabled = true;
}
public void Disable()
{
if (hookID == IntPtr.Zero)
return;
UnhookWindowsHookEx(hookID);
hookID = IntPtr.Zero;
enabled = false;
}
~WindowsLowLevelHook()
{
Enable();
}
private IntPtr SetHook(HookProc hookProc)
{
using (ProcessModule module = Process.GetCurrentProcess().MainModule)
return SetWindowsHookEx(WH_MOUSE, hookProc, GetModuleHandle(module.ModuleName), GetCurrentThreadId());
}
private IntPtr MouseProc(int nCode, IntPtr wParam, IntPtr lParam)
{
MOUSEHOOKSTRUCT lParamStruct = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));
if (nCode >= 0)
{
if (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
return (IntPtr)1;
}
}
return CallNextHookEx(hookID, nCode, wParam, lParam);
}
#region WinAPI
private const int WH_MOUSE = 7;
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205,
WM_LBUTTONDBLCLK = 0x0203,
WM_MBUTTONDOWN = 0x0207,
WM_MBUTTONUP = 0x0208
}
private Int32 GetWindowThreadProcessID(IntPtr hwnd)
{
Int32 pid = 1;
GetWindowThreadProcessId(hwnd, out pid);
return pid;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEHOOKSTRUCT
{
public POINT pt;
public IntPtr hwnd;
public uint wHitTestCode;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, Int32 dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32.dll")]
private static extern Int32 GetCurrentThreadId();
#endregion
}
#endif
}
Answer by Jessespike · May 16, 2016 at 08:47 PM
Did you try Script Reference: Cursor?
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
I've tried that, and that doesn't remove mouse functionality. It just locks the cursor to the center of the screen and hides it. The player can still click and that click will remove the highlighted state from a GUI button.
Answer by vittu1994 · May 16, 2016 at 09:26 PM
Well it seems to fix this you need to get inside the button class (or whatever UI you dont want to access with mouse) and go through all OnMouse functions.
The ones that exist in the button class is:
OnMouseDown()
OnMouseDrag()
OnMouseEnter()
OnMouseExit()
OnMouseOver()
OnMouseUp()
OnMouseUpAsButton()
You will need to access all instances of this class in your ingame and make sure these functions arent called cause they seem to control mouse input when it comes to UI.
EDIT: My best bet is to create a second button class. It is identical but you can edit it. Remove all functions that involves mouse input in the new one. Assign it to your buttons and remove the old one. I dont know where you can get the button script, just google it i guess
Your answer
Follow this Question
Related Questions
Left Mouse doesn't register click when keyboard button is pressed 1 Answer
Detect mouse inputs when the scene is changing to another scene. 2 Answers
How to control global mouse 0 Answers
Sprite/gameObject touch or click event 0 Answers
Objects not Detecting Mouse Clicks Reliably (OnMouseDown()) 1 Answer