Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
  • Photo
      11 Aug
    Error

    Should I be practicing programming every day? I feel if I don't, I'll get instantly rusty or something.

    Show comments (4)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Check Caps Lock and Num Lock


  • Please log in to reply
16 replies to this topic

#1 Termana

Termana

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 971 posts

Posted 19 December 2008 - 05:20 AM

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

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)

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).

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!

Edited by Termana, 19 December 2008 - 02:05 PM.

  • 2

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 19 December 2008 - 07:57 AM

Very nice, +rep!
  • 0

#3 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 19 December 2008 - 08:08 AM

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:

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

Tada!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2,764 posts

Posted 19 December 2008 - 04:40 PM

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.
  • 0

#5 Termana

Termana

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 971 posts

Posted 19 December 2008 - 04:50 PM

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

#6 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 19 December 2008 - 06:25 PM

I am voting the original will be less confusing.

Posted via CodeCall Mobile
  • 0

#7 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 20 December 2008 - 11:53 AM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 MathX

MathX

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,034 posts
  • Programming Language:Java
  • Learning:C, Java, C++, PHP, JavaScript, PL/SQL

Posted 22 December 2008 - 05:58 AM

+rep.......:D
  • 0

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


#9 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 22 December 2008 - 06:17 AM

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
  • 0

#10 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 22 December 2008 - 10:57 AM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#11 cismarel

cismarel

    CC Newcomer

  • Just Joined
  • PipPip
  • 24 posts

Posted 02 June 2009 - 12:25 AM

But if i want check the caps and num lock in a windows form application?
  • 0

#12 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,559 posts

Posted 02 June 2009 - 05:42 AM

So if thats the code.. it is possible to change the value?

Console.CapsLock = false;

is this value a copy of the value or does it really change if caps lock is on?
  • 0