- Home /
Detect a specific Key Press Event Without Keyboard Input
For key press normally we use Input, but I want to call a specific key press event without giving input from keyboard. In details I want a key press (space) event running with a Boolean value. But that key press event is not be called from keyboard input.
If (Input.GetKeyDown("space")) {
//space key is pressed
}
But I want:
If(press == true) { //here press is Boolean
//call space key is pressed event without key board input
}
Is it possible in unity?
Hi, Do you want to simulate keyboard keys with out taking input from user? or just to call a method(Code Snippet) ?
Answer by Jamora · Mar 26, 2014 at 02:24 PM
You need to take advantage of a programming concept called subroutines. They are also called functions, or methods and possibly several other names.
Define a function that does whatever a spacebar press should do, then call it whenever you need that action performed. Functions are defined in C# usually as
< access modifier > < return value > < function name > (< parameters >){
< method body >
}
that means, for example
public void SpacePressed(){/*empty method*/}
and in UnityScript as
function < function name >(< parameters >)< : return value, optional >{
< method body >
}
so
function SpacePressed(){/*empty method*/}
You call functions by their name:
SpacePressed();
But I want to call the space key is pressed event without checking the Input.Get$$anonymous$$eyDown("space") and without any keyboard input.
You don't need any input to call methods. Just call them when you want in your code. You can call methods at any given moment, by writing the method's name. I gave an example in the last code snippet in my answer.
Basically, to solve your problem, just put the method call inside the if clauses in your question.
Sorry I can't get you understand my question.Suppose I have no keyboard but I want to call Input.Get$$anonymous$$eyDown("space") event, then what can I do. In details I have a code where I set a boolean true when I press my space key.But I want unity to call space key press event automatically when I am not pressing space key in my keyboard.
Because your boolean is the same thing as having pressed space bar, then just set the boolean true somewhere else, and it's the same thing as having pressed space bar.
To illustrate, you make a method that performs whatever your space press performs, and then call if within your two ifs.
Let's say you want to print "Space was pressed" every time space was pressed. Your method would be, in C#
public void SpacePressed(){
print("Space was pressed");
}
And you'd call it as follows:
If (Input.Get$$anonymous$$eyDown("space")) {
SpacePressed();
}
If(press == true) { //here press is Boolean
SpacePressed();
press = false;
}
Now, whenever you set press to true, it is the absolute same thing as pressing the Space-key, without ever pressing the Space-key. It doesn't matter where you have
press = true;
it will be the same thing as having space pressed.
This question doesn't really seem answered. What if the poster wanted to call the left or right keys virtually? Using a boolean function just because the space bar happens to be a boolean input doesn't seem to be the answer the poster is looking for.
I think I can use System.Windows.Forms.Send$$anonymous$$eys.SendWait("{}"); for getting space key press event without pressing space key. But how can I do that inside unity?
Answer by Chom1czek · Oct 04, 2015 at 02:53 PM
I have discovered it recently but maybe you would like to check the "event system". Basicly you can check the current event in OnGUI method like this:
void OnGUI()
{
EventListener(Event.current);
}
void EventListener(Event e)
{
if(e.rawType == EventType.mousedown && e.button == 0) Debug.Log("Left click event handled");
}
PS OnGUI() must be implemented to get Event.current!
This check for current events, correct?
OP wanted a way to emulate the keyboard and fire off a specific 'keyboard event' without the use of a keyboard.
Ohh I see but now it doesn't make any much more sense to me :D Thanks and my bad.