PostMessage LPARAM trouble

This is a discussion on PostMessage LPARAM trouble within the C++ Programming forums, part of the General Programming Boards category; I am trying to send a keystroke to another window via PostMessage. I can successfully run WM_KEYDOWN without my program ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    20

    PostMessage LPARAM trouble

    I am trying to send a keystroke to another window via PostMessage. I can successfully run WM_KEYDOWN without my program crashing, but the other 3 messages fail.

    As of now I'm sending 'a' to the other program. (0x00021e0*)

    Code:
    #define SETBITS(var,mask)   (var |= mask)
    #define CLEARBITS(var,mask) (var &= ~mask)
    #define CHKBITS(var,mask)   ((var&mask) == mask)
    
    ...
    
    void SendKey(char input)
    {
    	LPARAM lParam;
    	short key = VkKeyScan(input);
    	UINT scancode = MapVirtualKey(key, 0);
    	SETBITS(lParam,0x00021e00);
    	PostMessage(lWnd, WM_KEYDOWN, key, lParam);
    	Sleep(10);
    	SETBITS(lParam,0x00021e02);
    	PostMessage(lWnd, WM_CHAR, key, lParam);
    	Sleep(10);
    	SETBITS(lParam,0x00021e03);
    	PostMessage(lWnd, WM_KEYUP, key, lParam);
    	Sleep(10);
    }
    I created 0x00021e0* from the API documentation, but like I said the last 3 are failing.

    Code:
    29
        Specifies the context code. The value is 1 if the ALT key is held down while the key is pressed; otherwise, the value is 0.
    30
        Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.
    31
        Specifies the transition state. The value is 1 if the key is being released, or it is 0 if the key is being pressed.
    So the first PostMessage() is obviously 0000. The second one that is not working is 0010, and the third is 0011.

    Help?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > SETBITS(lParam,0x00021e00);
    How about a nice simple
    lParam = 0x00021e00;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    That works too! I'm new to bit manipulation so I thought everything had to be or'ed to be set. that's why my macro or's the mask. But unfortunately it still crashes, so I'm guessing it's still the lparam.


    EDIT: I found the solution...lparam doesn't really matter at all either >_>. I forgot to initialize scancode = 0x0; My program now doesn't crash!
    Last edited by fl00d; 12-05-2007 at 09:01 PM. Reason: found a solution

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Display text in a Dialog Box
    By Dark_Phoenix in forum Windows Programming
    Replies: 9
    Last Post: 01-02-2009, 06:30 AM
  2. classmember WndProc isn't called ...
    By Greenhorn__ in forum Windows Programming
    Replies: 10
    Last Post: 07-19-2008, 12:40 PM
  3. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  4. button in function
    By algi in forum Windows Programming
    Replies: 1
    Last Post: 03-21-2005, 11:12 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21