Unable to trace CapsLock toggle on and off (28152)

I tried three ways:

1.)

    function OnGUI() {
var e : Event = Event.current;
Debug.Log(e.capsLock) ;
}

2.)

function Update(){

if (Input.GetKey(KeyCode.CapsLock)) {
	Debug.Log("true");
}
else
{
	Debug.Log("false");
}
}

3.)

import System;

function Update () {
 print(Console.CapsLock);
}

The first two give me true on when CappsLock is CONSTANTLY PRESSED regardless of it is on or off.
The third gives false always.

All three give false even if CapsLock is on. Why?
I have win7 x64 .NET 2.0.

2 Answers

2

You can solve this as following:

using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern short GetKeyState(int keyCode);

bool isCapsLockOn = false;
void Start()
{
    isCapsLockOn = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;//init stat
}

void Update()
{
    if(Input.GetKeyDown(KeyCode.CapsLock))
    {
        isCapsLockOn  = !isCapsLockOn ;
        //......
    }
}

Have you tried using a boolean value that’s beeing toggled each time caps lock is pressed? (sry for code in c# I’m clueless when it comes to java:P )

    private bool _capsToggled = false;

public void Update() {
    if( Input.GetKey(KeyCode.CapsLock) ) {
         _capsToggled = !_capsToggled;   //Toggle the boolean
         Debug.Log( "Caps Lock toggled: " + _capsToggled.ToString() );
    }

}

//Then of course when you want to perform whatever it is you want to do when caps lock is toggled you could use

if( _capsToggled )
     //Do this if caps is toggled
else
     //Do this if not

I get where you're getting with that, but how do we know that the initial state is false? For my example I wanna use it for hidden password warning to the end-user by the program in a form: "Beware, your Caps is on!". How does the program know the initial state of the CapsLock so that it knows the alternate in your paradigm? The import System; function Update () { print(Console.CapsLock); } should do exactly that job, but in my case for some reason is not working.