You are here:

C/program to on caps lock

Advertisement


Question
please help with the program that will help to on caps lock, num lock and scroll lock.please!!

Answer
If this is a Windows machine, then the following will work (this particular example is num lock, but the code will work for others, too, just with different key codes).  Also, please note: this particular program is really rather annoying... it loops infinitely, toggling num lock on and off repeatedly... :)

#include <Windows.h>

BOOL SetNumLock( BOOL bState )
{
   BYTE keyState[256];

   GetKeyboardState((LPBYTE)&keyState);
   if( (bState && !(keyState[VK_NUMLOCK] & 1)) ||
       (!bState && (keyState[VK_NUMLOCK] & 1)) )
   {
       // Simulate a key press
       keybd_event( VK_NUMLOCK,
           0x45,
           KEYEVENTF_EXTENDEDKEY | 0,
           0 );

       // Simulate a key release
       keybd_event( VK_NUMLOCK,
           0x45,
           KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
           0);
   }

   return (keyState[VK_NUMLOCK]);
}

void main()
{
   BOOL set = TRUE;
   while(1)
       set = !SetNumLock(set);
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.