UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

architecture

[Input System] Resolve ArgumentOutOfRangeException on Keyboard Key.None Access

Solution

input systemdebuggingevent handlingtroubleshootinguser interaction

Unity 2019.1.x - Unity 6.3.x

Published 26 days ago

Issue

 An ArgumentOutOfRangeException is encountered when Keyboard.current is indexed using Key.None. This occurs because Key.None represents a logical placeholder for an unassigned state rather than a physical key index within the device's control array.

Quick-Fix

Directly indexing Keyboard.current with Key.None fails because it is an invalid control index for physical device polling.

Expand Analysis

To prevent the ArgumentOutOfRangeException, your script must implement a validation check to ensure a key binding is not Key.None before attempting to access Keyboard.current with that key. Because Key.None is not a valid physical key, it should be treated as a special unbound state requiring a conditional check.

  1. Verify if the Key variable is not Key.None using a standard inequality check.
  2. Ensure Keyboard.current is not null to account for hardware disconnects.
  3. Poll the specific KeyControl only if the previous conditions are met.

Additional Tips

  • Use Key.None as a sentinel value in your UI to indicate that a binding is currently cleared or unassigned.
  • For production-grade input management, utilize InputAction rebinding instead of direct device polling to avoid manual Key.None handling.

Copy


using UnityEngine;
using UnityEngine.InputSystem;

public class KeyBindingValidator : MonoBehaviour
{
    public Key userBinding = Key.None;

    private void Update()
    {
        // Ensure target key is not Key.None before accessing the device array
        if (userBinding != Key.None && Keyboard.current != null)
        {
            if (Keyboard.current[userBinding].wasPressedThisFrame)
            {
                Debug.Log("Valid input received.");
            }
        }
    }
}

Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.