Problem is not reproducible or outdated
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 this 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 behind 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 think this might be beyond my limited experience. Any help would be greatly appreciated.
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.
Thanks for looking that over, i was going a little mad.
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.
Follow this Question
Related Questions
[SOLVED] get object which is nearby to mouse 1 Answer
Move only if mouse click the Terrain 0 Answers
Input.GetMouseButtonDown() not working 0 Answers
How to prevent mouse click from going through the GUI.Box 0 Answers
Mouse Cursor is flashing 0 Answers