- Home /
How to get the keyboard/OS capsLock state?
When running the sample provided at https://docs.unity3d.com/ScriptReference/Event-capsLock.html it appears that this accessor only returns true, when the capsLock key is currently depressed. Obviously, I can keep track of this myself, but I still need the starting on/off state.
Is there any way to get the actual capsLock state of the keyboard/OS? Meaning:
get a True value if the capsLock indicator for the keyboard is ON.
get a False value, if it's OFF.
Answer by cooldude5757 · Feb 07, 2018 at 07:25 AM
As far as I know, I dont think that is possible natively, but here is a hacky way of doing it, assuming the user atleast presses one button, you can do this:
void OnGUI()
{
Event e = Event.current;
//below I am checking if a key is pressed,
//then if the key is a character
//and then remove the shift causing inversion of problem
if (e.isKey & e.character != char.MinValue &!e.shift)
{
string UpperChar = e.character.ToString().ToUpper();
if (UpperChar == e.character.ToString())
{
Debug.Log("Capslock on");
}
else
{
Debug.Log("Capslock off");
}
}
}
So, if the shift key is down, and I get a capital letter, shift-lock is not active. If the shift key is down and I get a lowercase letter, shift lock IS active. Does that actually work? The caplock state does not affect e.shift', but DOES affect the character
e.character`? If so, I think that will work. Testing it out now. Thank you.
Edit: tested great! since I really only need to know the capslock state when a Letter is pressed, this should work fine. (I had been hoping to NOT rely on Events, and stick to just using the Input class. And indeed, some ti$$anonymous$$g issues between the two made a workaround needed to get them to play together, but it's working now.) Thanks, dude
It works, you might have to modify the code slightly but you get the concept of it. We are checking if capslock is on by comparing it to uppercase version.
Adding shift handling is just making it foolproof just in case the user clicks shift in the beginning.
Answer by neosca · Feb 07, 2018 at 10:27 AM
You can use Windows form or windows input to check states of Capslock, numlock or scrolllock:
using System.Windows.Input;
using System.Windows.Forms;
bool capsLockState = Control.IsKeyLocked (Keys.CapsLock);
Definitely a more elegant solution, but remember to include WindowsBase.dll in the plugins folder or else it wont recognize the namespace.
plus1 for a valid solution. Alas, this only will work for windows builds; I was hoping to find a more platform-agnostic solution, but I'll code each platform individually if I need to. Any idea how to do something like this on $$anonymous$$ac or Andriod or any of the other platforms Unity supports? Thank you!
Hi @neosca. I have null exception on any atempts to use system.windows.forms It realy work, and i do something wrong? All needed dll stored in plugins folder. How correct use it? Tnx.
Your answer
Follow this Question
Related Questions
Add Mobile Android and iOS Emojis 1 Answer
No keyboard events detected after any key is held. 0 Answers
Need help with disabiling keyboard inputs. 1 Answer
LeftControl key at runtime 0 Answers
Add keyboard input in ClickToMove 0 Answers