Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
5
Question by braytendo · May 09, 2016 at 09:57 PM · inputmousedisablemouseclick

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dalkeri · May 16, 2016 at 02:07 PM 0
Share

Same problem here, I'm sure there is a simple and easy way but can't find it

avatar image Jessespike Dalkeri · May 16, 2016 at 08:40 PM 0
Share

I've converted your "answer" to a comment. Please do not add comments as answers.

avatar image vittu1994 · May 16, 2016 at 04:11 PM 0
Share

So you want to have UI that you cant interact with using any mouse input?

avatar image braytendo vittu1994 · May 16, 2016 at 07:44 PM 0
Share

$$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.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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

 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Jessespike · May 16, 2016 at 08:47 PM

Did you try Script Reference: Cursor?

     Cursor.visible = false;
     Cursor.lockState = CursorLockMode.Locked;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image braytendo · May 16, 2016 at 08:50 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges