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#
  4. Problem with Send Keys to background process

Problem with Send Keys to background process

Scheduled Pinned Locked Moved C#
helptutorialquestion
8 Posts 3 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.
  • M Offline
    M Offline
    Member 3727499
    wrote on last edited by
    #1

    Hi everybody. I'm developing an application that sends keystrokes to another background application. The problem is that i have so send a "Alt+F", but i can't get it to work. I'm using PostMessage:

    [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
    private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

    const int VK_ALT = 0x12;
    const int WM_KEYDOWN = 0x100;

    PostMessage(hWnd, WM_KEYDOWN, VK_ALT, 0);
    PostMessage(hWnd, WM_KEYDOWN, VK_F, 0);

    The result is that the background application "see" the "F", but no the "Alt". Anybody has any idea how to fix it or how to do this without using SendKeys? Because the background application used to be minimized. Greetings and thanks in advance. :)

    F C 2 Replies Last reply
    0
    • M Member 3727499

      Hi everybody. I'm developing an application that sends keystrokes to another background application. The problem is that i have so send a "Alt+F", but i can't get it to work. I'm using PostMessage:

      [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
      private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

      const int VK_ALT = 0x12;
      const int WM_KEYDOWN = 0x100;

      PostMessage(hWnd, WM_KEYDOWN, VK_ALT, 0);
      PostMessage(hWnd, WM_KEYDOWN, VK_F, 0);

      The result is that the background application "see" the "F", but no the "Alt". Anybody has any idea how to fix it or how to do this without using SendKeys? Because the background application used to be minimized. Greetings and thanks in advance. :)

      F Offline
      F Offline
      freakyit
      wrote on last edited by
      #2

      hi, i have in mind to use the lPama for the modifier controls

      PostMessage(hWnd, WM_KEYDOWN, VK_F, VK_ALT);

      greetz

      M 1 Reply Last reply
      0
      • M Member 3727499

        Hi everybody. I'm developing an application that sends keystrokes to another background application. The problem is that i have so send a "Alt+F", but i can't get it to work. I'm using PostMessage:

        [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
        private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

        const int VK_ALT = 0x12;
        const int WM_KEYDOWN = 0x100;

        PostMessage(hWnd, WM_KEYDOWN, VK_ALT, 0);
        PostMessage(hWnd, WM_KEYDOWN, VK_F, 0);

        The result is that the background application "see" the "F", but no the "Alt". Anybody has any idea how to fix it or how to do this without using SendKeys? Because the background application used to be minimized. Greetings and thanks in advance. :)

        C Offline
        C Offline
        Covean
        wrote on last edited by
        #3

        I think the problem is, that you send an ALT key but not left or right ALT, what is a big difference for the OS. VK_ALT is only used in user application the simplify some things.

        1. send VK_LEFTALT = 0xA4 or VK_RIGHTALT = 0xA5

        or

        2. use SendKeys.Send

        or

        3. Have a look at (low-level) windows key hooking (SetWindowsHookEx). This helps the understand how key strokes are queued in the windows messaging system.

        Greetings Covean

        1 Reply Last reply
        0
        • F freakyit

          hi, i have in mind to use the lPama for the modifier controls

          PostMessage(hWnd, WM_KEYDOWN, VK_F, VK_ALT);

          greetz

          M Offline
          M Offline
          Member 3727499
          wrote on last edited by
          #4

          Thanks for both quick replies, but it doesn't seems to work :| Is there other way to do this? I can't use sendkeys because the application is minimized. Thanks

          C 1 Reply Last reply
          0
          • M Member 3727499

            Thanks for both quick replies, but it doesn't seems to work :| Is there other way to do this? I can't use sendkeys because the application is minimized. Thanks

            C Offline
            C Offline
            Covean
            wrote on last edited by
            #5

            If you understand C/C++ here is an old example where I send Ctrl+f to some window (hWnd).

            HWND hOldForegroundWindow = ::GetForegroundWindow();
            ::SetForegroundWindow(hWnd);
            ::ShowWindow(hWnd, SW\_MAXIMIZE);
            ::SetFocus(hWnd);
            Sleep(100);
            
            INPUT inputs\[2\];
            memset(&inputs\[0\], 0, sizeof(INPUT) \* 2);
            inputs\[0\].type = INPUT\_KEYBOARD;
            inputs\[0\].ki.wVk = VK\_CONTROL;
            inputs\[1\].type = INPUT\_KEYBOARD;
            inputs\[1\].ki.wVk = 70; // f
            
            ::SendInput(2, &inputs\[0\], sizeof(INPUT));
            
            Sleep(100);
            
            inputs\[0\].ki.dwFlags = KEYEVENTF\_KEYUP;
            inputs\[1\].ki.dwFlags = KEYEVENTF\_KEYUP;
            ::SendInput(2, &inputs\[0\], sizeof(INPUT));
            

            Maybe in your case you just have to wait some short period of time and send a KEYUP event.

            Greetings Covean

            M 1 Reply Last reply
            0
            • C Covean

              If you understand C/C++ here is an old example where I send Ctrl+f to some window (hWnd).

              HWND hOldForegroundWindow = ::GetForegroundWindow();
              ::SetForegroundWindow(hWnd);
              ::ShowWindow(hWnd, SW\_MAXIMIZE);
              ::SetFocus(hWnd);
              Sleep(100);
              
              INPUT inputs\[2\];
              memset(&inputs\[0\], 0, sizeof(INPUT) \* 2);
              inputs\[0\].type = INPUT\_KEYBOARD;
              inputs\[0\].ki.wVk = VK\_CONTROL;
              inputs\[1\].type = INPUT\_KEYBOARD;
              inputs\[1\].ki.wVk = 70; // f
              
              ::SendInput(2, &inputs\[0\], sizeof(INPUT));
              
              Sleep(100);
              
              inputs\[0\].ki.dwFlags = KEYEVENTF\_KEYUP;
              inputs\[1\].ki.dwFlags = KEYEVENTF\_KEYUP;
              ::SendInput(2, &inputs\[0\], sizeof(INPUT));
              

              Maybe in your case you just have to wait some short period of time and send a KEYUP event.

              Greetings Covean

              M Offline
              M Offline
              Member 3727499
              wrote on last edited by
              #6

              Hi, thanks for your reply, but the problem is that i need to do this without setting focus on the application :~ . I'm trying to do this with the notepad (for test), sending CTRL+F to open the "Search" window, but when i run the application, the notepad shows a "F" in the edit area. I don't know why it's ignoring the "CTRL".

              C 1 Reply Last reply
              0
              • M Member 3727499

                Hi, thanks for your reply, but the problem is that i need to do this without setting focus on the application :~ . I'm trying to do this with the notepad (for test), sending CTRL+F to open the "Search" window, but when i run the application, the notepad shows a "F" in the edit area. I don't know why it's ignoring the "CTRL".

                C Offline
                C Offline
                Covean
                wrote on last edited by
                #7

                1. I think in this case you can't use the SendKeys function, because they send the input to the window that has the focus. Can't you just switch the focus for this very short period of time to your app and than back to the app that had the focus before? 2. Ever tried to send KEYDOWN at first (try with ALT modifier set/unset) and after you sent left ALT + some key just sent an KEYUP event? Something like this:

                PostMessage(hWnd, KEYDOWN, VK_LEFTALT);
                PostMessage(hWnd, KEYDOWN, VK_F);
                Sleep(100);
                PostMessage(hWnd, KEYUP, VK_F);
                PostMessage(hWnd, KEYUP, VK_LEFTALT);

                Greetings Covean

                M 1 Reply Last reply
                0
                • C Covean

                  1. I think in this case you can't use the SendKeys function, because they send the input to the window that has the focus. Can't you just switch the focus for this very short period of time to your app and than back to the app that had the focus before? 2. Ever tried to send KEYDOWN at first (try with ALT modifier set/unset) and after you sent left ALT + some key just sent an KEYUP event? Something like this:

                  PostMessage(hWnd, KEYDOWN, VK_LEFTALT);
                  PostMessage(hWnd, KEYDOWN, VK_F);
                  Sleep(100);
                  PostMessage(hWnd, KEYUP, VK_F);
                  PostMessage(hWnd, KEYUP, VK_LEFTALT);

                  Greetings Covean

                  M Offline
                  M Offline
                  Member 3727499
                  wrote on last edited by
                  #8

                  Hi! I found the solution! it is a mix between a sendkeys and postmessage, that works :) First, set the "ControlKey" to "active" (i think it's like if we "press" the keyboard controlkey), then send the PostMessage to the hWnd (application), and then restore the "ControlKey" state.

                  keybd_event(VK_CONTROL, (byte)MapVirtualKey(VK_CONTROL, 0), 0, 0); // Control Down
                  PostMessage(hWnd, WM_KEYDOWN, (uint)VK_F, 0); // F
                  keybd_event(VK_CONTROL, (byte)MapVirtualKey(VK_CONTROL, 0), 2, 0); // Control Up

                  Thanks to all for the replies. :)

                  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