Question

I want to preprocess the keyboard message in PretranslateMsg in MFC. So I write the following code.

 if( pMsg->message == WM_KEYUP )   
        {
            if( GetKeyState(VK_CONTROL) || GetKeyState(VK_SHIFT) || GetKeyState(VK_MENU) )
            {
                CString csKey = TranslateKeyToString( GetKeyState(VK_CONTROL) & 0x8000 ,GetKeyState(VK_SHIFT) & 0x8000, GetKeyState(VK_MENU)&0x8000, pMsg->wParam );
                DoWorkForAcceleratorKey(csKey);
                return TRUE;
            }
        }

With above code. I can successfull get shotcut in "csKey" for "Ctrl+Alt+1" and "Ctrl+1" but I can't Get "Alt+1", when I press Alt+1, the csKey is a strange single character. So what happens for the "Alt+1", And how to solve this problem? Thank you.

Was it helpful?

Solution

You don't get a WM_KEYUP message for the 1. With the Alt key, the entered characters are used in a different way.

Instead of a WM_KEYUP, you receive the WM_SYSKEYUP. The WM_KEYUP you receive should have the nVirtKey code of VK_MENU.

This is are the messages captured by Spy++ when I press ALT+1 in Notepad.

WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:0 fAltDown:1 fRepeat:0 fUp:0
WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:0 fAltDown:1 fRepeat:1 fUp:0
WM_SYSKEYDOWN nVirtKey:'1' cRepeat:1 ScanCode:02 fExtended:0 fAltDown:1 fRepeat:0 fUp:0
WM_SYSCHAR chCharCode:'49' (49) cRepeat:1 ScanCode:02 fExtended:0 fAltDown:1 fRepeat:0 fUp:0
WM_SYSKEYUP nVirtKey:'1' cRepeat:1 ScanCode:02 fExtended:0 fAltDown:1 fRepeat:1 fUp:1
WM_KEYUP nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:0 fAltDown:1 fRepeat:0 fUp:0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top