- Home /
Simulating key presses
I want to use HTML buttons to make a character in Unity walk around in an environment by pressing the HTML buttons. I know how to communicate between HTML and Unity already, and I basically plan to use onMouseDown on the HTML button to update a UNITY boolean to true, and then the update function will execute code while the variable is true. And onMouseUp will change the variable back to false, stopping the update function from running that code. The problem is that I cannot figure out how to simulate pressing and holding down keyboard keys through code. I am using javascript in Unity and the character already walks using the arrow keys or w,a,s,d keys. Can anyone tell me how to simulate keyboard key presses through code? Any help would be greatly appreciated. Thanks in advance.
Answer by Jean-Fabre · Nov 18, 2010 at 09:59 AM
Hi,
It is possible to simulate keypress and any inputs really if you wrap all your calls to a class you built yourself. I do that always anyway, not for simulating but for allowing several different types of input to affect a single action in my application, and to have a lot more controls without affecting my game, for example depending on the context, I might want to disable all inputs or only some of them, etc etc.
so instead of querying
function Update () {
if (Input.GetKeyDown ("space"))
print ("space key was pressed");
}
you will now call
function Update () {
if (InputBroker.GetKeyDown ("space"))
print ("space key was pressed");
}
Where InputBroker is a script you wrote yourself with a static function GetKeyDown
By having this level of indirection, you can now call your InputBroker and force it to simulate a keypress or something else.
InputBroker.SetKeyDown ("space");
in InputBroker, you register space to be down for a frame in a private hash variable forcedKeyDowns or something.
then in your getKeyDown for InputBroker you would have something like
static function GetKeyDown(aKey){
return Input.GetKeyDown(aKey) || _forcedKeyDowns.Contains(aKey) ;
}
This is an idea, if you need a working code, I can spend a little more time on this, no problem.
Hope it helps,
Jean
Ahh, I'll start using this solution. Simple, yet terribly effective ^^
Is there an example of this somewhere. I don't know who to believe dreamora or you. I don't see anyway to simulate a keypress in unity, so wouldn't it have to come from the system?
$$anonymous$$eyboard events simply throw out a $$anonymous$$eyCode. Just bypass the event and use the keycode directly. Ultimately, you need to write code to respond to this anyway, so cut out the middleman.
I'm confused, doesn't this require a keyboard event to begin with. How do you generate the keyboard event in the first place without the keyboard.
Hi $$anonymous$$,
Would you be able to post some working code? It would be very helpful.
What $$anonymous$$ is suggesting is that you can have it set up such that one input will come back with information from another if you so choose.
You can either Generate a $$anonymous$$eyCode or simply skip to the output.
The actual "Event Generation" is what Dreamora refers to as Not Possible.
This little snippet can demonstrate
void Update () {
if(Input.Get$$anonymous$$ouseButton(0))
{
my$$anonymous$$ey = $$anonymous$$eyCode.G;
Debug.Log("Click handled " + my$$anonymous$$ey);
}
if(Input.Get$$anonymous$$ey(my$$anonymous$$ey))
{
Debug.Log("GGGGGGGGGGGGGGGGGG");
}
}
A Left $$anonymous$$ouse Click generates a $$anonymous$$eyCode.G and places it in a variable. See how Debug chucks out a G. This level of emulation is easy.
The next line Get$$anonymous$$ey(my$$anonymous$$ey) will NOT fire as the event was not generated through the Event system, however, pressing G on the keyboard still fires the event as my$$anonymous$$ey = $$anonymous$$eyCode.G;
I would like to investigate further when I have more time. I tried the Windows.Input $$anonymous$$eyConverter but "$$anonymous$$ethod not implemented" was my greeting after I'd set up the namespace.
I've tried to do something like this by simulating Delete when I press the left key, but it doesn't seem to work. It just moves left in the InputField.
This code doesn't work for me. Could you please give your InputBroker? For example
Your answer
