How to Insert Text at Current Caret Location?

Welcome Forums General PowerShell Q&A How to Insert Text at Current Caret Location?

Viewing 4 reply threads
  • Author
    Posts
    • #57143
      Participant
      Topics: 2
      Replies: 4
      Points: 0
      Rank: Member

      I can copy a string to the clipboard using a pipe and calling the clip program, such as | clip.exe, but how can I paste at the current caret location? All I need is to paste a value at the current location. Thank you for your help!

    • #57146
      Participant
      Topics: 0
      Replies: 31
      Points: 21
      Rank: Member

      Why do you want to do that?
      Maybe you could use the get and set clipboard.

      "test"|Set-Clipboard 
      Get-Clipboard
      • #57166
        Participant
        Topics: 2
        Replies: 4
        Points: 0
        Rank: Member

        Neither of these are recognized by my Powershell. I am using Powershell V2 and unfortunately I can't upgrade. Ultimately I am trying to read a COM port then paste the result at the current location in Excel or Notepad, etc. Everything else is working, just this last thing is holding me up. Thank you for your help!

    • #57172
      Participant
      Topics: 2
      Replies: 376
      Points: 0
      Rank: Member

      PSv2:

      function Get-ClipBoard {
      	Add-Type -AssemblyName System.Windows.Forms
      	$tb = New-Object System.Windows.Forms.TextBox
      	$tb.Multiline = $true
      	$tb.Paste()
      	$tb.Text
      }
       
      function Set-ClipBoard {
      	param (
      		[String][Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] $Text
      	)
      	Add-Type -AssemblyName System.Windows.Forms
      	$tb = New-Object System.Windows.Forms.TextBox
      	$tb.Multiline = $true
      	$tb.Text = $text
      	$tb.SelectAll()
      	$tb.Copy()
      }
      
      • #57220
        Participant
        Topics: 2
        Replies: 4
        Points: 0
        Rank: Member

        Thank you for your help! This is really close, but it pastes in the command window instead of caret location. Hopefully I'm using it right. Here is the full script I'm using to test these functions.

        function Get-ClipBoard {
        	Add-Type -AssemblyName System.Windows.Forms
        	$tb = New-Object System.Windows.Forms.TextBox
        	$tb.Multiline = $true
        	$tb.Paste()
        	$tb.Text
        }
        
        function Set-ClipBoard {
        	param (
        		[String][Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] $Text
        	)
        	Add-Type -AssemblyName System.Windows.Forms
        	$tb = New-Object System.Windows.Forms.TextBox
        	$tb.Multiline = $true
        	$tb.Text = $text
        	$tb.SelectAll()
        	$tb.Copy()
        }
        
        Set-Clipboard("Testing again: 1, 2, 3...")
        Start-Sleep -s 2
        Get-Clipboard
        Get-Clipboard
        Get-Clipboard
        Write-Host "Done!"
        

        During the 2 second delay I change cursor location to Notepad but the clipboard contents paste into the command window. Thanks again!

    • #57233
      Participant
      Topics: 2
      Replies: 4
      Points: 0
      Rank: Member

      Ok, I found a work around. The best way for me to paste at the cursor location is to send the CTRL+v keys directly. Here is a MWE. Thank you again for your help!

      # Load System.Windows.Forms to use SendKeys
      [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
      
      # Copy some text to the clipboard
      "Testing again: 1, 2, 3..." | clip.exe
      
      # Delay for 2 seconds while moving the cursor
      Start-Sleep -s 2
      
      # Paste using CTRL+v 3 times just for verification
      [System.Windows.Forms.SendKeys]::SendWait("^{v}") 
      [System.Windows.Forms.SendKeys]::SendWait("^{v}") 
      [System.Windows.Forms.SendKeys]::SendWait("^{v}") 
      
      # Finish up with the command window
      Write-Host "Done!"
      Read-Host "Press enter to close this window..."
      
    • #57299
      Participant
      Topics: 2
      Replies: 376
      Points: 0
      Rank: Member

      If you feel yourself capable to understand this topic
      http://stackoverflow.com/questions/2556872/how-to-set-foreground-window-from-powershell-event-subscriber-action

      you can select notepad window automatically 😉

      and there is alot of modules and examples about direct working with excel thru COM

      • #57362
        Participant
        Topics: 2
        Replies: 4
        Points: 0
        Rank: Member

        Thank you for the reference! I'll check it out.

Viewing 4 reply threads
  • The topic ‘How to Insert Text at Current Caret Location?’ is closed to new replies.