Author Topic: Getting keyboard input events  (Read 4110 times)

Holy Manfred

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 8
  • Posts: 71
    • View Profile
Getting keyboard input events
« on: December 28, 2013, 12:13:41 PM »
I'd like to check for certain keys being pressed using the NGUI event system if this is possible. I have a game object which is handling all kinds of UI stuff. It is set up with
  1. UICamera.genericEventHandler = gameObject;
to receive all events happening. That works well for click, double-click, scroll etc. but I am having problems getting keyboard input. My OnKey function looks like this:
  1. void OnKey(KeyCode key)
  2.         {
  3.                 Debug.Log("OnKeyCode " + key);
  4.         }
and I would expect it to get all kinds of keys pressed on the keyboard. However currently I only get certain inputs like 'UpArrow' 'RightArrow', etc. However I would like to be able to get any key or key combination being pressed so I can react to things like ESCAPE, SHIFT-S, CTRL-P, etc.

Of course I can do this the hacky way with Input.GetKeyDown... but I'd really like to use the NGUI events. I am sure there must be some script or component where I can customize what keys will trigger what events.
Using UIKeyBinding is not an option for me because I need to react to a lot of global things happening which might not be related to specific objects in the world.
Any suggestions on how to achieve this would be welcome.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Getting keyboard input events
« Reply #1 on: December 28, 2013, 04:20:16 PM »
NGUI doesn't listen to all keys, just specific ones. If you want to listen to all keys, just do it in your own Update() function and check UICamera.selectedObject == gameObject. If not, exit early.