- Home /
simulate a keypress from a script
hey guys , is it possible to simulate a key press with a script like , when the game starts i want the spacebar to be pressed withtout me pressing it , i want it to be pressed from the script ! ,thanks.
Answer by petrnita · May 04, 2015 at 09:35 AM
This is solution: http://answers.unity3d.com/answers/958624/view.html
This one is also relevant
http://answers.unity3d.com/questions/28473/simulating-key-presses.html
Answer by tanoshimi · Aug 06, 2014 at 01:31 PM
No that I'm aware of. I can't think why you'd need to do this though. Let's say your current code has something like:
void Update() {
if(Input.GetKey(KeyCode.Space)){
DoSomethingWhenSpaceIsPressed();
}
}
Then to "simulate" that keypress when the game starts, you'd simply have to add:
void Start() {
DoSomethingWhenSpaceIsPressed();
}
Or have I misinterpreted your question?
hey @tanoshimi thanks for the help but i think u misunderstood me a little :D ...what i wanted is , when the game starts or like when i enter a trigger , i want the Spacebar to be activated (pressed) from the script in realtime , without me pressing it on the keyboard
Yeah, but the important thing is Why?
If it's just that you want the game to do a function that is tied to your space bar, then to perform it again simply call the function by itself. And if the code is not in a function on its own and you don't want to make it so, just copy paste the code.
Your answer
Follow this Question
Related Questions
Press Ui Button To Simulate Keypress 3 Answers
How could i execute a command move for a fighting game? 3 Answers
Best way to handle keypress? 3 Answers
Odd input glitch 2 Answers
Need to press key twice to react 1 Answer