Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Glurth · Feb 04, 2018 at 07:00 PM · inputkeyboardos

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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");
             }
         }
             
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Feb 07, 2018 at 06:15 PM 0
Share

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

avatar image cooldude5757 Glurth · Feb 07, 2018 at 06:21 PM 0
Share

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.

avatar image
3

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);
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cooldude5757 · Feb 07, 2018 at 10:48 AM 0
Share

Definitely a more elegant solution, but remember to include WindowsBase.dll in the plugins folder or else it wont recognize the namespace.

avatar image Glurth · Feb 07, 2018 at 05:59 PM 0
Share

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!

avatar image Someoneunknown · Feb 17, 2018 at 02:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges