How to Insert Text at Current Caret Location?

This topic contains 7 replies, has 3 voices, and was last updated by  Richard 1 year ago.

  • Author
    Posts
  • #57143

    Richard
    Participant

    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

    Daan Weda
    Participant

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

    "test"|Set-Clipboard 
    Get-Clipboard
    • #57166

      Richard
      Participant

      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

    Max Kozlov
    Participant

    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

      Richard
      Participant

      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

    Richard
    Participant

    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

    Max Kozlov
    Participant

    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

      Richard
      Participant

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

You must be logged in to reply to this topic.