+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Check Caps Lock and Num Lock

  1. #1
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Check Caps Lock and Num Lock

    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:
    Code:
    bool capslock, numlock;
    This declares two booleen values (true or false), which we will set.

    Code:
    capslock = Console.CapsLock;
    numlock = Console.NumberLock;
    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:
    if (capslock == true)
    {
    Console.WriteLine("Caps Lock is On!");
    }
    else
    {
    Console.WriteLine("Caps Lock is Off!");
    }
    This code tests the value of capslock, if it is true (meaning the caps lock button is on), it prints "Caps Lock is On!"
    Otherwise it prints "Caps Lock is Off" (meaning capslock is false, and the caps lock button is off).

    Code:
    if (numlock == true)
    {
    Console.WriteLine("Number Lock is On!");
    }
    else
    {
    Console.WriteLine("Number Lock is Off!");
    }
    This code does exactly the same as the caps lock only, for numlock.
    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Check Caps Lock and Num Lock

    Very nice, +rep!

  4. #3
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Check Caps Lock and Num Lock

    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:

    Code:
    Console.WriteLine("Number Lock is " + (Console.NumberLock ? "On!" : "Off!"));
    Console.WriteLine("Caps Lock is " + (Console.CapsLock ? "On!" : "Off!"));
    Tada!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  5. #4
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Check Caps Lock and Num Lock

    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.

  6. #5
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: Check Caps Lock and Num Lock

    lol thank you amr and jordan
    santa - which one do you think will be less confusing to a beginner?
    Posted via CodeCall Mobile

  7. #6
    Jordan Guest

    Re: Check Caps Lock and Num Lock

    I am voting the original will be less confusing.

    Posted via CodeCall Mobile

  8. #7
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Check Caps Lock and Num Lock

    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.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  9. #8
    Join Date
    Oct 2008
    Location
    Istog, Kosova
    Posts
    4,001
    Blog Entries
    1
    Rep Power
    40

    Re: Check Caps Lock and Num Lock

    +rep.......
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  10. #9
    Jordan Guest

    Re: Check Caps Lock and Num Lock

    Quote Originally Posted by Santa Claus View Post
    not some disgusting slop that Termana calls a "tutorial". Shocking.
    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

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Check Caps Lock and Num Lock

    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.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to lock the keyboard in C#?
    By Gabriel_Munits in forum C# Programming
    Replies: 1
    Last Post: 11-05-2011, 06:58 AM
  2. Controlling caps/num/scoll lock LEDs
    By ThunderGraduate in forum General Programming
    Replies: 2
    Last Post: 11-14-2010, 10:41 PM
  3. lock c++
    By h4x in forum C and C++
    Replies: 5
    Last Post: 09-11-2009, 06:33 AM
  4. lock XP
    By abu_rakan in forum C# Programming
    Replies: 3
    Last Post: 12-30-2008, 08:13 PM
  5. lock and pay system
    By nishbur in forum Software Development Tools
    Replies: 3
    Last Post: 07-01-2008, 12:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts