• 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 /
This question was closed Mar 23, 2020 at 06:04 PM by ImNewAtThis for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by ImNewAtThis · Mar 18, 2016 at 03:24 AM · c#mouseclickmouse position

Could someone help me figure out which part of this code relates to mouse button click simulation please?

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.InteropServices;
 
 public class MouseClickSimulation : MonoBehaviour
 {
     [Flags]
     public enum MouseEventFlags
     {
         LeftDown = 0x00000002,
         LeftUp = 0x00000004,
         MiddleDown = 0x00000020,
         MiddleUp = 0x00000040,
         Move = 0x00000001,
         Absolute = 0x00008000,
         RightDown = 0x00000008,
         RightUp = 0x00000010
     }
 
     [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
     [return: MarshalAs(UnmanagedType.Bool)]
     private static extern bool SetCursorPos(int X, int Y);
 
     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     private static extern bool GetCursorPos(out MousePoint lpMousePoint);
 
     [DllImport("user32.dll")]
     private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
 
     public static void SetCursorPosition(int X, int Y)
     {
         SetCursorPos(X, Y);
     }
 
     public static void SetCursorPosition(MousePoint point)
     {
         SetCursorPos(point.X, point.Y);
     }
 
     public static MousePoint GetCursorPosition()
     {
         MousePoint currentMousePoint;
         var gotPoint = GetCursorPos(out currentMousePoint);
         if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
         return currentMousePoint;
     }
 
     public static void MouseEvent(MouseEventFlags value)
     {
         MousePoint position = GetCursorPosition();
 
         mouse_event
             ((int)value,
              position.X,
              position.Y,
              0,
              0)
             ;
     }
 
     [StructLayout(LayoutKind.Sequential)]
     public struct MousePoint
     {
         public int X;
         public int Y;
 
         public MousePoint(int x, int y)
         {
             X = x;
             Y = y;
         }
 
     }
 
 
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Joystick1Button0))
           MouseEvent(MouseEventFlags.LeftUp | MouseEventFlags.LeftDown);
     }
 
 }
 

I've used t$$anonymous$$s code to simulate the left mouse click function when i press the "A" button on an xbox controller. It also functions to control mouse movement with directional key input rather than mouse input. I'm hoping to weed out the sections of code that relate to mouse movement and leave be$$anonymous$$nd the functional left click simulation. I've attempted to do that several times but so many of the functions call previously stated functions that whenever i try to remove parts i just end up with errors on top of errors. I t$$anonymous$$nk t$$anonymous$$s might be beyond my limited experience. Any help would be greatly appreciated.

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1

Answer by LazyElephant · Mar 18, 2016 at 04:51 AM

It doesn't look like that script is the cause of the mouse movement. It only checks to see if the joystick button is pressed and fires a mouse click event. The reason the mouse is moving with the joystick is most likely just a result of how the inputs are configured in Unity.

If you go to Edit->Project Settings->Input and expand Axes->Mouse X and Mouse Y, you'll probably see that under Joy Num it says Get Motion from all Joysticks. There isn't an option to turn off joystick input, so I would recommend changing it to different joystick numbers to see if that will stop it from moving the mouse.

Comment
Add comment · Show 2 · 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 ImNewAtThis · Mar 21, 2016 at 02:54 PM 0
Share

Thanks for looking that over, i was going a little mad.

avatar image ScottKane ImNewAtThis · May 15, 2020 at 02:55 PM 0
Share

In future don't delete the post, just because your issue is solved doesn't mean that someone else couldn't benefit if they are in the same position as you.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change the color of the each phase of the cube separately ? 0 Answers

How to move a game object to a position after selecting it 0 Answers

Match 3 Movement Using Keyboard Controls,Match 3 movement using keyboard controls 1 Answer

[SOLVED] get object which is nearby to mouse 1 Answer

Move only if mouse click the Terrain 0 Answers


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