Simulate Keyboard Input

I have this small web application that I’d like to control using html buttons.

On my Unity scene I’m using a RotateCamera.js script that allows the user to rotate the camera around a pivotal point using the WASD or arrow keys (it’s the same script used in the ShadowDemoProject).

What I’m trying to do is, I have 4 arrow key buttons on the web page, and I’d like for the application to simulate the corresponding arrow keys being pressed and rotate the camera around by putting the mouse over the arrow key buttons on the web page.

I’ve been messing around with the Input class, Input.GetKey, keyCodes, etc. but I’ve failed miserably.

Is what I’m suggesting even possible? All I’d need is for a way to simulate inputting keys on the keyboard by clicking or hovering over a web page button.

Any help would be greatly appreciated.
Cheers.

Instead of writing

If (Input.GetButtonDown ())
{
   transform.Rotate ();
}

Write :

If (Input.GetButtonDown ())
{
   Rotating ();
}

function Rotating ()
{
   transform.Rotate ();
}

So you are able to call Rotating (), even outside of Input.GetButtonDown.

Well, in a case like this, all you have to do is set up the rotation around the center to automatically happen if a boolean is true. Then, you just set up the arrow buttons to be RepeatButton type, which makes them set a boolean to true every frame they’re held down, and you’re all set.

var leftArrow : boolean = false;

function OnGUI()
{
   leftArrow = GUI.RepeatButton(Rect(x,y,width,height), "Content");
}

Something along the lines of the above.

Then, you just need the code for rotating to run whenever the boolean is true.

function Update()
{
   if (leftArrow) {
      // Rotate left code
   }
}

First of all, thanks to both of you for your replies.

@AkilaeTribe
The problem is that the Input.GetButtonDown() function only works IF the user is pressing a key, which is what I’m trying to emulate without actually pressing it.

@Gargerath Sunmann

So what you are suggesting is to change the way the camera is rotated?
Currently this is the code for the CameraRotation.js script:

var target : Transform;
var edgeBorder = 0.1;
var horizontalSpeed = 360.0;
var verticalSpeed = 120.0;
var minVertical = 20.0;
var maxVertical = 85.0;

private var x = 0.0;
private var y = 0.0;
private var distance = 0.0;

function Start()
{
    x = transform.eulerAngles.y;
    y = transform.eulerAngles.x;
    distance = (transform.position - target.position).magnitude;
}

function LateUpdate()
{
    var dt = Time.deltaTime;
    x -= Input.GetAxis("Horizontal") * horizontalSpeed * dt;
    y += Input.GetAxis("Vertical") * verticalSpeed * dt;
    
    y = ClampAngle(y, minVertical, maxVertical);
     
    var rotation = Quaternion.Euler(y, x, 0);
    var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    
    transform.rotation = rotation;
    transform.position = position;
}

static function ClampAngle (angle : float, min : float, max : float) {
    if (angle < -360)
        angle += 360;
    if (angle > 360)
        angle -= 360;
    return Mathf.Clamp (angle, min, max);
}

But I think what you are suggesting is to add OnGUI functions with some buttons embedded in the application. The problem is that the arrow buttons are OUTSIDE the application. They are in the web page where the application is embedded, and I’d like for the user to be able to put the mouse over the button (without clicking) and simulate the arrow-key being pressed behavior. I think that the GUI.RepeatButton behavior can be implemented by using the onMouseOver html feature, but now I’d need for a way to emulate that behavior for those two LateUpdate() lines:
x -= Input.GetAxis(“Horizontal”) * horizontalSpeed * dt;
y += Input.GetAxis(“Vertical”) * verticalSpeed * dt;
I think that is what makes the camera rotate in this case whenever you press an arrow key.

There is a manual page here that explains how to communicate between the JavaScript in the host web page and the Unity player. The idea is that you can detect the events in the page and then call functions in the player to respond to them.

Hi andeeee.
Thanks for your reply. The communication between Unity and the Web page is not an issue. The application does several communications with the web page (both ways) and those work fine. The problem is finding a way to implement a simulation of a key being pressed while you put your mouse over a web page button (without clicking it, using the onMouseOver directive).

You can rotate the camera around in the application by using the arrow keys. I’d like to give an extra option for the users so that they can rotate the camera using the mouse and the buttons displayed on the web page.

kernelPANIC, AkilaeTribe already gave you a (proper?) answer. Reorganize your code so it’s not so dependent on directly accessing the keyboard input and invoke the behaviour scripts manually via other means.

delighted

A problem :?:

There might be a better answer; perhaps Unity does have some built-in input-emulation API that I don’t know aboot. But I wouldn’t count on it.

Plus I want to stop talking oot of my ass. There may be a better way, generically speaking, of doing it. But I’ve never had to do simulate a thing with my applications so I’m just going by what “makes sense” to me.