Detecting Arrow key pressed in InputField (For an controls menu)

Hey all!

Currently working on a menu system for a game, and there needs to be a way to change how movement is done. I have the movement keys already being saved to file for use. I just need a way to detect that an arrow key is being pressed in an input field to change the ‘movement’ key in the file. I dont need help saving the controls, just detecting an arrow key (or special key) is being pressed, because it seems the only keys input field accept is characters, numbers, or special characters. Any ideas?

Add this script to any InputField that needs to check for special characters
Add more If Statements for all the characters you care about.
Sadly there is no easy way to just ask for the keyPressed with Unity’s Input System. so you can’t just call a function or a switch statement. I think your stuck with a long list of Ifs

using UnityEngine;
using Unity.UI;

public SpecialCharacterInput : Monobehaviour
{
   InputField  myField;
   
   void Awake()
   {
      myField = GetComponent<InputField>():
    }

    void Update()
    {
         if (Input.GetKeyDown(KeyCode.LeftArrow)
         {
              myField.text = "Left Arrow";
         }
   }
}

Then your code that sets the KeyBinding can just check for those special strings

1 Like

Oh man, I’ve just spent 30 minutes researching this myself and the answer seem to be, burn a nearly pointless update call every tick.

you can always wrap all those ifs in a Unity - Scripting API: Input.anyKeyDown so when nothing is pressed no additional processing is done needlessly etc. it’s fairly low impact so performance really shouldn’t be that affected.

The answer is even more embarrassing. In trying to solve this with the UnityUI API, I kind of forgot I’d already rolled my own global input event system to handle exactly these cases.

I’ve got a radical idea, how about I use the code I’ve actually already written…