Go Back   CodeCall Programming Forum > Software Development > Tutorials > CSharp Tutorials
Register Blogs Search Today's Posts Mark Forums Read

CSharp Tutorials Tutorials for C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-19-2008, 08:20 AM
Termana's Avatar
Code Warrior
 
Join Date: Oct 2008
Posts: 4,055
Termana is a name known to allTermana is a name known to allTermana is a name known to allTermana is a name known to allTermana is a name known to allTermana is a name known to all
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 05:05 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-19-2008, 10:57 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Check Caps Lock and Num Lock

Very nice, +rep!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-19-2008, 11:08 AM
Xav's Avatar
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-19-2008, 07:40 PM
amrosama's Avatar
Code Warrior
/////////|||||\\\\\\\\\
 
Join Date: Aug 2007
Location: Pyramids st, Giza, Egypt
Age: 21
Posts: 8,112
amrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to behold
Send a message via MSN to amrosama
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-19-2008, 07:50 PM
Termana's Avatar
Code Warrior
 
Join Date: Oct 2008
Posts: 4,055
Termana is a name known to allTermana is a name known to allTermana is a name known to allTermana is a name known to allTermana is a name known to allTermana is a name known to all
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 12-19-2008, 09:25 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Check Caps Lock and Num Lock

I am voting the original will be less confusing.

Posted via CodeCall Mobile
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-20-2008, 02:53 PM
Xav's Avatar
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-22-2008, 08:58 AM
MathX's Avatar
Guru
 
Join Date: Oct 2008
Location: Kosovo
Age: 19
Posts: 3,994
MathX has a spectacular aura aboutMathX has a spectacular aura about
Send a message via MSN to MathX
Re: Check Caps Lock and Num Lock

+rep.......
__________________
My Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 12-22-2008, 09:17 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 12-22-2008, 01:57 PM
Xav's Avatar
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -5. The time now is 09:29 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0