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.

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

it is not possible to simulate keypresses.

SetKeyDown? This does nothing.