Register keyboard events.

I want to a GUI event to call a keyboard event. In other words a OnGUI event to register a keyboard event, without the keyboard being used of course:), can this be done?

Hi inno,

i hope to understand your question correct. Here the code in c#.

void OnGUI(){
   // Gui Event
   if(GUI.Button(new Rect(10,10,100,20),"Click Me")){
       DoSomeStuff();
   }
}

void FixedUpdate(){
   //Keyboard Event
   if(Input.GetButton("up")){
      DoSomeStuff();
   }
}

void DoSomeStuff(){
   // Here i can do my stuff
}

Thanks for the reply. Actually I want something closer to:

void OnGUI(){
// Gui Event
if(GUI.Button(new Rect(10,10,100,20),“Click Me”)){
//Behave as if I clicked the keyboard key uparrow.
}
}

In other words instead of using the arrow keys to move a character I use the gui buttons. So when I click the GUI button it would be the same as clicking the up arrow. Imagine as if I was using the virtual keyboard of Windows.

What LDiederich is suggesting is that you can achieve what we perceive to be your desired results without mucking with tricking the system into thinking a keyboard event has been fired. If what you’re trying to do is have both the up button and the up arrow key behave identically, it’s more desirable to abstract the key’s behavior to a separate method, and redirect both button presses and key presses to that function. So, rather than having your input map look like this:

button
  |      ___ some behavior
  |     /
keypress

it would look like this:

button
        \___ some behavior
        /
keypress

If you want some other specific behavior (ie, pressing a GUI button labeled “L” causes an “L” to be typed in some text field), you’d have to put that behavior into the button and then call your desired behavior method. I hope that helps. Let us know if that’s the end result you’re looking for or if you need something more specific.

Thanks for the reply. The problem is that what you are suggesting was what I thought of doing. But due to custom code and the way the project was built from the beginning it would require a complete rewrite of the base code. Not to mention the fact that some scripts are calling others scripts, disabling components etc. So that is why I thought of fooling the keyboard.