1

I'm trying to make cursor with Unity, which moves with keyboard input. It will move with WSAD keys, and send touch event with Q Key. So What I want to do is something like:

if (Input.GetKeyDown(KeyCode.Q)
{
    // Is identical to touch/click the given position of screen for this frame.
    SendTouchEvent(currentCursorPos);
}

Detecting touch is pretty easy, but how do I make touch events artificially?

Copy-pasting my already existing input handler (Using raycast on touch position, for example) would be also a solution, but I think there would be clearer solutions.

1
  • Or you might go in a way to use new Unity input system which will create code for events handling, and you could directly fire those events from your code instead. Commented Sep 23, 2021 at 5:26

2 Answers 2

5

It is far from perfect, but here is a jumping-off point on what you can do with the old input system.

using UnityEngine;
using UnityEngine.EventSystems;

public class TestScript : StandaloneInputModule
{
    [SerializeField] private KeyCode left, right, up, down, click;
    [SerializeField] private RectTransform fakeCursor = null;

    private float moveSpeed = 5f;

    public void ClickAt(Vector2 pos, bool pressed)
    {
        Input.simulateMouseWithTouches = true;
        var pointerData = GetTouchPointerEventData(new Touch()
        {
            position = pos,
        }, out bool b, out bool bb);

        ProcessTouchPress(pointerData, pressed, !pressed);
    }

    void Update()
    {
        // instead of the specific input checks, you can use Input.GetAxis("Horizontal") and Input.GetAxis("Vertical")
        if (Input.GetKey(left))
        {
            fakeCursor.anchoredPosition += new Vector2(-1 * moveSpeed, 0f);
        }

        if (Input.GetKey(right))
        {
            fakeCursor.anchoredPosition += new Vector2(moveSpeed, 0f);
        }

        if (Input.GetKey(down))
        {
            fakeCursor.anchoredPosition += new Vector2(0f, -1 * moveSpeed);
        }

        if (Input.GetKey(up))
        {
            fakeCursor.anchoredPosition += new Vector2(0f, moveSpeed);
        }

        if (Input.GetKeyDown(click))
        {
            ClickAt(fakeCursor.position, true);
        }

        if (Input.GetKeyUp(click))
        {
            ClickAt(fakeCursor.position, false);
        }
    }
}

Set the KeyCode values to whatever you prefer. In my example, I set a UI image to the cursor and set the canvas renderer to Overlay, so the coordinates were already in screen space. I replaced the InputModule on the scenes EventSystem with this script.

Here is a gif of the script:

Example

I am moving my fake cursor around the screen using wasd and when I hit space, it simulates a click event on the position of the fake cursor.

Sign up to request clarification or add additional context in comments.

Comments

0

As an alternate way to the answer above, unity has a package "Input System "to debug inputs.

Here is how to get it & setup;

1- Open Window -> Package Manager package manager

2- Install Input System package

input system

3- Now you will be able to see input debugger under Window -> Input Debugger

Input Debugger

4- Open Input Debugger and click on Options -> Simulate Touch Input From Mouse or Pen

Simulate Touch

Done!

1 Comment

I don't think this fundamentally solves the problem as all it can seem to do is simulate ONE touch with the mouse. For properly testing a touch application, this seems like not nearly enough. Anything that only needed one touch as if that touch was simply a cursor could just be tested with the cursor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.