6

I'm very new to scripting in Unity, I'm trying to create a button, and once clicked it needs to simulate the 'F' Key being pressed (To pick up an item)

Here is the current code I have, I've looked all over unity forums before writing this but couldn't find anything that worked.

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class button : MonoBehaviour {

    public void ButtonToClick(int clickToButton)
    {
        SendKeys.Send("F");
    }
} 
Galandil
  • 4,169
  • 1
  • 13
  • 24
Casper Round
  • 91
  • 1
  • 1
  • 4
  • why doe sthis have a javascript tag? – I wrestled a bear once. Feb 22 '18 at 14:47
  • OP what platform are you creating for? When you write "Simulating" do you mean that the user will use the UI/Touch button or do you mean a press on the keyboard? – Doh09 Feb 22 '18 at 15:34
  • I don't understand why all these answers. None of them actually answered the question and they should not work. Simulating a key press is **platform dependent** since that's not any part of current Unity API. First, you have to mention what platform you are targeting. Secondly, you have Google each platform, find a C++, Java(Android) code to simulate a key press then use C# to bring them together and make one single API to do that. Any other thing, you are wasting your time. – Programmer Feb 22 '18 at 16:07
  • @Programmer actually the answer of bilal1409 is the only one that addresses the question, even though only for Windows platform. – Galandil Feb 22 '18 at 16:56
  • @Galandil Yes, it did address it but partially because it works on Windows only and he should have mentioned that since there is no windows tag in this question. Also, you don't even need that library he linked. You can invoke C++ library [directly](https://stackoverflow.com/a/3047971/3785314) from C# to make it work on Windows. – Programmer Feb 22 '18 at 17:23

2 Answers2

3

I believe simulating the key press is not the right way to do it.

Instead, you should call the PickUp function when the button is clicked the same way Pickup is called when the F key is pressed.

// Drag & Drop the object holding the script to the `OnClick` listener of your button
// Then, simply select the `Pickup` function
public void Pickup()
{
    // code ....
}

private void Update()
{
    if( Input.GetKeyDown( KeyCode.F ) )
        Pickup() ;
}
Hellium
  • 7,206
  • 2
  • 19
  • 49
  • 1
    This is the best way to do this. But in my app, I use arrows and enter to move between buttons using unity navigation system (without using any other function), How to simulate arrows clicks on this case? – MBS Feb 22 '18 at 15:21
  • Do you mean you want a GUI button (called **B**) to simulate the keypress of the arrow so as to navigate between the buttons **A1** and **A2**? I fear this is not possible, because once you click on the GUI button **B**, **A2** would be selected (thanks to a custom function), but then, **B** would be selected (because you have clicked on it) so the navigation system gets broken. I may be wrong though, since I don't know when exactly a `Button` gets selected (before / after the `OnClick` event is raised) – Hellium Feb 22 '18 at 15:38
  • @Hellium, if you make a system where you assign a value to each UI element, then have arrows cycle that int value up and down and select the current by int value selected UI element, that would work. It's more manual but it would work. To make it use up and down arrows you can create an int value for vertical and then use left/right for horizontal. – Doh09 Feb 22 '18 at 15:40
  • How would I include the main pickup controller C script, as this is being written on the button C Script – Casper Round Feb 22 '18 at 17:21
  • I don't think the Pickup script should be attached to a GUI button, but to the entity picking up the object (your player maybe). Then, you will be able to bind the button **C** to the Pickup script. – Hellium Feb 23 '18 at 08:04
-2

From this question:

  1. Download zip file on http://inputsimulator.codeplex.com

  2. Unzip that to Assets directory with Your script (C#) in Unity project

  3. Reload MonoDevelop (if is opened)

  4. In script on top write: using WindowsInput;

  5. In your class you can use this, for example:

    InputSimulator.SimulateKeyPress (VirtualKeyCode.RIGHT); //simulate right arrow press

Edit:

You can install it without the above link using Nuget:

Install-Package InputSimulator

Check it's repository on github.

MBS
  • 673
  • 2
  • 16
  • 48
  • Will this work when on mobile though? - The prefab inventory script uses the key F, hence why I'm routing a button so the user can pick it up via mobile – Casper Round Feb 22 '18 at 14:46
  • This library simulates key clicks as if you click on the corresponding key – MBS Feb 22 '18 at 14:51
  • You don't need to download anything to simulate a normal button press in unity, just use Unity's Input class instead, i'll write an answer below explaining it. – Doh09 Feb 22 '18 at 14:53
  • This works, but only on Windows, it doesn't on iOS/Android. – Galandil Feb 22 '18 at 15:14
  • WARNING: website returns errors and security warnings. I don't recommend entering – Darkgaze Nov 19 '19 at 18:43