Send ^v does not work

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Wigi
Posts: 140
Joined: 05 Jun 2017, 10:52
Contact:

Send ^v does not work

15 Aug 2018, 08:10

Hi all,

I have an issue with pasting the clipboard. My code to comment other code with Ctrl-k almost works but pasting the result gives incorrect output.
I have this code:

Code: Select all

^k::
{
    clipboard =
    Send ^c
    ClipWait, 1

    if ErrorLevel <> 0
        return

    NewClip =
    WaitingForFirstCharOfLine = y
    AutoTrim, off

    nLines:= CountLines(clipboard)

    if(nLines = 1)
       {
          ss = % Substr( clipboard, 1, 1 )
          If ( ss = "#" )
             SendInput, {Home}{#}
          Else
             SendInput, {Home}{#}{Space}
       }
    else
    {
        NewClip = `#

        Loop, parse, clipboard
        {
            if A_LoopField = `n
                NewClip = %NewClip%%A_loopField%
            else
                NewClip = %NewClip%%A_LoopField%
        }

        clipboard = %NewClip%
        msgbox %NewClip%
        Send ^v

    }
}
So for example. Change this:

Code: Select all

   If( DimensionExists( pDim ) > 0 );

      vDim = Dimnm( '}Dimensions', Dimix( '}Dimensions', pDim ));
      DimensionElementInsertDirect( vTempDimForDims, '', vDim, 'N' );

   ElseIf( CubeExists( pDim ) > 0 );
to:

Code: Select all

#   If( DimensionExists( pDim ) > 0 );
#
#      vDim = Dimnm( '}Dimensions', Dimix( '}Dimensions', pDim ));
#      DimensionElementInsertDirect( vTempDimForDims, '', vDim, 'N' );
#
#   ElseIf( CubeExists( pDim ) > 0 );
# is the commentary sign in the program at hand.

With msgbox I can see that the clipboard is fine but pasting the result fails. The result does not include the # character and removes spaces.

See 3 pictures attached please.

Wim
Attachments
03.png
03.png (4.35 KiB) Viewed 948 times
04.png
04.png (13.59 KiB) Viewed 948 times
05.png
05.png (4.14 KiB) Viewed 948 times
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Send ^v does not work

15 Aug 2018, 09:15

Hallo,
you have not given function CountLines()
Wigi
Posts: 140
Joined: 05 Jun 2017, 10:52
Contact:

Re: Send ^v does not work

15 Aug 2018, 11:46

Hello,

I will add it later today. But msgbox just before pasting the clipboard confirms the contents of the clipboard. See the image. I’m surprised that it does not paste correctly.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Send ^v does not work

15 Aug 2018, 11:54

this is what ur else branch produces for me

Code: Select all

Clipboard := "
(
   If( DimensionExists( pDim ) > 0 );

      vDim = Dimnm( '}Dimensions', Dimix( '}Dimensions', pDim ));
      DimensionElementInsertDirect( vTempDimForDims, '', vDim, 'N' );

   ElseIf( CubeExists( pDim ) > 0 );
)"

AutoTrim off

NewClip = `#

Loop, parse, clipboard
{
   if A_LoopField = `n
      NewClip = %NewClip%%A_loopField%
   else
      NewClip = %NewClip%%A_LoopField%
}

msgbox % clipboard := NewClip

/*
#   If( DimensionExists( pDim ) > 0 );

      vDim = Dimnm( '}Dimensions', Dimix( '}Dimensions', pDim ));
      DimensionElementInsertDirect( vTempDimForDims, '', vDim, 'N' );

   ElseIf( CubeExists( pDim ) > 0 );
*/
this is a way to write # before each line:

Code: Select all

Clipboard := "
(
   If( DimensionExists( pDim ) > 0 );

      vDim = Dimnm( '}Dimensions', Dimix( '}Dimensions', pDim ));
      DimensionElementInsertDirect( vTempDimForDims, '', vDim, 'N' );

   ElseIf( CubeExists( pDim ) > 0 );
)"

for each, Line in StrSplit(Clipboard, "`n", "`r")
   commented .= "# " Line "`n"

msgbox % clipboard := commented

/*
#    If( DimensionExists( pDim ) > 0 );
# 
#       vDim = Dimnm( '}Dimensions', Dimix( '}Dimensions', pDim ));
#       DimensionElementInsertDirect( vTempDimForDims, '', vDim, 'N' );
# 
#    ElseIf( CubeExists( pDim ) > 0 );
*/
Wigi
Posts: 140
Joined: 05 Jun 2017, 10:52
Contact:

Re: Send ^v does not work

15 Aug 2018, 12:33

Great, I will adapt and test. Thanks
Wigi
Posts: 140
Joined: 05 Jun 2017, 10:52
Contact:

Re: Send ^v does not work

16 Aug 2018, 01:46

Hi,

Here is that custom function:

Code: Select all

CountLines(Text)
{
     StringReplace, Text, Text, `n, `n, UseErrorLevel
     Return ErrorLevel + 1
}
Wigi
Posts: 140
Joined: 05 Jun 2017, 10:52
Contact:

Re: Send ^v does not work

16 Aug 2018, 04:03

Many thanks both !

In fact, I missed a Return command and subsequent coding was pasting the text without spaces and such. My bad.

Here is the code to comment (Ctrl-k) and Uncomment (Ctrl-k). You have to select the text first before launching the hotkey.

Code: Select all

^k::
{
    NewClip = 
    clipboard =
    Send ^c
    ClipWait, 1

    if ErrorLevel <> 0
        return

    AutoTrim, off

    for each, Line in StrSplit(Clipboard, "`n", "`r")
    {
       if ( Line = "" )
          NewClip .= Line "`r`n"
       else
          NewClip .= "# " Line "`r`n"
    }
    StringTrimRight, NewClip, NewClip, 2

    clipboard = %NewClip%
    Send ^v
    Return
}

^+k::
{
    clipboard =
    Send ^c

    ClipWait, 1

    if ErrorLevel <> 0
        return

    NewClip =
    WaitingForFirstCharOfLine = y
    AutoTrim, off

    for each, Line in StrSplit(Clipboard, "`n", "`r")
    {
       StringTrimLeft, Line, Line, 2
       NewClip .= Line "`r`n"
    }
    StringTrimRight, NewClip, NewClip, 2

    clipboard = %NewClip%
    Send ^v
    Return
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 109 guests