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?

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();

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!