- Home /
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?
Same question please, it saves me many lines on a complicated demo scene.
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.
Answer by petrnita · May 02, 2015 at 02:52 PM
This is solution:
Download zip file on: http://inputsimulator.codeplex.com
Unzip that to Assets directory with Your script (C#) in Unity project
Reload MonoDevelop (if is openend)
In script on top write: using WindowsInput;
and ... in class You can use this for example: InputSimulator.SimulateKeyPress (VirtualKeyCode.RIGHT); //simulate right arrow press
Enjoy :)
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?
Unfortunately this doesn't work on a $$anonymous$$ac - I get "DllNotFoundException: user32.dll"
Have you find some solution on $$anonymous$$ac?
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
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.
This is the perfect solution for what I needed. Thanks!
Accept the answer then... and mark the question as closed!
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
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
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);
}
}
Hi, it works for me but only 1 time, the next time i call the function it dosent work anymore, any idea why please ?
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);
}
}
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
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);
...
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.
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.
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.
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
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.