CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Guest

    Help! How to send a CTRL+X key to another window?

    There is a "SendKeys" procedure in VB to send one or more keystrokes to the active window as if typed at the keyboard, even a combination keystrokes,such as "CTRL+X".How to implement this function in VC?

    Thanks


  2. #2
    Join Date
    May 1999
    Posts
    53

    Re: Help! How to send a CTRL+X key to another window?

    Get the hwnd of the window you want to send the keys to (for example CWnd* pWnd = CWnd::FindWindow(NULL, "Untitled - Notepad"), and then post or send a WM_CHAR message to it. For example:

    [ccode]
    // Get the window that we want to send Ctrl+X to. In this example 'pWnd' will be set to
    // the edit control in Notepad (dialog ID 0x0F). Change this.
    CWnd* pWnd = CWnd::FindWindow(NULL, "Untitled - Notepad");
    if (pWnd)
    {
    pWnd = pWnd->GetDlgItem(0x0F);
    } else {
    AfxMessageBox("Can't find notepad");
    exit(0);
    }
    pWnd->SendMessage(WM_CHAR, 24, 0, 0); // 24 = Ctrl+X




    Hope this helps.



  3. #3
    Guest

    Re: Help! How to send a CTRL+X key to another window?

    Thank you very much!
    Another question,How about the "CTRL+SHIFT+somekey","ALT+somekey","CTRL+ALT+somekey","ALT+SHIFT+somekey"or other similar keystrokes combinations.

    Thanks.


  4. #4
    Join Date
    May 1999
    Posts
    53

    Re: Help! How to send a CTRL+X key to another window?


    if (GetKeyState(VK_CTRL) > 0 && GetKeyState(VK_SHIFT) > 0)
    {
    // Ctrl+Shift is pressed (Note: I'm not sure if it's VK_CTRL or VK_CONTROL)
    }



    it looks like there's no VK_ALT.





  5. #5
    Join Date
    May 1999
    Posts
    53

    Re: Help! How to send a CTRL+X key to another window?

    It's the 24th letter in the alphabet.

    Ctrl+letter returns the position of 'letter' in the alphabet.



  6. #6
    Join Date
    May 1999
    Posts
    53

    Re: Help! How to send a CTRL+X key to another window?

    Send? What do you mean?




  7. #7
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Help! How to send a CTRL+X key to another window?


    SendMessage( WM_KEYDOWN, //Shift F5) as an example
    SendMessage( WM_KEYDOWN, //Alt F5) as an example
    SendMessage( WM_KEYDOWN, //Ctrl F5) as an example

    PostMessage( WM_KEYDOWN, //Shift F5) as an example
    PostMessage( WM_KEYDOWN, //Alt F5) as an example
    PostMessage( WM_KEYDOWN, //Ctrl F5) as an example

    Sally



  8. #8
    Guest

    Re: Help! How to send a CTRL+X key to another window?

    Hey, I found a good solution.
    Use the following code:

    HWND hWnd=::FindWindow(NULL, some window caption);
    if (hWnd)
    {
    BYTE ks[1]=
    {
    VK_CTRL//if wanna to send a ALT+somekey, change to VK_MENU
    };
    DWORD tid=::GetWindowThreadProcessId(hWnd,NULL);
    ::SetKeyboardState(ks);
    ::AttachThreadInput(::GetCurrentThreadId(),tid,TRUE);
    ::BringWindowToTop(hWnd);
    ::PostMessage(hWnd,WM_KEYDOWN,someletter, 0);
    ::AttachThreadInput(::GetCurrentThreadId(),tid,FALSE);
    }
    However, I can't send the "shift+somekey", nor the combination of 3 keys, sush as "CTRL+SHIFT+somekey".



  9. #9
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Help! How to send a CTRL+X key to another window?

    <Ctrl>0 - <Ctrl>9 do not have a corresponding ASCII code as the letters do; to test for this you would trap the WM_CHAR for the digits 0-9, and use (::GetAsyncKeyState(WK_CONTROL) & 0x8000) (boolean test) to see if the <Ctrl> key is down at the same time.

    VB's SendKeys() has special characters to indicate <Ctrl> pressed, but in VC++ you would have to send WM_KEYDOWN messages for the key being pressed, then the WM_CHAR for the digit, then the WM_KEYUP for the key release. Not the most elegant way of doing it, unfortunately. The <Alt> key is even worse.

    --
    Jason Teagle
    [email protected]

  10. #10
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Help! How to send a CTRL+X key to another window?

    Oops, that should have been VK_CONTROL, not WK_CONTROL. Stupid keyboard.

    --
    Jason Teagle
    [email protected]

  11. #11
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Help! How to send a CTRL+X key to another window?

    Sending keystrokes to a window:

    * ASCII key (letter, digit, space, etc.):
    WM_CHAR with ASCII code.
    * Special + ASCII (special = non-ASCII, but not <Alt&gt:
    WM_KEYDOWN + VK_ (VK_SHIFT, VK_CONTROL, VK_NEXT (<PgDn&gt, etc.) code for each special key to be simulated, then WM_CHAR with ASCII code, then matching WM_KEYUP for special keys.
    * <Alt> + ASCII:
    WM_SYSCHAR with ASCII code. This SHOULD work, but if it doesn't then send the individual WM_SYSKEYDOWN and WM_SYSKEYUP instead.
    * <Alt> + Special + ASCII:
    WM_KEYDOWN for special keys, then WM_SYSCHAR (or key down and key up, if that doesn't work) with ASCII code, then WM_KEYUP for special keys.
    * Special on its own:
    WM_KEYDOWN and WM_KEYUP with VK_ code.
    * <Alt> on its own:
    WM_KEYDOWN and WM_KEYUP with VK_MENU (= <Alt&gt. Note that WM_SYSKEYxxx is for OTHER keys pressed with <Alt>, not <Alt> alone.

    I have had difficulty getting the <Alt> combinations to work sometimes, but the theory is OK!



    --
    Jason Teagle
    [email protected]

  12. #12
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Help! How to send a CTRL+X key to another window?

    You must be kidding........ I hope....

    Sally


  13. #13
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Help! How to send a CTRL+X key to another window?

    Kidding about what?

    --
    Jason Teagle
    [email protected]

  14. #14
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Help! How to send a CTRL+X key to another window?

    Essentially, yes; but not quite.

    ---// Repeat count is always 1 or more, for ANY key, ASCII-generating
    // or otherwise.
    SendMessage( WM_KEYDOWN, VK_CONTROL, 1 );

    // These two could be replaced by a WM_CHAR (WM_SYSCHAR for <Alt>+) -
    // as can all ASCII-generating keys and key combos.
    SendMessage( WM_KEYDOWN, '1', 1 );
    SendMessage( WM_KEYUP, '1', 0xC0000001 ); // Check out the help
    // for this message.

    SendMessage( WM_KEYUP, VK_CONTROL, 0xC0000001 );




    ---

    Painful, isn't it?



    --
    Jason Teagle
    [email protected]

  15. #15
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Help! How to send a CTRL+X key to another window?

    Does Windows have to make it THAT complicated to send a simple keystroke?????

    Sally


Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured