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. How to trigger Paste in terminal server window?

How to trigger Paste in terminal server window?

Scheduled Pinned Locked Moved C#
questiondatabasesysadmintutorial
8 Posts 3 Posters 9 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
    mav northwind
    wrote on last edited by
    #1

    Hi! I've asked a similar question some time ago, but unfortunately without usable results, so I try again. I put some text into the clipboard and want to trigger a paste action inside a terminal server session (i.e. programmatically do what happens when you press Ctrl+V inside a mstsc window). Yes, I know that you can't programmatically see the different windows inside the WTS session and that the currently active application will be the target for my paste, but that's exactly what I want. So far I tried using SendKeys, P/invoke to SendInput and keybd_event, posting WM_PASTE or even WM_KEYDOWN and WM_KEYUP with the exact parameters Spy++ reports, but I can't get it to work. The first 4 ways simply give no visible result at all, with WM_KEYDOWN/UP I get a plain "v" instead of Ctrl+V. Does anyone have any further idea?

    Regards, mav -- Black holes are the places where God divided by 0...

    K S 2 Replies Last reply
    0
    • M mav northwind

      Hi! I've asked a similar question some time ago, but unfortunately without usable results, so I try again. I put some text into the clipboard and want to trigger a paste action inside a terminal server session (i.e. programmatically do what happens when you press Ctrl+V inside a mstsc window). Yes, I know that you can't programmatically see the different windows inside the WTS session and that the currently active application will be the target for my paste, but that's exactly what I want. So far I tried using SendKeys, P/invoke to SendInput and keybd_event, posting WM_PASTE or even WM_KEYDOWN and WM_KEYUP with the exact parameters Spy++ reports, but I can't get it to work. The first 4 ways simply give no visible result at all, with WM_KEYDOWN/UP I get a plain "v" instead of Ctrl+V. Does anyone have any further idea?

      Regards, mav -- Black holes are the places where God divided by 0...

      K Offline
      K Offline
      Kristian Sixhoj
      wrote on last edited by
      #2

      mav.northwind wrote:

      So far I tried using SendKeys,

      Like this?

      SendKeys.SendWait("[Clipboard content here]");

      or like this:

      // simulates a Ctrl+V keypress
      SendKeys.SendWait("^a^v");

      If you've tried the second one, it wont work - as you probably have noticed. Console windows doesn't accept pasting of text. Try getting the content of the clipboard somehow, store it in a string and then write it out like this:

      SendKeys.SendWait(clipboardContent);

      Kristian Sixhoej


      "Failure is not an option" - Gene Kranz

      M 1 Reply Last reply
      0
      • K Kristian Sixhoj

        mav.northwind wrote:

        So far I tried using SendKeys,

        Like this?

        SendKeys.SendWait("[Clipboard content here]");

        or like this:

        // simulates a Ctrl+V keypress
        SendKeys.SendWait("^a^v");

        If you've tried the second one, it wont work - as you probably have noticed. Console windows doesn't accept pasting of text. Try getting the content of the clipboard somehow, store it in a string and then write it out like this:

        SendKeys.SendWait(clipboardContent);

        Kristian Sixhoej


        "Failure is not an option" - Gene Kranz

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        Hi! First of all, thanks for you reply. Unfortunately, neither the first nor the second version has any visible effect on the mstsc window.

        Regards, mav -- Black holes are the places where God divided by 0...

        1 Reply Last reply
        0
        • M mav northwind

          Hi! I've asked a similar question some time ago, but unfortunately without usable results, so I try again. I put some text into the clipboard and want to trigger a paste action inside a terminal server session (i.e. programmatically do what happens when you press Ctrl+V inside a mstsc window). Yes, I know that you can't programmatically see the different windows inside the WTS session and that the currently active application will be the target for my paste, but that's exactly what I want. So far I tried using SendKeys, P/invoke to SendInput and keybd_event, posting WM_PASTE or even WM_KEYDOWN and WM_KEYUP with the exact parameters Spy++ reports, but I can't get it to work. The first 4 ways simply give no visible result at all, with WM_KEYDOWN/UP I get a plain "v" instead of Ctrl+V. Does anyone have any further idea?

          Regards, mav -- Black holes are the places where God divided by 0...

          S Offline
          S Offline
          Skippums
          wrote on last edited by
          #4

          Why not do the following, where you know how to write a single char to the console (as indicated in your question where you said you can send a 'v' character):

          if (Clipboard.ContainsText()) {
          string ct = Clipboard.GetText();
          foreach (char c in ct)
          WriteCharToConsole(c);
          }

          I know it is a hack, but it will work. Sorry for not getting back to you on your original post, but I got kind of sidetracked. I still plan to look into how to do this the "right" way, but this will suffice until then.

          Sounds like somebody's got a case of the Mondays -Jeff

          M 1 Reply Last reply
          0
          • S Skippums

            Why not do the following, where you know how to write a single char to the console (as indicated in your question where you said you can send a 'v' character):

            if (Clipboard.ContainsText()) {
            string ct = Clipboard.GetText();
            foreach (char c in ct)
            WriteCharToConsole(c);
            }

            I know it is a hack, but it will work. Sorry for not getting back to you on your original post, but I got kind of sidetracked. I still plan to look into how to do this the "right" way, but this will suffice until then.

            Sounds like somebody's got a case of the Mondays -Jeff

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            Thanks, Jeff.

            Skippums wrote:

            Sorry for not getting back to you on your original post,

            No problem here - you're not obliged to solve my problems :) The main reason why this "hack" won't do, I'm afraid, is that the clipboard contains RTF... Nevertheless, thanks for the suggestion.

            Regards, mav -- Black holes are the places where God divided by 0...

            S 1 Reply Last reply
            0
            • M mav northwind

              Thanks, Jeff.

              Skippums wrote:

              Sorry for not getting back to you on your original post,

              No problem here - you're not obliged to solve my problems :) The main reason why this "hack" won't do, I'm afraid, is that the clipboard contains RTF... Nevertheless, thanks for the suggestion.

              Regards, mav -- Black holes are the places where God divided by 0...

              S Offline
              S Offline
              Skippums
              wrote on last edited by
              #6

              What if you read the RTF into the RichTextBox.Rtf property, then read it back out from the RichTextBox.Text property? This raises the hack level to about a 9 out of 10, but again, I think it may work for you. Also, isn't there a way to get RTF text from the clipboard without the formatting? You may want to check into that as well.

              Sounds like somebody's got a case of the Mondays -Jeff

              M 2 Replies Last reply
              0
              • S Skippums

                What if you read the RTF into the RichTextBox.Rtf property, then read it back out from the RichTextBox.Text property? This raises the hack level to about a 9 out of 10, but again, I think it may work for you. Also, isn't there a way to get RTF text from the clipboard without the formatting? You may want to check into that as well.

                Sounds like somebody's got a case of the Mondays -Jeff

                M Offline
                M Offline
                mav northwind
                wrote on last edited by
                #7

                :) Sure, there are easy ways to retrieve plain text from RTF, but that's not the point. The requirement is to paste the contents of a RichTextBox into the terminal server session, just like one would with a local application. Formatting etc. has to be retained. What I cannot understand is that I don't get the same results when sending the same messages with the same parameters Spy++ is recording...

                Regards, mav -- Black holes are the places where God divided by 0...

                1 Reply Last reply
                0
                • S Skippums

                  What if you read the RTF into the RichTextBox.Rtf property, then read it back out from the RichTextBox.Text property? This raises the hack level to about a 9 out of 10, but again, I think it may work for you. Also, isn't there a way to get RTF text from the clipboard without the formatting? You may want to check into that as well.

                  Sounds like somebody's got a case of the Mondays -Jeff

                  M Offline
                  M Offline
                  mav northwind
                  wrote on last edited by
                  #8

                  :-D Victory is mine! (as Stewie Griffin would say) Finally, I got some code from a guy in an MCE forum who was sending keystrokes to an mstsc window in response to IR remote control events. He also used keybd_event (which didn't work in my tests), but also used AttachThreadInput, so I think this was the missing piece. With this code I was able to send Ctrl+V to mstsc, thus triggering paste into the current application. Works like a charm! Anyway, thanks for your support!

                  Regards, mav -- Black holes are the places where God divided by 0...

                  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