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!
Last edited by Termana; 12-19-2008 at 02:05 PM.
Very nice, +rep!
Your code is very long-winded and inefficient. For a start, you don't need " = true" on the condition because the whole statement evaluates as a boolean anyway. Secondly, you are printing two things based on a condition. You don't need separate booleans to store the values.
Here is my version of your entire program in two lines:
Tada!Code:Console.WriteLine("Number Lock is " + (Console.NumberLock ? "On!" : "Off!")); Console.WriteLine("Caps Lock is " + (Console.CapsLock ? "On!" : "Off!"));
good work termana, ignore santa he spends his life time with green elfs and high dnd children who sit on his lap ane wet his pants..any one would be crazy.
lol thank you amr and jordan
santa - which one do you think will be less confusing to a beginner?
Posted via CodeCall Mobile
I am voting the original will be less confusing.
Posted via CodeCall Mobile
It is not a program's job to be easy to understand. I cannot accept responsibility for another's incompetence.
If the user is confused, then it's bad. We programmers need to write good code, not some disgusting slop that Termana calls a "tutorial". Shocking.
+rep.......![]()
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
Wow, are you taking up a new position as the forum insulter? There is nothing wrong with the code Termana originally wrote. If anything, it is more clear what is done than using a ternary operation not to mention reusable later in code. Where you will need to type your entire ternary operation out each time you want to test that value he must simply call a variable.
-rep
Eh? You are talking nonsense. If I want to test that value, I need only refer to Console.CapsLock, instead of his custom variable, as both are boolean values. And my code is more concise, better than some n00bisb code lawl.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks