Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sending keys

Sending keys

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Justin Tay

    SendInput. The window must have focus. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/sendinput.asp[^]

    S Offline
    S Offline
    Smith
    wrote on last edited by
    #3

    sendInput doesn't take Hwnd as a param so is it not possible to specify the window? and hence you said "The window must have focus" ? NULL

    J T 2 Replies Last reply
    0
    • S Smith

      sendInput doesn't take Hwnd as a param so is it not possible to specify the window? and hence you said "The window must have focus" ? NULL

      J Offline
      J Offline
      Justin Tay
      wrote on last edited by
      #4

      Yes it's not possible to specify a window as it's simulating keys at the lowest level. You would have to set the window as the foreground window. Raymond talks about this. You can also read the comments for more good info. http://blogs.msdn.com/oldnewthing/archive/2005/05/30/423202.aspx[^] Using PostMessage with WM_KEYDOWN / WM_KEYUP / WM_SYSKEYDOWN / WM_SYSKEYUP (WM_CHAR is generated by the TranslateMessage function) might work with a number of applications though (there's that problem with key states though). You would have to test with all the applications you wish to send input to if you would like to still continue with this route. -- modified at 6:49 Thursday 6th July, 2006

      1 Reply Last reply
      0
      • S Smith

        sendInput doesn't take Hwnd as a param so is it not possible to specify the window? and hence you said "The window must have focus" ? NULL

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #5

        Meat Loaf wrote:

        endInput doesn't take Hwnd as a param so is it not possible to specify the window? and hence you said "The window must have focus" ?

        then First SetFocus on window and then send Keyinput to that window. Is setting focus on That window incurred any problem?

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

        S 1 Reply Last reply
        0
        • S Smith

          How do I send keys to application? I tried like ::SendMessage(hwnd,WM_CHAR,(WPARM)VK_KEY,NULL) But it's not right I guess.. please help me. NULL

          M Offline
          M Offline
          Muhammad Azam
          wrote on last edited by
          #6

          hi, Assuming m_pWnd is pointer to the window you want to send keys then use, m_pWnd->SendMessage(WM_KEYDOWN,WPARAM(VK_DELETE),LPARAM(0x01530001)); m_pWnd->SendMessage(WM_KEYUP,WPARAM(VK_DELETE),LPARAM(0xC1530001)); or ::SendMessage(hwnd,WM_KEYDOWN,WPARAM(VK_DELETE),LPARAM(0x01530001)); ::SendMessage(hwnd,WM_KEYUP,WPARAM(VK_DELETE),LPARAM(0xC1530001)); :) if this answers you then, vote for 5 :cool:

          S 2 Replies Last reply
          0
          • T ThatsAlok

            Meat Loaf wrote:

            endInput doesn't take Hwnd as a param so is it not possible to specify the window? and hence you said "The window must have focus" ?

            then First SetFocus on window and then send Keyinput to that window. Is setting focus on That window incurred any problem?

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

            S Offline
            S Offline
            Smith
            wrote on last edited by
            #7

            I haven't tried that yet. There must be no problem in setting the focus. But still I'd try to do it with a sendMessage. NULL

            T 1 Reply Last reply
            0
            • M Muhammad Azam

              hi, Assuming m_pWnd is pointer to the window you want to send keys then use, m_pWnd->SendMessage(WM_KEYDOWN,WPARAM(VK_DELETE),LPARAM(0x01530001)); m_pWnd->SendMessage(WM_KEYUP,WPARAM(VK_DELETE),LPARAM(0xC1530001)); or ::SendMessage(hwnd,WM_KEYDOWN,WPARAM(VK_DELETE),LPARAM(0x01530001)); ::SendMessage(hwnd,WM_KEYUP,WPARAM(VK_DELETE),LPARAM(0xC1530001)); :) if this answers you then, vote for 5 :cool:

              S Offline
              S Offline
              Smith
              wrote on last edited by
              #8

              Muhammad Azam wrote:

              ::SendMessage(hwnd,WM_KEYDOWN,WPARAM(VK_DELETE),LPARAM(0x01530001)); ::SendMessage(hwnd,WM_KEYUP,WPARAM(VK_DELETE),LPARAM(0xC1530001));

              What's being stored in LPARAM here? Can you explain a bit? NULL

              1 Reply Last reply
              0
              • M Muhammad Azam

                hi, Assuming m_pWnd is pointer to the window you want to send keys then use, m_pWnd->SendMessage(WM_KEYDOWN,WPARAM(VK_DELETE),LPARAM(0x01530001)); m_pWnd->SendMessage(WM_KEYUP,WPARAM(VK_DELETE),LPARAM(0xC1530001)); or ::SendMessage(hwnd,WM_KEYDOWN,WPARAM(VK_DELETE),LPARAM(0x01530001)); ::SendMessage(hwnd,WM_KEYUP,WPARAM(VK_DELETE),LPARAM(0xC1530001)); :) if this answers you then, vote for 5 :cool:

                S Offline
                S Offline
                Smith
                wrote on last edited by
                #9

                what If I'd do the try the same with VK_F1 ? should I change the LPARAM accordingly? NULL

                M 2 Replies Last reply
                0
                • S Smith

                  I haven't tried that yet. There must be no problem in setting the focus. But still I'd try to do it with a sendMessage. NULL

                  T Offline
                  T Offline
                  ThatsAlok
                  wrote on last edited by
                  #10

                  Meat Loaf wrote:

                  I haven't tried that yet. There must be no problem in setting the focus. But still I'd try to do it with a sendMessage.

                  then Simulate KeyPress by sending WM_KEYDOWN and WM_KEYUP message one after another!

                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                  cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Re

                  1 Reply Last reply
                  0
                  • S Smith

                    what If I'd do the try the same with VK_F1 ? should I change the LPARAM accordingly? NULL

                    M Offline
                    M Offline
                    Muhammad Azam
                    wrote on last edited by
                    #11

                    No I guess not, is it working with VK_F1?

                    1 Reply Last reply
                    0
                    • S Smith

                      what If I'd do the try the same with VK_F1 ? should I change the LPARAM accordingly? NULL

                      M Offline
                      M Offline
                      Muhammad Azam
                      wrote on last edited by
                      #12

                      ok here is your explaination 1. Run your Application, 2. run spy++ and set it to capture KeyBorad messages (using Finder tool). 3. Press F1 key (or what ever key you want to send). 4. analyze the Messages log window and double click on the message having WM_KEYDOWN and nVirtualKey:VK_F1 or WM_KEYUP and nVirtualKey:VK_F1. Get LPARAM value from there. you are done. The LPARAM valur for F1 remains constant. no matter where you run your application and when you run it. This way u can get LPARAM values for all your virtual key simulations, infact LPARAM contains bit combinations for ScanCode,RepeatCout,AltDown, Extended Value for the WM_KeyDown event. Thanks Azam

                      S 1 Reply Last reply
                      0
                      • M Muhammad Azam

                        ok here is your explaination 1. Run your Application, 2. run spy++ and set it to capture KeyBorad messages (using Finder tool). 3. Press F1 key (or what ever key you want to send). 4. analyze the Messages log window and double click on the message having WM_KEYDOWN and nVirtualKey:VK_F1 or WM_KEYUP and nVirtualKey:VK_F1. Get LPARAM value from there. you are done. The LPARAM valur for F1 remains constant. no matter where you run your application and when you run it. This way u can get LPARAM values for all your virtual key simulations, infact LPARAM contains bit combinations for ScanCode,RepeatCout,AltDown, Extended Value for the WM_KeyDown event. Thanks Azam

                        S Offline
                        S Offline
                        Smith
                        wrote on last edited by
                        #13

                        Hey Still not working :( . SendInput is the way to go I guess :~ NULL

                        1 Reply Last reply
                        0
                        • S Smith

                          How do I send keys to application? I tried like ::SendMessage(hwnd,WM_CHAR,(WPARM)VK_KEY,NULL) But it's not right I guess.. please help me. NULL

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #14

                          See my example here.


                          "The largest fire starts but with the smallest spark." - David Crow

                          "Judge not by the eye but by the heart." - Native American Proverb

                          S 1 Reply Last reply
                          0
                          • D David Crow

                            See my example here.


                            "The largest fire starts but with the smallest spark." - David Crow

                            "Judge not by the eye but by the heart." - Native American Proverb

                            S Offline
                            S Offline
                            Smith
                            wrote on last edited by
                            #15

                            Thanks david. It looks good I'll try that. NULL

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups