You are here:

C++/case sensitive problems



I am Eddie, another expert here in C++. Another expert and I couldn't figure this stumper out. In a windows application, if you switch on the wParam in the WM_KEYDOWN message, it gives you the key pressed. Assuming the key is a letter, you can't use the lower case of the key to perform actions:

case 'a':
// does not work

case 'A':
// works for both upper and lower case

I was wondering why this is. I also noticed it works the same way with the function GetAsyncKeyState(). I tried passing 97 as the param for lower case 'a' and it wouldn't register. Any suggestions would by highly appreciated.

Using Microsoft Visual C++ is that makes a difference.

Thanks a lot,

- Eddie

The reason is (if you read the documentation carefully) is that you are dealing with MS Windows virtual key codes and _not_ character codes.

A virtual key code represents a key on the keyboard and _not_ the character it would produce under current keyboard conditions (caps lock, shift, ctrl, alt, alt gr etc.). As standard US/UK and probably standard keyboards for many other locales do not have keys for lower case letters (the letter keys are generally marked using upper case letters). They have no virtual key code.

Under the virtual key code scheme the ASCII code for 'a' (0x61) represents the numeric keypad 1 key.

I suggest you go back to the MSDN library and read up on virtual key codes - the online (at ) and CD/DVD editions should contain a link from the GetAsyncKeyState documentation - which I presume you have looked at already

If you do not need to track individual key presses and releases then I suggest you use a handler for WM_CHAR instead (or if you are using Windows XP and need it WM_UNICHAR) - assuming of course that your message loop uses TranslateMessage as is usual.

If you really wish to use the WM_KEYDOWN and WM_KEYUP messages to track key presses and releases you have to map them to characters yourself - and this means tracking the state of the modifier keys alt, control and of course shift.

For example with an implementation of the IPropertyPage COM interface you provide keystroke message handling via its TranslateAccelerator method, as shown below from an ATL implementation of IPropertyPage I wrote some time ago, with a changed class name (note use a mono-spaced font to view, also posting through AllExperts may have trashed the indentation - sorry if this is so!):

STDMETHODIMP CMyPropertyPage::TranslateAccelerator(MSG *pMsg)
 BOOL bHandled(FALSE);
 int iKeyMods = GetKeyModifiers();

 switch ( pMsg->wParam )
 case VK_ESCAPE:
   hr = OnKeyEscape( pMsg, iKeyMods, bHandled );

 case VK_RETURN:
   hr = OnKeyReturn( pMsg, iKeyMods, bHandled );

 case VK_TAB:
   hr = OnKeyTab( pMsg, iKeyMods, bHandled );

 case VK_UP:
   hr = OnKeyUp( pMsg, iKeyMods, bHandled );

 case VK_DOWN:
   hr = OnKeyDown( pMsg, iKeyMods, bHandled );

 case VK_SPACE:
   hr = OnKeySpace( pMsg, iKeyMods, bHandled );

 case VK_DELETE:
   hr = OnKeyDelete( pMsg, iKeyMods, bHandled );

 case VK_INSERT:
   hr = OnKeyInsert( pMsg, iKeyMods, bHandled );


 if ( !bHandled )
     hr =
 return hr;

Where GetKeyModifiers and the OnKey... functions are other member functions of the property page class.

GetKeyModifiers obtains the key state information from the Win32 function GetKeyState for the current modifier keys (control, shift and alt) and packs this information into three bits of an int (using enum names for the bit values).

Here is a part of the OnKeyTab implementation:

HRESULT CMyPropertyPage::OnKeyTab
( MSG *pMsg
, int iKeyMods
, BOOL & bHandled

 bool isShift = iKeyMods == KEY_MOD_SHIFT; // note shift key state

 if (iKeyMods!=0 && !isShift) // only interested in TAB or SHIFT+TAB
     return hr;

 if ( IsKeyDown(pMsg) )
   {// only interested in key down events


Again IsKeyDown is one of my class' helper member functions. It returns true for a WM_KEYDOWN message (ergo it returns false for WM_KEYUP messages).

The reason I showed you this code is to demonstrate the sort of additional flexibility that comes with processing the WM_KEYUP and WM_KEYDOWN messages - and of course the flip side of this flexibility - the added responsibility and complexity!  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.