Skip to content

Instantly share code, notes, and snippets.

@arsh5620
Last active October 21, 2020 02:51
Show Gist options
  • Save arsh5620/e36086559449d1b72e7807c1f86541d9 to your computer and use it in GitHub Desktop.
Save arsh5620/e36086559449d1b72e7807c1f86541d9 to your computer and use it in GitHub Desktop.
Unity CapsLock Quick Hack
using UnityEngine;
public class UnityCapsLockScript : MonoBehaviour
{
bool _isCapsLock = false;
void OnGUI()
{
// https://answers.unity.com/questions/1464150/how-to-get-the-keyboardos-capslock-state.html
// Original thanks to cooldude5757 on unity answers
IsCapsLockOn(Event.current);
if (Event.current.isKey & Event.current.character > 0)
{
Debug.Log($"Caps lock is {_isCapsLock}");
}
}
// Must be called from OnGUI
public void IsCapsLockOn(Event e)
{
if (e.isKey)
{
if (e.character >= 'A' && e.character <= 'Z')
{
if (e.shift)
{
_isCapsLock = false;
}
else
{
_isCapsLock = true;
}
}
else if (e.character >= 'a' && e.character <= 'z')
{
if (e.shift)
{
_isCapsLock = true;
}
else
{
_isCapsLock = false;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment