窗口处于活动状态时无法切换键盘语言

时间:2012-08-30 19:42:30

标签: c winapi

我正在学习Win32 API。我创建了一个空窗口,并在其中添加了一个编辑框以输入一些文本。我可以在其中键入文本,但无法切换键盘语言。

Alt + Shift等快捷方式不起作用。如果我单击托盘中的语言图标,它将暂停几秒钟,然后它将最终打开列表,但我的窗口将变为非活动状态,因此更改不会影响它。

如果我这样做,我可以改变语言:

  1. 按Alt + Shift(或任何组合)
  2. 按Alt,Space
  3. 通常,在左上方的窗口菜单中弹出第2步,但同时步骤1也将变为实际步骤。

    我想,我没有处理某种消息,或者创建错误的窗口,但我找不到错误的内容。这是完整的代码:

    #include <windows.h>
    
    // handle of the edit box
    HWND testEditBox;
    
    // shows a message
    void showMessage( LPSTR message ){
        MessageBox( GetActiveWindow(), message, "", MB_OK );
    }
    
    // a simple window procedure
    LRESULT CALLBACK windowProc( HWND window, UINT message, WPARAM wparam, LPARAM lparam )
    {
        switch( message )
        {
            case WM_CLOSE:
                DestroyWindow( window );
                break;
            case WM_DESTROY:
                PostQuitMessage( 0 );
                break;
            default:
                return DefWindowProc( window, message, wparam, lparam );
        }
        return 0;
    }
    
    int WINAPI WinMain( HINSTANCE instance, HINSTANCE prevInstance, LPSTR commandLine, int showMode )
    {
        // create window class
        WNDCLASS windowClass;
        windowClass.cbClsExtra = 0;
    windowClass.cbWndExtra = 0;
    windowClass.style = 0;
        windowClass.hInstance = instance;
        windowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
        windowClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
        windowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
        windowClass.lpszClassName = "testWindow";
        windowClass.lpfnWndProc = windowProc;
    
        if( !RegisterClass( &windowClass ) )
        {
            showMessage( "Some trouble, sir" );
            return GetLastError();
        }
    
        HWND window = CreateWindow
        (
            "testWindow",
            "Test Window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT,
            400, 300,
            NULL, // no parent
            NULL, // no menu
            instance,
            NULL // no 'window-creation data'
        );
    
        if( !window ){
            showMessage( "Some trouble, sir" );
            return GetLastError();
        }
    
        ShowWindow( window, showMode );
    
    
        // add an EDIT box to our window
        testEditBox = CreateWindow
        (
            "EDIT",
            "input here",
            WS_CHILD,
            10, 10,
            200, 20,
            window,
            NULL,
            instance,
            NULL
        );
        ShowWindow( testEditBox, SW_SHOW );
    
        // start message loop
        MSG message;
        while( GetMessage( &message, window, 0, 0 ) > 0 )
        {
            TranslateMessage( &message );
            DispatchMessage( &message );
        }
    
    
        return message.wParam;
    }
    

1 个答案:

答案 0 :(得分:0)

问题出在GetMessage( &message, window, 0, 0 )行。您只收到window的邮件,而不是testEditBox,因此不会分派某些邮件。该行应该像GetMessage(&message, NULL, 0, 0)

当我按下并释放 Ctrl + Shift 时,这些是testEditBox在您的应用程序中收到的消息:

WM_KEYDOWN nVirtKey:VK_CONTROL cRepeat:1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYDOWN nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYUP nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:1 fUp:1
WM_KEYUP nVirtKey:VK_CONTROL cRepeat:1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:1 fUp:1

将它们与修复后的消息进行比较:

WM_KEYDOWN nVirtKey:VK_CONTROL cRepeat:1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYDOWN nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYUP nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:1 fUp:1
WM_IME_NOTIFY dwCommand:IMN_CLOSESTATUSWINDOW dwData:00000000
WM_IME_NOTIFY dwCommand:IMN_OPENSTATUSWINDOW dwData:00000000
WM_INPUTLANGCHANGE charset:0 hkl:04090409 //this
WM_IME_NOTIFY dwCommand:IMN_SETCOMPOSITIONFONT dwData:00000000
WM_KEYUP nVirtKey:VK_CONTROL cRepeat:1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:1 fUp:1