Simulate Keypress

So I know this was possible in 4.6 of Unity, now the code changed allot in Unity 5
and im not the best coder now im wondering if it is still possible to simulate a keypress when I click on a 2d sprite with a 2d collider (box).

so it would go like this

  1. click on sprite (with 2d collider)

  2. simulate the key ‘‘i’’

not sure if this is hard or easy

any help from a documentation till a complete script would be very appreciated!

I don’t think you want to be simulating keys by invoking the Events for key presses. Instead call the method that Key would have invoke should it have been pressed.

Without any context this is what I could think of. Not sure what your trying to achieve.

// If the 'I' key was pressed.
if(Input.GetKeyDown(KeyCode.I)) {
    SomeMethod();
}

// If your sprite was pressed.
void OnMouseDown() {
    SomeMethod();
}
1 Like

im trying to achieve the ability to press ‘‘i’’ without a keyboard, but by clicking on a sprite with my mouse ingame

You can’t mess with the Input class manually. So typically you need a layer of abstraction between Input and your code. That way you can fake the input when the conditions are right.

Basically what @Polymorphik said.

1 Like

May be useful with the ZFBrowser asset from the Unity Asset Store in order to automatically feed some values into the webpage!