Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if Caps Lock and Num Lock is on or off through Console in C#
The C# Console class provides built-in properties to check the status of the Caps Lock and Num Lock keys. The Console.CapsLock and Console.NumberLock properties return a boolean value indicating whether these keys are currently active.
Syntax
Following is the syntax to check Caps Lock status −
bool capsLockStatus = Console.CapsLock;
Following is the syntax to check Num Lock status −
bool numLockStatus = Console.NumberLock;
Return Value
Console.CapsLockreturnstrueif Caps Lock is on,falseotherwise.Console.NumberLockreturnstrueif Num Lock is on,falseotherwise.
Checking Caps Lock Status
Example
using System;
public class Demo {
public static void Main(string[] args) {
Console.WriteLine("The CAPS LOCK is on or not? " + Console.CapsLock);
}
}
The output of the above code is −
The CAPS LOCK is on or not? False
Checking Num Lock Status
Example
using System;
public class Demo {
public static void Main(string[] args) {
Console.WriteLine("The Num LOCK is on or not? " + Console.NumberLock);
}
}
The output of the above code is −
The Num LOCK is on or not? True
Checking Both Keys Together
Example
using System;
public class Demo {
public static void Main(string[] args) {
bool capsLock = Console.CapsLock;
bool numLock = Console.NumberLock;
Console.WriteLine("Caps Lock Status: " + (capsLock ? "ON" : "OFF"));
Console.WriteLine("Num Lock Status: " + (numLock ? "ON" : "OFF"));
if (capsLock && numLock) {
Console.WriteLine("Both keys are active");
} else if (!capsLock && !numLock) {
Console.WriteLine("Both keys are inactive");
} else {
Console.WriteLine("Keys have different states");
}
}
}
The output of the above code is −
Caps Lock Status: OFF Num Lock Status: ON Keys have different states
Key Features
| Property | Description | Return Type |
|---|---|---|
Console.CapsLock |
Gets the status of the Caps Lock key | bool |
Console.NumberLock |
Gets the status of the Num Lock key | bool |
Conclusion
The Console.CapsLock and Console.NumberLock properties provide a simple way to check the current state of these toggle keys in C# console applications. These properties return boolean values that can be used for conditional logic or user feedback in your programs.
