C# API Sendmessage or PostMessage to replace SendKeys - Help Needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NVergunst
    New Member
    • Apr 2007
    • 18

    C# API Sendmessage or PostMessage to replace SendKeys - Help Needed

    Hello everyone.

    I have what I believe to be a simple question, yet I can not find anything that is helpful to me.

    Basically I have an application, that I want to use to control external applications. My program will run either embedded within another program or minimized to the system tray. So SendKeys is out because it only works if the controlled application has focus, which it will not.

    So my conclusion is the windows API. I have never used the windows API, and from my experience the past few days I am not sure I want to!

    So I am using user32.dll like this (these are not written in stone and are used by looking at other examples):
    Code:
    [DllImport("user32.dll")]
            public static extern int FindWindow(
            string lpClassName, // class name
            string lpWindowName // window name
            );
    [DllImport("user32.dll")]
            public static extern int SendMessage(
            int hWnd, // handle to destination window
            uint Msg, // message
            int wParam, // first message parameter
            int lParam // second message parameter
            );
    FindWindow() works perfectly. I can get the handle of the window. And the sendmessage function works as well with the example I pulled it from which calls the close command of the window like this:

    Code:
    const int WM_SYSCOMMAND = 0x018;
    const int SC_CLOSE = 0x053;
    
    int WindowToFind = FindWindow(null,"Untitled - Notepad");
    int result = SendMessage(WindowToFind, WM_SYSCOMMAND, SC_CLOSE, 0);
    Now this is all fine and dandy, but the question becomes, how do I send the keypress "A" or "B" or "Ctrl+D" to some window?

    I read that the command "WM_KEYDOWN = 0x0100" should be used, but what is the argument? Is it the character code in hex? Is it some random thing? I have not seen any working code or full samples of code anywhere. If someone could link me to some fully working code to do this, or just post a sample I would be highly appreciative.

    Thankyou in advance...
  • NVergunst
    New Member
    • Apr 2007
    • 18

    #2
    Anyone? Bueler, bueler?

    Comment

    • NVergunst
      New Member
      • Apr 2007
      • 18

      #3
      Doe nobody know how to send keystrokes through the windows API?

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by NVergunst
        Anyone? Bueler, bueler?
        Instead of trying to be "cute" by making a movie reference that probably only 10% of the people here have seen, why don't you try posting to the correct forum. The .NET forum is where the C# questions are answered not the c/c++ forum. But lucky for you I moderate both and can move this thread over. Next time pay better attention to what you are doing.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Originally posted by NVergunst

          Code:
          const int WM_SYSCOMMAND = 0x018;
          const int SC_CLOSE = 0x053;
          
          int WindowToFind = FindWindow(null,"Untitled - Notepad");
          int result = SendMessage(WindowToFind, WM_SYSCOMMAND, SC_CLOSE, 0);
          Now this is all fine and dandy, but the question becomes, how do I send the keypress "A" or "B" or "Ctrl+D" to some window?

          I read that the command "WM_KEYDOWN = 0x0100" should be used, but what is the argument? Is it the character code in hex? Is it some random thing? I have not seen any working code or full samples of code anywhere. If someone could link me to some fully working code to do this, or just post a sample I would be highly appreciative.

          Thankyou in advance...
          You can use SendMessage the same way as before but instead of WM_SYSCOMMAND you make it WM_KEYDOWN and instead of SC_CLOSE you need to pass the hex value of the keypress or see if the VK_ key mappings will work for you. Also be aware how WM_KEYDOWN messages are handled you may need to make up special WPARAM and LPARAM parameters in order to process Shift+ Ctrl+ and Alt+ keys.

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Here you go:

            The structure of a WM_KEYDOWN message: http://msdn2.microsoft.com/en-us/library/ms646280.aspx

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              You might also want to consider sending a WM_KEYUP message since it is almost impossible to only press a key down and never up on a computer system. Some applications my not respond to input until they receive both a KEYDOWN and KEYUP message.

              Comment

              • NVergunst
                New Member
                • Apr 2007
                • 18

                #8
                Originally posted by RedSon
                Instead of trying to be "cute" by making a movie reference that probably only 10% of the people here have seen, why don't you try posting to the correct forum. The .NET forum is where the C# questions are answered not the c/c++ forum. But lucky for you I moderate both and can move this thread over. Next time pay better attention to what you are doing.
                I honestly didnt even see the .NET area. sorry. I figured C# fits into C++/C better than basic or java. sorry again.

                Comment

                • NVergunst
                  New Member
                  • Apr 2007
                  • 18

                  #9
                  Originally posted by RedSon
                  You can use SendMessage the same way as before but instead of WM_SYSCOMMAND you make it WM_KEYDOWN and instead of SC_CLOSE you need to pass the hex value of the keypress or see if the VK_ key mappings will work for you. Also be aware how WM_KEYDOWN messages are handled you may need to make up special WPARAM and LPARAM parameters in order to process Shift+ Ctrl+ and Alt+ keys.
                  I thought that I had tried that, but ti didn't work...

                  WM_KEYDOWN = 0x0100

                  or so I believe.

                  Then for instance to send the 'A' key, it would be: 0x0041 isnt it?

                  So then SendMessage(win dow, 0x0100, 0x0041, 0) should do it right? Well I cant seem to get it to work with Notepad for example.

                  Could you post a full example please?

                  Comment

                  • NVergunst
                    New Member
                    • Apr 2007
                    • 18

                    #10
                    Originally posted by RedSon
                    Here you go:

                    The structure of a WM_KEYDOWN message: http://msdn2.microsoft.com/en-us/library/ms646280.aspx
                    Yes I have seen that, but honestly makes no sense whatsoever to me.

                    Is that the bit makeup of the 32bit integer?

                    so if I want to send it once, then bits 0-15 are <00000000000000 1>
                    and for the letter A it is for bits 16 through 23 <01000001>
                    and then not extended key so bit 24 is <0>
                    and 25-31 are all zero because some are reserved and the others must be down for a WM_KEYDOWN message.

                    So total would be <00000000010000 010000000000000 001> for the byte to send. That in decimal form is: 4259841 and hex would be 0x410001

                    so the total message would be SendMessage(Win dow, 0x100, 0x410001, 0)

                    or did I do something completely wrong?

                    I learn via examples, and I cant seem to find any full examples. Maybe code that take input from some text box and outputs it to notepad... Something simple for me to digest.

                    thanks for the responses so far.

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #11
                      Actually its better to use PostMessage since it returns right away.

                      Code:
                      LRESULT PostMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
                      
                      WM_KEYDOWN
                             WPARAM wParam
                              LPARAM lParam;
                      Parameters
                          wParam
                              Specifies the virtual-key code of the nonsystem key. 
                          lParam
                              Specifies the repeat count, scan code, extended-key flag, 
                              context code, previous key-state flag, and transition-state flag, 
                              as shown in [URL=http://msdn2.microsoft.com/en-us/library/ms646280.aspx]this[/URL] table.
                      
                      ......So......
                      
                      LRESULT debugvalue = PostMessage( Handle_to_where_the_message_is_going,
                                                        WM_KEYDOWN, (WPARAM)[URL=http://msdn2.microsoft.com/en-us/library/aa908606.aspx]VK_ENTER[/URL], (LPARAM)0)

                      Comment

                      • RedSon
                        Recognized Expert Expert
                        • Jan 2007
                        • 4980

                        #12
                        Keep in mind that you might also want to send a WM_KEYUP message too.

                        Comment

                        Working...