In this tutorial I will show you how you can check weather Caps Lock or Num Lock is on. (I am using Visual C# Express Edition)
1. Start a new project by going to File -> New Project -> Console Application -> OK
2. Go into the main function and type:
This declares two booleen values (true or false), which we will set.Code:bool capslock, numlock;
Console.CapsLock, returns a booleen value (true or false) to tell weather the caps lock is on or off, (true being on, false being off), and Console.NumberLock also returns a booleen value to see weather NumLock is on or off (again true being on, false being off)Code:capslock = Console.CapsLock; numlock = Console.NumberLock;
This code tests the value of capslock, if it is true (meaning the caps lock button is on), it prints "Caps Lock is On!"Code:if (capslock == true) { Console.WriteLine("Caps Lock is On!"); } else { Console.WriteLine("Caps Lock is Off!"); }
Otherwise it prints "Caps Lock is Off" (meaning capslock is false, and the caps lock button is off).
This code does exactly the same as the caps lock only, for numlock.Code:if (numlock == true) { Console.WriteLine("Number Lock is On!"); } else { Console.WriteLine("Number Lock is Off!"); }
You can now start the application and fiddle with the NumLock and CapsLock buttons on your keyboard to see that it works!


LinkBack URL
About LinkBacks





Reply With Quote








Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum