Keypress Prefab

Hi!

I’ve been doing some research and have been trying to come up with a solution but so far with no luck :frowning:

what i’m hoping to achieve is that when you press the Cube prefab i have on the screen with the mouse it will then simulate me pressing the Space key.

So far this is what i’ve got with no progress…

function OnMouseDown () {

if (Input.GetMouseButtonDown(KeyCode.Space))
           
}

Any ideas?

Well first of all, you cant use Input.Get in the way you’re trying to use it. Input.GetKeyDown/MouseButtonDown is a function that returns true if the given keycode is currently down. It doesn’t simulate a keypress using the passed keycode. Also, Input.GetMouseButtonDown(KeyCode.Space) would never return true as that particular function looks for mouse input only (0, 1, 2, etc).

This UnityAnswer is a pretty good example of how to simulate keypresses via code. However, if it isn’t absolutely necessary for you to simulate the “key” but rather the function attached to that key, then it’s as easy as making that function accessible to this portion of the script and calling it using this method:

if(Input.GetMouseButtonDown(0)) //0 = Left Mouse 1 = Right Mouse
{
//This would be where you put the function call that occurs
//normally when the player presses SPACE
    SpaceBarFunction();

}

So I am extremely interested in this :slight_smile: Would you be able to attach a pic of the Profiler, especially during the loading hiccup?? :smile:

1 Like

Thanks for the answer McMayhem! i dont seem to be having much luck with Javascripts… i think i have a basic outline of where im going… if someone could correct my script, or help me find something similar it’d be much appreciated!

function OnTriggerEnter(Col : Collider){  //detect mouse click?

if(Input.GetMouseButtonDown(0)) //detects that user has clicked on the prefab
   
     Input.GetKey("Space") //when prefab is clicked, simulate that the space bar was pressed
}

i think i may be getting closer! Hopefully! :stuck_out_tongue:

Fersut - is there any reason why you need to simulate the key press?
As per mayhems comment, the keypress is only a stepping stone to doing something usually (i.e. normally when I press space bar, its because I want a space). You could use his example of a function to just skip the input part, as really what your doing is effectively creating a virtual keyboard, why bother with the inputs side of the world?

public string OnSpaceBarClick()

{
return " ";
}

You could use the above as a way of getting that value back anyhow.

Also Input.GetKey returns a Boolean value based on whether the key has been pressed or not, so wont work in your script above.

1 Like

haha i totally responded to the wrong thread (too many tabs open) and now I can’t find the post I was looking at yesterday >.<

I think we’re all having mental hiccups today :smile:. I just realized I talked about a UnityAnswer post without ever actually linking it in the post! Here is the post I was referring to that talks about how to simulate keypresses.

You’ll not that from that post, it’s a pretty complicated process as there is no native way to simulate a keypress in raw form with Unity’s vanilla API. The best way to do it is to have a single standard function that handles what to do when pressing space, then access that function when clicking on the object.

Example Spacebar Code:

public void OnSpaceDown()
{
     Debug.Log("Space bar was pressed!");
}

You would then call OnSpaceDown() whenever you wanted to simulate that keypress. This will not work if your desire is to have some external program register that the spacebar was pressed. This will only work within Unity. (That’s the only scenario I can think of where you’d actually need to simulate raw input)

As for your way of handling the code, I think I have the general idea of what you’re trying to accomplish. It seems like you want to simulate the space key when the player clicks on the object. If that’s the case then we need to make some adjustments to your code.

Let’s break it down:

//Actually, OnTriggerEnter only detects collision with other objects in the scene, and has nothing to do with mouse clicks
function OnTriggerEnter(Col : Collider){  //detect mouse click?
if(Input.GetMouseButtonDown(0)) //detects that user has clicked on the prefab
   //Input.GetKey, Input.GetKeyDown, Input.GetKeyUp all return a boolean (true or false) and thus would not do anything right here. This is where you would execute your OnSpaceDown() function
     Input.GetKey("Space") //when prefab is clicked, simulate that the space bar was pressed
}

Since you want this to happen when the player clicks on the object, you have to use a different event called “OnMouseDown” which does actually require a trigger collider to be attached to the object.

This is what it should look like:

 void OnMouseDown() {
        OnSpaceDown(); //Simulate the spacebar
    }

Hope that helps a bit.

1 Like