- Home /
Simulate a mouse Click?
I am have been making a game solely for pc, i have now come to the point id like to implement joystick, I've done so successfully with every key binding, however my game comprises of mouse clicks, OnMouseUp to be precise, and I am stumped how to accomplish this, i merely want to have for example:
if (input.GetButtonDown("A") {
//simulate mouse click in general
}
Which would then effect my OnMouseUp scripts through out the game as if the mouse it self was clicked.
This i would have thought was simple as my cursor is locked to the centre of the screen, my mouse enters and exit codes work fine and prove this, its just getting the mouse to click i cant do, please help its like the last thing on the to do list :P
On$$anonymous$$ouseUp(); Is just a function. I would think you can just call it as such.
if (input.GetButtonDown("A") {
//simulate mouse click in general
On$$anonymous$$ouseUp();
}
maybe its as simple as that... I know On$$anonymous$$ouseUp() is an event that happens when a mouse button is clicked but it may be as simple as forcing the state by calling the function?
where getRun is pressing the Run button:- This is what i put in a script in JS..
function FixedUpdate(){
if(getRun){
On$$anonymous$$ouseUp();
}
}
function On$$anonymous$$ouseUp(){
Debug.Log("mouse");
}
And sure enough when i pressed the Run button Debug.Log printed "mouse"
i was a tad vague im afraid to say by joystick i meant I'm using an xbox controller no a touch screen function, I'm also wanting a way that will all me to do so with out having to reedit all my previous codes, the "getRun" function would work, however this means i will have to edit all scripts with the mouse function in it, I'm also quesrioing whether this would mean i could call the mouse function wherever in the scene and not have to be entered with the mouse would it not?
I tested the get run idea and as I said it does click the mouse but it does not require the mouse to be on the object, I can click anywhere in my scene and the function happens
Answer by vexe · Mar 02, 2014 at 05:07 PM
This already have been answered How can I control the mouse (Move and send mouse click signals) via the keyboard (Specifically for PC - For sending mouse event via buttons, see 2nd edit)
Answer by ChrissTman · Aug 28, 2021 at 01:23 PM
This is an old question, but it was quite hard to find this as a solution. So hope this will help someone. Also, with the new Input System, it might be even easier.
To put it simply. You can inherit your own class from StandaloneInputModule
. And there you are able to call ProcessTouchPress
which will resolve into a clicking action. In general i would recommend looking through the source code of the Unity UI system which contains the EventSystem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class TestInputModule : StandaloneInputModule
{
public void ClickAt(float x, float y)
{
Input.simulateMouseWithTouches = true;
var pointerData = GetTouchPointerEventData(new Touch()
{
position = new Vector2(x, y),
}, out bool b, out bool bb);
ProcessTouchPress(pointerData, true, true);
}
void Update()
{
if(Input.GetKeyDown(KeyCode.F1))
{
ClickAt(Screen.width / 2, Screen.height / 2);
}
}
}
Hey, it is way more complicated to simulate drag or scroll. If you want to achieve that, you should dive into the code and try to understand it and replace some functionality.
But, i think that's just for the old input system. The new one has a nice API. https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Testing.html
So perhaps, revaluate if you don't want to move to the new Input System.
No, new InputSystem available from 2020 version. But thank you anyway)
Your answer
Follow this Question
Related Questions
Check if mouse is down while in Edit Mode 1 Answer
Find out if any Button on any Gamepad has been pressed and which one 6 Answers
Implementing a virtual joystick with mouse 1 Answer
OnMouseDown() Doesn't work 17 Answers
How to disable button OnClick when drag on the screen with OnMouseDrag 2 Answers