Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by simpson45711 · Dec 03, 2013 at 05:44 PM · buttonnguikeypress

Is it possible to simulate a keypress/input in code?

Say i had an input named 'fire' mapped to a key 'z'. Is there any way to simulate either a button press or input value (within a ngui on screen button) in code rather than creating isolated functions for the fire action?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image frogsbo · Jan 18, 2014 at 07:50 PM 0
Share

Same question please, it saves me many lines on a complicated demo scene.

avatar image MrVerdoux · Jan 18, 2014 at 09:32 PM 1
Share

shouldn't your input conect with functions? Just call them. If the input does more than calling one function you should consider wraping all the action of an input key to another function.

5 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by petrnita · May 02, 2015 at 02:52 PM

This is solution:

  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 openend)

  4. In script on top write: using WindowsInput;

  5. and ... in class You can use this for example: InputSimulator.SimulateKeyPress (VirtualKeyCode.RIGHT); //simulate right arrow press

  6. Enjoy :)

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dansav · Oct 27, 2015 at 02:31 AM 0
Share

I tried this code and it works great except I get duplicate characters for each keydown. I'm trying to type in uwebkit (a browser inside unity) so it may have something to do with that. Could someone verify that it did not duplicate characters when they used it?

avatar image Fr2 · Aug 07, 2016 at 08:50 AM 1
Share

Unfortunately this doesn't work on a $$anonymous$$ac - I get "DllNotFoundException: user32.dll"

avatar image cracker999999 Fr2 · Dec 19, 2016 at 05:30 AM 0
Share

Have you find some solution on $$anonymous$$ac?

avatar image Hathakas · Dec 29, 2016 at 03:42 AM 1
Share

This is almost perfect. it works exactly how I want it in the editor/player but when I build it onto android, it does not work!

Any ideas? I've been working on finding a solution for this all day :(

Thanks

avatar image Harinezumi · Sep 06, 2017 at 08:53 AM 0
Share

For those wondering, this will only work on Windows, because (as the name says on the link), the project is Windows Input Simulator. It uses Windows DLL to send input events to the operating system. So those developing for $$anonymous$$ac, Android, etc. are out of luck with this.

However, check out the answer below that refers another answer, that will be platform independent.

avatar image imranmouna · Oct 03, 2017 at 10:06 PM 0
Share

This is the perfect solution for what I needed. Thanks!

avatar image tormentoarmagedoom imranmouna · Apr 11, 2018 at 02:50 PM 0
Share

Accept the answer then... and mark the question as closed!

avatar image Bunny83 tormentoarmagedoom · Apr 11, 2018 at 03:31 PM 0
Share

Uhm, "imranmouna" did not ask the question ^^. However i'll accept the answer and closing the question to prevent further "thank you" bumping. If you want to express your gratitude just upvote the answer / question

Show more comments
avatar image
3

Answer by zeppike · May 02, 2015 at 10:01 AM

I would suggest this answer http://answers.unity3d.com/questions/28473/simulating-key-presses.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by kirbyderby2000 · Aug 11, 2020 at 09:08 AM

The post mentioned by petrnita worked perfectly for me on Windows in Unity 2019. I see a few people mentioned that they got missing namespace errors trying to use the solution he linked; to fix this go into the "inputsimulator\sourceCode\inputsimulator" directory of the download, then delete the "WindowsInput.SampleClient.Wpf" and "WindowsInput.Tests" folders.

The API is also used differently since his answer was shared. Use the API as shown below:

 using UnityEngine;
 // Include the WindowsInput namespace
 using WindowsInput;
 
 public class InputTest : MonoBehaviour
 {
 
     public void TestKeyInput()
     {
         // Build an input simulator instance
         InputSimulator inputSimulator = new InputSimulator();
         // Then call the keyboard key down method, pass in the enum virtual key code you want to press
         inputSimulator.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.VK_1);
     }
 
 }


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SIV · Aug 31, 2020 at 07:30 PM 0
Share

Hi, it works for me but only 1 time, the next time i call the function it dosent work anymore, any idea why please ?

avatar image kirbyderby2000 SIV · Aug 31, 2020 at 08:17 PM 0
Share

What's the context where the method is called? Have you tested to see if the method simply isn't being called more than once?

Try caching the InputSimulator variable if you're going to use it often like so

  using UnityEngine;
  // Include the WindowsInput namespace
  using WindowsInput;
  
  public class InputTest : $$anonymous$$onoBehaviour
  {
     // Build an input simulator instance
     InputSimulator inputSimulator = new InputSimulator();
  
      public void TestKeyInput()
      {
          
          // Then call the keyboard key down method, pass in the enum virtual key code you want to press
          inputSimulator.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.VK_1);
      }
  
  }



avatar image SIV kirbyderby2000 · Sep 02, 2020 at 11:09 AM 0
Share

I didint get what do you mean by the context its called in, what i did is making a UI button that whenever user press on it (its for mobile version) it will call what the "space bar" is about to call, i tried your second script now it dosent work at all

avatar image
0

Answer by DataSmith2112 · Apr 11, 2018 at 12:55 AM

Create Empty Object and disable it. Word the if statement with an || option.

Than you can trigger the key if statement just by activating the "KeyObject" from any script.


...

public GameObject keyObject;

...

if (Input.GetButtonUp (Key) || keyObject.activeSelf == true) {

keyObject.SetActive (false);

...

...

}


Now from a different script ...


...

public GameObject keyObject;

...

keyObject.SetActive (true);

...

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DataSmith2112 · Apr 11, 2018 at 02:07 PM 0
Share

Why would you give this dislikes? If you are looking to write system code use a system language. If you are looking to simulate a key press in your own unity code this works like a charm.

avatar image Bunny83 DataSmith2112 · Apr 11, 2018 at 03:37 PM 1
Share

Yes, it's the more practical / general answer ^^. Though it highly depends on what you want to achieve. Also using the active state of a gameobject just to issue an event is not a great design. You better use a boolean variable.


Anyways i gave you an upvote.

avatar image DataSmith2112 Bunny83 · Apr 11, 2018 at 11:28 PM 0
Share

Thank you. Using a game object as a switch is cheap effective and one of the most powerful ways to design. Skyrim is one huge example. $$anonymous$$ost all the code relies on these types of "switches". This method is way more powerful than you may think when combined with each other. One of the bonuses is it can be added or removed from existing code easily. This is one of them out of the box way of doing things. I learned it from Skyrim over the 10 years I modded that game. At first I too thought it was kind of odd. Later after chaining together multiple outcomes via object switches. I started to see just how powerful it was and why Skyrim used it so much. Let's say you have 10 different scripts that all can't be fired unless multiple factors are in place from other scripts. You could just test every factor or you could just have the script set up an object to be used as the flow director. It's like having a tangible global that any script can access on the fly with ease. I understand this can be done many ways ie: just a simple global variable. But in the end a system based off these types of switches is more than easy to read and understand at a glance. I myself use many ways to direct my programs flow. This is definitely one of my favorite methods to use when using multiple scripts. Expectually in the design phase. In fact one of the most compelling examples of how well this works is your own body. As long as the empty object is just being used as a switch as far as the compiler looks at it, it is just a global variable. The difference is this "switch" has talents. It can be (lol) self aware. Not only able to hold a "on/off" value but able to incite change itself. $$anonymous$$ind of a hardcore variable ... Just try a few tests yourself, it don't take long to see the power of this technique. :)

...

The truth is you can simulate a keypress but, whatever system you use will not be compatible on multiple platforms or possibly not at all months later. This technique will always work. It will work in your code today and years later it will still work perfectly. It is not dependent on some other language that may change over time.

...

Also, you do know there is a reason a program like Unity would choose to leave out things of this nature. Unity is protecting itself from malware programmers with hostile intent.

avatar image
0

Answer by DRProductions · Jan 18, 2014 at 08:03 PM

The best option is to just add a function for firing but if you don't want to do that you can try:

JavaScript: http://stackoverflow.com/questions/596481/simulate-javascript-key-events

C# : http://social.msdn.microsoft.com/Forums/windows/en-US/f1b195b7-1568-46f5-83bb-e1e85b188af2/how-to-simulate-a-key-press-in-c?forum=winforms

These solution may or may not working in unity but it is what I found on the internet

I strongly suggest making a function as you may find that you want to fire in many different ways and functions just simplify your code so much.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NGUI UIButton Event question 1 Answer

Disable Sprite when keypress (Working Script, just need disble) 0 Answers

Press Ui Button To Simulate Keypress 3 Answers

Access NGUI button's notify(OnClick) 1 Answer

NGUI: Button don't detect input if parent change position on-screen? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges