Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Capslock detection ?

Discussion in 'Scripting' started by ghostshot3, Jul 2, 2018.

  1. ghostshot3

    ghostshot3

    Joined:
    Apr 30, 2018
    Posts:
    3
    How to detect if my capslock is activate in keyboard using c# ?
     
  2. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
  3. ghostshot3

    ghostshot3

    Joined:
    Apr 30, 2018
    Posts:
    3
    Not working
     
  4. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    Hmmm... Try using the code suggested by quicktom here: https://answers.unity.com/questions/164795/unable-to-trace-capslock-on-and-off.html

    He wrote:
    Code (CSharp):
    1. using System.Runtime.InteropServices;
    2. [DllImport("user32.dll")]
    3. public static extern short GetKeyState(int keyCode);
    4. bool isCapsLockOn = false;
    5. void Start()
    6. {
    7.      isCapsLockOn = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;//init stat
    8. }
    9. void Update()
    10. {
    11.      if(Input.GetKeyDown(KeyCode.CapsLock))
    12.      {
    13.          isCapsLockOn  = !isCapsLockOn ;
    14.          //......
    15.      }
    16. }
    Personally, that code is out of my area of knowledge so I can't guarantee anything there.
    If that doesn't work, post the code that you used.
    I hope this fixes the problem!
     
  5. ghostshot3

    ghostshot3

    Joined:
    Apr 30, 2018
    Posts:
    3
    It worked thanks ,and yes i didnt understand this code too lol
     
  6. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    Lol. I'm glad that it worked.