Skip to content

Handling MFC Keyboard Messages With Shift & Toggle Key State

1. Overview of Keyboard Messages

In this example we will learn how to handle Keyboard Messages using MFC SDI Application. First, We will provide a handler function for the Keyboard. Then, inside the handler, we will check for Shift Key State and Toggle Key State.

We know that keyboard and mouse are basic hardware interface supported by almost all the Computer Operating System. When the user hits the keyboard key, the device driver for the keyboard deals with it by making use of the interrupts.

Windows OS knows about these kinds of hardware actions in the form of a Message Structures. The hardware fills these structures with base data and places that in the Operating System Message Queue. The Windows Operating System will take these messages and then adds more information into it. Finally, it places the Message in the process specific Message Queue. This queue is called Application Message Queue. When the user pushes a Keyboard Key, MFC gets a Keyboard Message from this Application Message Queue.

Note
Every Application has its own message queue. Some calls it as Application Message Queue and some calls it as Process Message Queue.

In MFC SDI, the application runs a thread which pools the Application Message Queue and dispatches the message to the corresponding window. We provide a handler for these messages if want to respond to it. We call these kinds of Messages as Windows Messages. When the message is from Keyboard, we call it as Keyboard Message.

2. Create MFC SDI Application

This Example is a simple SDI Application. To create the Application on your own follow these steps:

Step 1: Create a new project and chose MFC Application under the Visual C++ category. Then provide the project name as shown in the below screenshot:

Create MFC SDI Application to Handle Keyboard Messages
Create MFC SDI Application to Handle Keyboard Messages

Step 2: Click OK to the above shown dialog. This will open a wizard now. Accept the defaults for the pages except the one shown in the below screen:

Select MFC Application Type
Select MFC Application Type

The SDI Application is ready to handle different keyboard messages from the user input. Now, we will learn the Keyboard Message Handling.

3. WM_KEYDOWN & WM_KEYUP Keyboard Messages

When the user hits a key in the Keyboard, the target window will receive the below specified windows messages. The below picture shows the messages and its details:

MFC Keyboard Messages
MFC Keyboard Messages

 

From the above table, we can conclude that WM_KEYDOWN and WM_KEYUP will be fired for any Keyboard Keys. Therefore, windows fires this event for both System Keys and the Character Keys. Let us say you typed the alphabet letter ‘K’ in a notepad. The hardware interprets that as two events. One is KeyDown and the other one is KeyUp. So we get three messages WM_KEYDOWN, WM_KEYUP and WM_CHAR out of 5 as the typed keystroke is not a System Key.

There are Special Keys which we should know. In software world, we categorize these as Shift Keys and Toggle Keys. Examples for Shift Keys are Ctrl, Shift and Alt. The Toggle Key changes its Key-State in between ON & OFF. Best examples for Toggle Keys are Caps Lock, Num Lock & Scroll Lock.

Note
Shift Keys not only represents Shift. The alt and ctrl also called as Shift Keys.

We will use the example created in section 2 to learn how to handle the WM_KEYDOWN event. One can handle other events the same way.

4. Adding WM_KEYDOWN Message Handler

First, we need to provide the handler for the WM_KEYDOWN Message. First, we go to the class view and select properties for the class CKeyBoardInputView. Next, we will handle our keyboard message in the view class. After that, we provide the handler function for WM_KEYDOWN Windows Message. To do this, we have to click on the Message icon, then double click on the grid column for the WM_KEYDOWN. The below picture shows the steps:

Handle WM_KEYDOWN Keyboard Message in MFC Application Through Visual Studio IDE
Handle WM_KEYDOWN Keyboard Message in MFC Application Through Visual Studio IDE

The signature of the empty handler is shown below:

In this article, we will deal with the first parameter only. The first parameter represents the key code which you can compare with the keyboard constants to decide what keystroke user triggers.

5. Looking for Keyboard Key Status

There are different types of keys in Keyboard. Alphanumeric Keys, Shift Keys and Toggle Keys. In this section, we will explore how we can check for keyboard key press.

5.1 Alphanumeric Key Check

We can compare the alphanumeric keystroke directly with character constants. In the below code, we are looking for the English letter M or the Numeric digit 3.

5.2 System Key Check

We should compare System Keys with the predefined constant values living in the “ Winuser.h” header file. The below code in the Keyboard Message Handler shows how we ensure that the typed keystroke is either an F5 Key or an End Key. These two Keys are System Keys and it will not fall in the Alphabets or Numeric Keys.

5.3 Shift Key Check

Sometimes we will end up like we need to check the alphanumeric keystroke with the Shift Keys State like Ctrl, Shift and Alt. In the Message Handler will check for the Ctrl+A and the Shift+S combination of the Keys.

When converting the 8000 from hexadecimal to binary we get 16 digits with the high bit set to one and others zero (1000 0000 0000 0000). The GetKeyState function returns a negative number when the user holds down a Shift key. Remember, Shift key stands for all three Keys; Shift, Ctrl & Alt. So, in the Keyboard Message handler, we can either check the return value is negative or check the high-order bit with the Bit-wise AND Operator ‘&’ to determine the Shift Key State. The code snippet above uses the Bit-wise Operator to make the comparison.

5.4 Toggle Key Check

The Toggle Key like Insert, NumLock and CapsLock will change their state between ON and OFF. When user hits the Toggle Key, MFC inverts Toggle Key’s current state. In the previous section, we used the High-order bit of the nChar (First parameter of the event handler) to decide the state (Pressed down or not) of the Shift Key. One can use the low-order bit to check the Toggle Key state. When the low-order bit is set, we can conclude that the Toggle Key is turned ON. The below code informs you whenever the CAPSLOCK Key is turned ON. It also shows a message box when the NumLock Toggle Key State is OFF.

6. Summary

Here, we dealt with different kind of key strokes and handling them with Keyboard Message Handler. One can also try for the Shift+Alt, Shift+Ctrl & Ctrl+Alt combination as well.

Source Code: Download Keyboard Handling Example for Google Drive

Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: