Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

A couple of KeyDown problems.

Discussion in 'UGUI & TextMesh Pro' started by Jesper-Nielsen, Feb 21, 2017.

  1. Jesper-Nielsen

    Jesper-Nielsen

    Joined:
    Nov 14, 2013
    Posts:
    95
    Up until now I've been using the good old Input.GetKeyDown() to handle keyboard events during game play. It works well since the only key I've implemented logic for is escape...
    But this doesn't really scale well... I want to provide several input keys and also implement custom key binding for all of these.
    To avoid a long hardcoded chain of if/else if I've been playing with Event.Current in OnGUI() but I still do have a couple of problems:
    1: Character keys seem to send KeyDown twice? The first time it's sent without the character, the second time with. Does anyone know why?
    2: How do I check if an InputField or other UI element that must consume the event is focused? If this is the case I want to ignore the event if it's relevant for the control (typed character, enter, backspace, typed tab or tab navigation etc.) - perhaps even ignore everything but "escape".
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    You really aren't supposed to use Event.Current, that's not really what it's designed for.

    If managing Input.Get is too cumbersome for you, then look at assets like InControl that manage it for you.
     
  3. Jesper-Nielsen

    Jesper-Nielsen

    Joined:
    Nov 14, 2013
    Posts:
    95
    You mean Event.Current is really not designed for being used? Or it's designed for being used in another way?

    Anyway - even if I do opt for asking "did the user press 'A'? Did the user press 'B'? etc. etc." (which is an abomination IMHO and not something any professional programmer would ever put into production code) the problem still stands:

    How do I make sure I don't open the quest log if the user types "q" and the chat input - or any other text input - is focused? Is there any way I can register an event listener on the canvas itself that only receives events if they are not used/handled by child objects? Eventtrigger (or big invisible button at far clip plane etc.) should work nicely for mouse events etc. but apparently not KeyDown?