Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

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

, 24 May 2002 CPOL
How to toggle the Num Lock, Caps Lock, and Scroll Lock keys programmatically.
togglekeys_demo.zip
ToggleKeys.aps
res
ToggleKeysDoc.ico
ToggleKeys.ico
Toolbar.bmp
ToggleKeys.dep
ToggleKeys.dsw
ToggleKeys.ncb
ToggleKeys.dsp
ToggleKeys.mak
ToggleKeys.plg
togglekeys_exe.zip
ToggleKeys.exe
togglekeys_src.zip
// 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)

Share

About the Author

Armen Hakobyan
Software Developer (Senior) SafeNet Inc
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150302.1 | Last Updated 25 May 2002
Article Copyright 2002 by Armen Hakobyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid