1

How can I press a key from the C# script? All I found it's just how to get click on a key, I did not find how to press programmatically on a key. How can I do that?

I want to make the key pressed. Not to check if the key is pressed.

For example, I want to press Enter with my C# script.

  • Why do you need to? You could be going about it the wrong way – BugFinder Jan 13 '21 at 19:28
  • I want to make the key pressed – זיו שיאון Jan 13 '21 at 19:36
  • 2
    There's not a motor in the key so you can't have the code press it. Maybe if you make a robot finger that can press it ;) But I have a feeling you're not actually interested in the key being pressed, you're interested in *something* reacting as if the key was pressed. So, what is that *something* in your use case? – Ruzihm Jan 13 '21 at 19:57
  • Ok, I have an idea. Thanks – זיו שיאון Jan 13 '21 at 19:58
  • 1
    The [command pattern](https://www.habrador.com/tutorials/programming-patterns/1-command-pattern/) might be of interest if that *something* is other parts of the code you are writing. – Ruzihm Jan 13 '21 at 20:01
  • 1
    Thanks. Have a good day – זיו שיאון Jan 13 '21 at 20:04
  • FWIW OP may have actually been wanting to simulate a keypress. I have that need to use a VR keyboard to simulate keypresses so that standard input events are generated so a 3rd-party component can react as if a key has been pressed without me directly calling anything in it. – michael Nov 01 '21 at 20:13

1 Answers1

0

There is no way in vanilla Unity to simulate a keypress, I believe. Based on what you require, it's most likely that you're trying to simulate an event happening. What you could do is simply make a function that's going to execute the event that's going to happen when you press ENTER and then call that event whenever it's supposed to be executed.

It's already been discussed here : How to simulate a key press on button click - Unity

Alvandi
  • 16
  • 3