PostMessage troubles with SHIFT key - Programmer

This is a discussion on PostMessage troubles with SHIFT key - Programmer ; I use PostMessage to drive an application by sending it keyboard events. Messages that are sent with no modifiers are processed well. As an example, the '.' key message sent here: My messages: Recognized: "Release brakes" Message: 0x100 wParam: 0xbe ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: PostMessage troubles with SHIFT key

  1. PostMessage troubles with SHIFT key

    I use PostMessage to drive an application by sending it keyboard
    events. Messages that are sent with no modifiers are processed well.
    As an example, the '.' key message sent here:

    My messages:

    Recognized: "Release brakes"
    Message: 0x100 wParam: 0xbe lParam: 0x340001
    Message: 0x102 wParam: 0x2e lParam: 0x340001
    Message: 0x101 wParam: 0xbe lParam: 0xc0340001

    What Spy++ generates when the '.' key is pressed:

    00130122 P WM_KEYDOWN wParam:000000BE lParam:00340001
    00130122 P WM_CHAR wParam:0000002E lParam:00340001
    00130122 P WM_KEYUP wParam:000000BE lParam:C0340001

    (Both are the same).

    But then, if I try doing the same thing for the SHIFT-1 key sequence,
    I generate the following messages from my application:

    Message: 0x100 wParam: 0x10 lParam: 0x2a0001
    Message: 0x100 wParam: 0x31 lParam: 0x20001
    Message: 0x102 wParam: 0x21 lParam: 0x20001
    Message: 0x101 wParam: 0x31 lParam: 0xc0020001
    Message: 0x101 wParam: 0x10 lParam: 0xc02a0001

    And Spy++ generated the following:

    00130122 P WM_KEYDOWN wParam:00000010 lParam:002A0001
    00130122 P WM_KEYDOWN wParam:00000031 lParam:00020001
    00130122 P WM_CHAR wParam:00000021 lParam:00020001
    00130122 P WM_KEYUP wParam:00000031 lParam:C0020001
    00130122 P WM_KEYUP wParam:00000010 lParam:C02A0001

    (Both are the same again).

    For an obscure reason, the application that I try to control through
    those messages (Microsoft Flight Simulator 2004) does not react to the
    SHIFT message, but does behave as expected for messages like the '.'
    pressed. Are there other messages than keyboard messages that I should
    care about for modifiers? What could be so special about the SHIFT
    modifier that would make it not work? Any help is greatly appreciated.

  2. Re: PostMessage troubles with SHIFT key

    Although you posted the messages, functions like GetKeyboardState
    and GetKeyState will continue to report that the shift key is not
    down.

    If you want to generate fake input, use SendInput.

    On 3 Mar 2004 14:30:38 -0800, incognito-3@excite.com (incognito)
    wrote:
    >But then, if I try doing the same thing for the SHIFT-1 key sequence,
    >I generate the following messages from my application:
    >
    > Message: 0x100 wParam: 0x10 lParam: 0x2a0001
    > Message: 0x100 wParam: 0x31 lParam: 0x20001
    > Message: 0x102 wParam: 0x21 lParam: 0x20001
    > Message: 0x101 wParam: 0x31 lParam: 0xc0020001
    > Message: 0x101 wParam: 0x10 lParam: 0xc02a0001
    >
    >And Spy++ generated the following:
    >
    >00130122 P WM_KEYDOWN wParam:00000010 lParam:002A0001
    >00130122 P WM_KEYDOWN wParam:00000031 lParam:00020001
    >00130122 P WM_CHAR wParam:00000021 lParam:00020001
    >00130122 P WM_KEYUP wParam:00000031 lParam:C0020001
    >00130122 P WM_KEYUP wParam:00000010 lParam:C02A0001
    >
    >(Both are the same again).
    >
    >For an obscure reason, the application that I try to control through
    >those messages (Microsoft Flight Simulator 2004) does not react to the
    >SHIFT message, but does behave as expected for messages like the '.'
    >pressed. Are there other messages than keyboard messages that I should
    >care about for modifiers? What could be so special about the SHIFT
    >modifier that would make it not work? Any help is greatly appreciated.



  3. Re: PostMessage troubles with SHIFT key

    Mr. Chen,

    Thank you for your reply. I have tried to work with SendInput. The
    documentation and samples that can be found are less than adequate for
    it.

    As an example, every sample code that can be found online doesn't
    specify a different HWND to send the message to. Also, is the
    KEYEVENTF_KEYUP flag stating that it will do a key up after a key
    down, or does it transform the entire message into a key up? I have
    tried all those permutations.

    My code to date is the following:

    int VoiceKernel::MyPostMessage( HWND dWindow, unsigned int message,
    unsigned int wparam, long lparam ) {
    int dReturn = 0;
    HWND keepFocus = GetFocus();
    HWND keepActive = GetActiveWindow();
    SetFocus( dWindow );
    SetActiveWindow( dWindow );
    INPUT dInput[1];
    memset( &dInput[0], 0, sizeof( dInput[0] ) );
    dInput[0].type = INPUT_KEYBOARD;
    dInput[0].ki.wVk = wparam;
    if ( message == WM_KEYUP )
    {
    dInput[0].ki.dwFlags = KEYEVENTF_KEYUP;
    }
    else
    {
    dInput[0].ki.dwFlags = 0;
    }
    dReturn = SendInput( 1, &dInput[0], sizeof( dInput[0] ) );
    SetFocus( keepFocus );
    SetActiveWindow( keepActive );
    return dReturn;
    }

    My only result is to get a 'bing' sound when called. Any help is more
    than welcome at this point.

    Raymond Chen wrote in message news:...
    > Although you posted the messages, functions like GetKeyboardState
    > and GetKeyState will continue to report that the shift key is not
    > down.
    >
    > If you want to generate fake input, use SendInput.
    >
    > On 3 Mar 2004 14:30:38 -0800, incognito-3@excite.com (incognito)
    > wrote:
    > >But then, if I try doing the same thing for the SHIFT-1 key sequence,
    > >I generate the following messages from my application:
    > >
    > > Message: 0x100 wParam: 0x10 lParam: 0x2a0001
    > > Message: 0x100 wParam: 0x31 lParam: 0x20001
    > > Message: 0x102 wParam: 0x21 lParam: 0x20001
    > > Message: 0x101 wParam: 0x31 lParam: 0xc0020001
    > > Message: 0x101 wParam: 0x10 lParam: 0xc02a0001
    > >
    > >And Spy++ generated the following:
    > >
    > >00130122 P WM_KEYDOWN wParam:00000010 lParam:002A0001
    > >00130122 P WM_KEYDOWN wParam:00000031 lParam:00020001
    > >00130122 P WM_CHAR wParam:00000021 lParam:00020001
    > >00130122 P WM_KEYUP wParam:00000031 lParam:C0020001
    > >00130122 P WM_KEYUP wParam:00000010 lParam:C02A0001
    > >
    > >(Both are the same again).
    > >
    > >For an obscure reason, the application that I try to control through
    > >those messages (Microsoft Flight Simulator 2004) does not react to the
    > >SHIFT message, but does behave as expected for messages like the '.'
    > >pressed. Are there other messages than keyboard messages that I should
    > >care about for modifiers? What could be so special about the SHIFT
    > >modifier that would make it not work? Any help is greatly appreciated.


  4. Re: PostMessage troubles with SHIFT key

    Focus and active window work only within your virtual input
    state. You need to set the foreground window (which is global,
    not virtualized) and then the active window within that thread
    (which is not something a thread typically gives you access to).

    The easiest way is just to leave focus on the window you want to
    receive the input.

    Instead of trying to convert PostMessage, just send the whole
    thing at one go. For example, to simulate the press and release
    of shift and period to the current focus window. it would be
    something like (note: not tested)

    INPUT input[4] = { 0 };

    // shift down
    input[0].type = INPUT_KEYBOARD;
    input[0].ki.wVk = VK_SHIFT;
    input[0].ki.wScan = 42; // left shift

    // period down
    input[1].type = INPUT_KEYBOARD;
    input[1].ki.wVk = VK_OEM_PERIOD;
    input[1].ki.wScan = 52; // period

    // period up
    input[2].type = INPUT_KEYBOARD;
    input[2].ki.wVk = VK_OEM_PERIOD;
    input[2].ki.dwFlags = KEYEVENTF_KEYUP;
    input[2].ki.wScan = 52; // period

    // shift up
    input[3].type = INPUT_KEYBOARD;
    input[3].ki.wVk = VK_SHIFT;;
    input[3].ki.dwFlags = KEYEVENTF_KEYUP;
    input[3].ki.wScan = 42; // left shift

    SendInput(4, input, sizeof(input[0]));



  5. Re: PostMessage troubles with SHIFT key

    Thank you very much. That worked. I did not set wScan right. I truly
    appreciate your help.

    Raymond Chen wrote in message news:...
    > Focus and active window work only within your virtual input
    > state. You need to set the foreground window (which is global,
    > not virtualized) and then the active window within that thread
    > (which is not something a thread typically gives you access to).
    >
    > The easiest way is just to leave focus on the window you want to
    > receive the input.
    >
    > Instead of trying to convert PostMessage, just send the whole
    > thing at one go. For example, to simulate the press and release
    > of shift and period to the current focus window. it would be
    > something like (note: not tested)
    >
    > INPUT input[4] = { 0 };
    >
    > // shift down
    > input[0].type = INPUT_KEYBOARD;
    > input[0].ki.wVk = VK_SHIFT;
    > input[0].ki.wScan = 42; // left shift
    >
    > // period down
    > input[1].type = INPUT_KEYBOARD;
    > input[1].ki.wVk = VK_OEM_PERIOD;
    > input[1].ki.wScan = 52; // period
    >
    > // period up
    > input[2].type = INPUT_KEYBOARD;
    > input[2].ki.wVk = VK_OEM_PERIOD;
    > input[2].ki.dwFlags = KEYEVENTF_KEYUP;
    > input[2].ki.wScan = 52; // period
    >
    > // shift up
    > input[3].type = INPUT_KEYBOARD;
    > input[3].ki.wVk = VK_SHIFT;;
    > input[3].ki.dwFlags = KEYEVENTF_KEYUP;
    > input[3].ki.wScan = 42; // left shift
    >
    > SendInput(4, input, sizeof(input[0]));


+ Reply to Thread