Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / MFC

Toggling the Num Lock, Caps Lock, and Scroll Lock keys

Rate me:
Please Sign up or sign in to vote.
4.84/5 (23 votes)
24 May 2002CPOL3 min read 252.4K   7K   65  
How to toggle the Num Lock, Caps Lock, and Scroll Lock keys programmatically
There are two more functions that can toggle keyboard state but each of them has some specific limitations like SendInput does. In this article, you will find all possible variants with their descriptions and limitations and a simple solution for toggling these keys avoiding compatibility problems.
// MfcxStatusBar.cpp : implementation file
// Copyright(c) Armen Hakobyan 2002
// mailto:armenh@cit.am

#include "stdafx.h"
#include "StatusBarEx.h"
#include <WinAble.h>

// CStatusBarEx
#define ID_TIMER_TIME		0x0001L

IMPLEMENT_DYNAMIC(CStatusBarEx, CStatusBar)
CStatusBarEx::CStatusBarEx()
{
}

CStatusBarEx::~CStatusBarEx()
{
}

BEGIN_MESSAGE_MAP(CStatusBarEx, CStatusBar)
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnNMDblclk)	
END_MESSAGE_MAP()

// CStatusBarEx message handlers

void CStatusBarEx::OnNMDblclk(NMHDR* pNMHDR, LRESULT *pResult)
{
	NMMOUSE* pNMMOUSE = (NMMOUSE*)pNMHDR;
	INT nInd = GetItemID((int)pNMMOUSE->dwItemSpec);

	if((nInd >= ID_INDICATOR_CAPS) && 
	   (nInd <= ID_INDICATOR_OVR))
	{		
		UINT nVirtKey = 0;
		switch(nInd)
		{
		case ID_INDICATOR_CAPS: nVirtKey = VK_CAPITAL; break;
		case ID_INDICATOR_NUM:  nVirtKey = VK_NUMLOCK; break;
		case ID_INDICATOR_SCRL: nVirtKey = VK_SCROLL;  break;
		case ID_INDICATOR_OVR:  nVirtKey = VK_INSERT;  break;
		}
		
		ASSERT(nVirtKey >= 1 && nVirtKey<= 254);		
	
		DWORD dwVersion = ::GetVersion();

		// Windows NT/2000/XP: The keybd_event function can toggle the 
		// NUM LOCK, CAPS LOCK, and SCROLL LOCK keys.

		if( dwVersion < 0x80000000) // Win NT
		{			
			::keybd_event( nVirtKey, 0x45, KEYEVENTF_EXTENDEDKEY, 0 );
			::keybd_event( nVirtKey, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
		}
		else
		{
			// Windows 95/98/Me: The keybd_event function can toggle only the 
			// CAPS LOCK and SCROLL LOCK keys. It cannot toggle the NUM LOCK key.
			// We can do this with SendInput but it requires 
			// Windows NT 4.0 SP3 and later or Windows 98 and later.

			DWORD dwMin = (DWORD)(HIBYTE(LOWORD(dwVersion)));
			if(dwMin >= 10) // Windows 98 and later
			{				
				INPUT input[2];
				::ZeroMemory(input, sizeof(input));		
				input[0].type = input[1].type = INPUT_KEYBOARD;
				input[0].ki.wVk  = input[1].ki.wVk = nVirtKey;		
				input[1].ki.dwFlags = KEYEVENTF_KEYUP;  // THIS IS IMPORTANT
				::SendInput(2, input, sizeof(INPUT));			
			}
			else // Windows  95
			{
				// Because the SetKeyboardState function alters the input state 
				// of the calling thread and not the global input state of the system, 
				// an application cannot use SetKeyboardState to set the 
				// NUM LOCK, CAPS LOCK, or SCROLL LOCK (or the Japanese KANA) indicator lights 
				// on the keyboard.

				BYTE byKeybState[256];
				::GetKeyboardState(byKeybState);		 
				byKeybState[nVirtKey] = !(BOOL)::GetKeyState(nVirtKey);
				::SetKeyboardState(byKeybState);
			}
		}
		::Beep(500, 100);
	}
	else
	{
		CWnd* pWnd = GetParent();
		if(pWnd != NULL)
		{			 
			//TODO: SendNotifyMessage; 
		}
	}

	*pResult = 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) SafeNet Inc
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions