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

Thread: Check Caps Lock and Num Lock

  1. #1
    Code Warrior Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana's Avatar
    Join Date
    Oct 2008
    Posts
    4,055
    Blog Entries
    6

    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 04:05 PM.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Check Caps Lock and Num Lock

    Very nice, +rep!

  3. #3
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  4. #4
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,182
    Blog Entries
    12

    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.

  5. #5
    Code Warrior Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana's Avatar
    Join Date
    Oct 2008
    Posts
    4,055
    Blog Entries
    6

    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

  6. #6
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Check Caps Lock and Num Lock

    I am voting the original will be less confusing.

    Posted via CodeCall Mobile

  7. #7
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  8. #8
    Guru MathX has a spectacular aura about MathX has a spectacular aura about MathX's Avatar
    Join Date
    Oct 2008
    Location
    Kosovo
    Age
    19
    Posts
    4,006

    Re: Check Caps Lock and Num Lock

    +rep.......

  9. #9
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    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

  10. #10
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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