Author Topic: Is it possible to simulate a keypress/input in code?  (Read 12773 times)

Der_Kevin

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
Is it possible to simulate a keypress/input in code?
« on: June 10, 2014, 09:03:47 AM »
Hey!

I have a simple question. is it possible to map a keyboard key to an NGUI onscreen button?
for example when i press an NGUI button unity gets told that i pressed Y on my keyboard?

spooky

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 16
    • View Profile
Re: Is it possible to simulate a keypress/input in code?
« Reply #1 on: June 10, 2014, 11:25:54 AM »
Use the script UIKeyBinding on the button. Video explanation here https://www.youtube.com/watch?v=jDxWG81sNPc&feature=youtu.be&t=16m55s

Edit: Wait that's backwards. I'm not sure if you can do that; regardless, it would make more sense to not simulate key presses, and instead accept key presses on your buttons, which call the function which your key press would execute when clicked. The same effect is accomplished that way, but you don't have to pretend to be an input device. Just map your input code and button code to the same function.
« Last Edit: June 10, 2014, 12:11:11 PM by spooky »

Der_Kevin

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Is it possible to simulate a keypress/input in code?
« Reply #2 on: June 10, 2014, 12:06:16 PM »
not realy. i want it the other way around.
with the key binding you press the button by pressing Y
but i want that when i press a button it tells unity i pressed Y on the keyboard.

for example i have a box and when i click y on my keyboard it opens up. so what if i dont have a keyboard (mobile for example)? I want a y button on my screen that i can click and it performs the "if key Y go down" action. you know what i mean?

spooky

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 16
    • View Profile
Re: Is it possible to simulate a keypress/input in code?
« Reply #3 on: June 10, 2014, 12:09:21 PM »
I edited my earlier post, since I realized I'd misunderstood your original post.

A vague example, based on what you said:
  1. public class YourClass : MonoBehaviour
  2. {
  3.    void Update ()
  4.    {
  5.       if (Input.GetKeyDown(KeyCode.Y)) DoSomething();
  6.    }
  7.    public void DoSomething ()
  8.    {
  9.       //whatever is currently happening when 'Y' is pressed
  10.    }
  11. }
  12.  

Then your UIButton calls DoSomething() as well, and you can use a UIKeyBinding to make the button be a shortcut for the Y button, so that you don't need the Update() code to be redundant, but only if your virtual 'Y' button will be enabled even when there is a keyboard.
« Last Edit: June 10, 2014, 12:17:56 PM by spooky »

Der_Kevin

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Is it possible to simulate a keypress/input in code?
« Reply #4 on: June 10, 2014, 12:23:59 PM »
Hey, thanks! that helped a lot :)

I have another question since i am just getting into all the input stuff in unity and iam messing a little bit around with the input manager. so lets say i have set up a fire button in the input manager (Fire1) and my code looks like this

  1. void Update() {
  2.         if (Input.GetButtonDown("Fire1"))
  3.             Instantiate(projectile, transform.position, transform.rotation) as GameObject;
  4.        
  5.     }
  6.  

how can i say that my NGUI button is the "Fire1" button?
i mean my positive button is left ctrl but cant i say somehow that that my alternative postive button is a ngui button or a game object?

spooky

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 16
    • View Profile
Re: Is it possible to simulate a keypress/input in code?
« Reply #5 on: June 10, 2014, 12:44:06 PM »
You can accomplish that in the exact same way I described above.
Just make a wrapper function for Instantiate() named something like 'Shoot', so instead of calling Instantiate() you call Shoot() and that calls Instantiate() with the parameters you would normally set. Then, if Shoot() is public, you can assign it to the on click event of your NGUI button... However, you might want to take a look at some tutorials for the basics of C# programming, since using functions (aka Methods) is a fairly basic and critical component to writing code.

You can check my code from the previous post for reference on what this description would actually look like, except instead of 'DoSomething' we're calling it 'Shoot'.