Sending Keystroke to HWND

This is a discussion on Sending Keystroke to HWND within the Windows Programming forums, part of the Platform Specific Boards category; hey does any1 know how to send like Ctrl+X to a window with SendMessage? I dont want to use accelarators ...

  1. #1
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204

    Sending Keystroke to HWND

    hey does any1 know how to send like Ctrl+X to a window with SendMessage? I dont want to use accelarators for this matter. I can use WM_KEYDOWN and WM_CHAR but how do i send multiple keys?

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    To send multiple keys you just send a series of WM_CHAR messages. One message per character to send.
    WM_CHAR is your best bet for sending them, I think that is the most commonly processed keyboard message.

    Incidently if you are trying to get text in a text box or similar child window you can use teh WM_SETTEXT message to send an entire string.

  3. #3
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Exile
    To send multiple keys you just send a series of WM_CHAR messages. One message per character to send.
    WM_CHAR is your best bet for sending them, I think that is the most commonly processed keyboard message.

    Incidently if you are trying to get text in a text box or similar child window you can use teh WM_SETTEXT message to send an entire string.

    Well its not working i dont think WM_CHAR detects Control Key but i tried WM_KEYDOWN maybe you can tell me why it aint wrking, it should do a command once i send Ctrl+Y in it.

    heres the code that aint workig right..
    i have this inside WM_COMMAND, execute whenever this button is pressed, but aint sending correctly..

    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYDOWN, VK_CONTROL, 0 );
    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYDOWN, 89, 0 );

    89 = Letter 'Y'

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    Unlike the WM_LBUTTONDOWN messages the WM_CHAR and WM_KEYDOWN events do not send any modifier key status. So you can't send the control key press in the message. So you'll have to do some other stuff to get that key press.

    I don't know of any way to send the modifier keys to the program. You may be able to try something like:

    Code:
    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYDOWN, VK_CONTROL, 0 );
    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYDOWN, 89, 0 );
    SendDlgItemMessage( hwnd,e_mdichild, WM_CHAR, 89, 0 );
    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYUP, 89, 0 );
    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYUP, VK_CONTROL, 0 );
    Just to make sure all the appropriate key events are sent.

    Since you are dealing with a child window (based on teh function calls used) is you can use SetKeyboardState() to mark the control key as pressed, then send the WM_CHAR for key 89. This way whatever method the window uses to detemine that the control key is/isn't down should say that its down. SetKeyboardState() will only effect the thread that calls is, so if your child window was created by a different thread than the one that calls SetKeyboardState() the child window won't see the results. It would look something like this:

    Code:
    BYTE KeyState[256];
    GetKeyState(&KeyState);
    //Have the key down high order byte needs to be 1
    //how to do that eludes me right now, so I'm just blanketly setting it to 1
    //the correct way might be (still not positive on it):
    //KeyState[VK_LCONTROL] = 0x00010000;
    KeyState[VK_LCONTROL] = 0xFFFFFFFF;
    if (SetKeyStates(&KeyState) == FALSE)
    {
        MessageBox(NULL, "Failed to set key state!", "Error!", MB_OK);
    }
     SendDlgItemMessage( hwnd,e_mdichild, WM_KEYDOWN, 89, 0 );
    I've never used taht before, so I'm not 100% sure thats how it will work.

    Beyond those two suggestions I'm not sure what to say, what you are looking for isn't something the Windows API really covers, simulating mouse cliks is easy, keyboard events can be done but not to any real detail. You might be stuck there, since there just isn't any good way to send a modified key press event.

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Exile
    Unlike the WM_LBUTTONDOWN messages the WM_CHAR and WM_KEYDOWN events do not send any modifier key status. So you can't send the control key press in the message. So you'll have to do some other stuff to get that key press.

    I don't know of any way to send the modifier keys to the program. You may be able to try something like:

    Code:
    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYDOWN, VK_CONTROL, 0 );
    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYDOWN, 89, 0 );
    SendDlgItemMessage( hwnd,e_mdichild, WM_CHAR, 89, 0 );
    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYUP, 89, 0 );
    SendDlgItemMessage( hwnd,e_mdichild, WM_KEYUP, VK_CONTROL, 0 );
    Just to make sure all the appropriate key events are sent.

    Since you are dealing with a child window (based on teh function calls used) is you can use SetKeyboardState() to mark the control key as pressed, then send the WM_CHAR for key 89. This way whatever method the window uses to detemine that the control key is/isn't down should say that its down. SetKeyboardState() will only effect the thread that calls is, so if your child window was created by a different thread than the one that calls SetKeyboardState() the child window won't see the results. It would look something like this:

    Code:
    BYTE KeyState[256];
    GetKeyState(&KeyState);
    //Have the key down high order byte needs to be 1
    //how to do that eludes me right now, so I'm just blanketly setting it to 1
    //the correct way might be (still not positive on it):
    //KeyState[VK_LCONTROL] = 0x00010000;
    KeyState[VK_LCONTROL] = 0xFFFFFFFF;
    if (SetKeyStates(&KeyState) == FALSE)
    {
        MessageBox(NULL, "Failed to set key state!", "Error!", MB_OK);
    }
     SendDlgItemMessage( hwnd,e_mdichild, WM_KEYDOWN, 89, 0 );
    I've never used taht before, so I'm not 100% sure thats how it will work.

    Beyond those two suggestions I'm not sure what to say, what you are looking for isn't something the Windows API really covers, simulating mouse cliks is easy, keyboard events can be done but not to any real detail. You might be stuck there, since there just isn't any good way to send a modified key press event.

    Thnks for these suggestions ill try them when i wake up later, its already morning here, been awake all day. ill just post here if they work thnks

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Are you just trying to cut the text from a window? If so, there are better ways than sending ctrl-x. I've tried sending ctrl + another key to a window in another process before, and the only way I was able to get it to work was the following:

    Code:
    SetForegroundWindow(hwnd); 
    keybd_event(VK_CONTROL, 0x1D, 0, 0); 
    keybd_event(0x58, 0x47, 0, 0); 
    keybd_event(0x58, 0x47, KEYEVENTF_KEYUP, 0); 
    keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_KEYUP, 0);

  7. #7
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by bithub
    Are you just trying to cut the text from a window? If so, there are better ways than sending ctrl-x. I've tried sending ctrl + another key to a window in another process before, and the only way I was able to get it to work was the following:

    Code:
    SetForegroundWindow(hwnd); 
    keybd_event(VK_CONTROL, 0x1D, 0, 0); 
    keybd_event(0x58, 0x47, 0, 0); 
    keybd_event(0x58, 0x47, KEYEVENTF_KEYUP, 0); 
    keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_KEYUP, 0);

    The other one didnt work, but yours worked perfectly thnks to both

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  3. My first "real" windows app
    By JoshR in forum Windows Programming
    Replies: 2
    Last Post: 07-28-2005, 07:40 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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