- Home /
How do you say if("Capslock") is on?
in terms of the character controller for an fps i want the caps lock to be an auto run function. shift is already a run button so i will just copy that but what do i put instead of...
//Run
if (Input.GetButton ("Shift")) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= RunSpeed;
}
it says that in the script reference, click on the first enumerations word, then keyCode, and there's a whole list of possible keyboard inputs.
Answer by Scribe · Dec 06, 2010 at 08:40 PM
If you want to ask if they press the capslock key then you do something you use:
if (Input.GetKey(KeyCode.CapsLock))
but if you want to check whether capslock is switched on or not you should use:
Event.capsLock (this returns true when capslock is switched on)
Is this a direct state check or a key event check? I'm used to checking key events for states and checking states after an UpdateState routine.
file:///Applications/Unity/Unity.app/Contents/Documentation/Documentation/ScriptReference/Event-capsLock.html this is the page about the event, I think it is a key event check but i'm not certain sorry
Answer by GlitchEnzo2 · Dec 06, 2010 at 07:38 PM
You want to use KeyCode.CapsLock
if (Input.GetKey(KeyCode.CapsLock)) {
// put code here
}
Answer by BitMiller · Jan 17, 2011 at 10:41 AM
Scribe and GlitchEnzo, yet my reputation isn't enough to vote you both down.
KeyCode.CapsLock and Event.capsLock only returns True if the key is pressed, not returning the state of the lock. There would be a way with importing System.Console namespace with the CapsLock function but Unity doesn't seem to import it... Must be some kind of a security issue. I also could take a use of it but couldn't find an answer yet.
http://msdn.microsoft.com/en-us/library/system.console.capslock.aspx
import System;
function Start () { print(Console.CapsLock) }
says: Unknown identifier: 'CapsLock'.
Of course I've also tried import System.Console; print(CapsLock);
I'm with you Bit$$anonymous$$iller cant actually query if the Capslock is engaged.
Answer by darvinda · Oct 06, 2015 at 11:35 AM
System.Console.CapsLock only available when you are using the full mono, not .Net 2.0 Subset.
To change: Edit/Project Settings/Player,
Other Settings/Optimization/API Compatibility Level -> .Net 2.0
After that System.Console.CapsLock will be available.
BUT! Mono seems to be bugged, and this variable always return false.
Actually there are two ways to get Caps Lock info:
Copy UnityInstallDir\Editor\Data\Mono\lib\mono\2.0\System.Windows.Forms.dll to your Assets/Plugins folder. After that you can query Caps Lock state by the following code:
System.Windows.Forms.Control.IsKeyLocked(System.Windows.Forms.Keys.CapsLock) or System.Windows.Forms.Control.IsKeyLocked(System.Windows.Forms.Keys.Capital)
Using WinAPI: Define an external function in a class:
[DllImport("USER32.dll")] public static extern short GetKeyState(int nVirtKey);
and query it:
(GetKeyState(0x14) & 1)>0) // 0x14 = VK_CAPITAL (Caps Lock)
return value:
If the high-order bit is 1, the key is down; otherwise, it is up.
If the low-order bit is 1, the key is toggled.