Reputation: 220
I just downloaded AutoHotkey and modified a script I found online, but the problem is that it works consistently only in notepad. In a Windows program, such as an HTML editor or in a textarea in Firefox, it rarely works (it seems to work once only after the 2nd try).
The script functionality is really simple: Copy the selected text to the clipboard, insert the the text that I specify at the beginning of it, along with a new line, and at the end of it, a new line, and the text that I specify.
This is the original script, which also doesn't work consistently in the other programs I mentioned. What it does is similar to what I want to do above, except that it doesn't insert any new lines, and it asks for what text to insert before and after the text.
Original Script:
#i::
clipsaved:= ClipboardAll
Send, ^c
WinGetTitle, CurrentWinTitle
InputBox, inputVar, Input character, Input character wHich will surround the text.
clip := Clipboard
clip = %inputVar%%clip%%inputVar%
Clipboard := clip
WinActivate, %CurrentWinTitle%
Send, ^v
Clipboard := clipsaved
return
Modified Script: The one I want to work
#+c::
clipsaved:= ClipboardAll
Send, ^c
WinGetTitle, CurrentWinTitle
; InputBox, inputVar, Input character, Input character wHich will surround the text.
clip := Clipboard
; clip = %inputVar%%clip%%inputVar%
clip = /*`r`n%clip%`r`n*/
Clipboard := clip
WinActivate, %CurrentWinTitle%
Send, ^v
Clipboard := clipsaved
return
So, what's wrong here? Also, why is it capturing something initially to the clipboard rather than Ctrl+C, and what is it doing at the end? Also, what's the purpose of it knowing where to activate (WinActivate) using the Windows Current Title (%CurrentWinTitle%).
Upvotes: 0
Views: 2234
Reputation: 1015
[EDIT: see the script I provided in my other answer that uses the ClipCopy and ClipPaste functions, for a more robust solution. I keep this script here because it has some comments and features that may be useful for other closely-related problems.]
Usually in AutoHotkey a script simply works or doesn't work. However with certain keyboard combinations a bit of trial and error is required to get the script to run smoothly. Two methods are shown, setting the keys to Up
or using KeyWait
.
Using the hWnd rather than the window title, to identify the window, is more reliable.
Setting the clipboard contents to empty and using the ClipWait
command, helps to ensure that the copying delay is neither too short nor too long, unfortunately there is not a similar command to determine how long to wait for the paste to complete, this is trial and error.
In the script below, ClipWait
is set to 5, it will wait for a maximum of 5 seconds, and if the clipboard copying process has not completed, the clipboard's original contents will be restored.
#+c::
WinGet, hWnd, ID, A
clipsaved := ClipboardAll
Clipboard := ""
;method 1
Send, {Win Up}{Shift Up}{c Up}
;method 2
;KeyWait, Shift
;KeyWait, LWin
;KeyWait, RWin
Send, ^c
ClipWait, 5
if ErrorLevel
{
Clipboard := clipsaved
clipsaved := ""
return
}
; InputBox, inputVar, Input character, Input character wHich will surround the text.
clip := Clipboard
; clip := inputVar clip inputVar
clip := "/*`r`n" clip "`r`n*/"
Clipboard := clip
WinActivate, % "ahk_id " hWnd
Send, ^v
Sleep, 3000
Clipboard := clipsaved
return
Upvotes: 0
Reputation: 1015
In AutoHotkey sometimes there are problems when keys that trigger a script, and keys that are sent by a script, interfere, especially when modifier keys such as Shift/Ctrl/Win/Alt are involved. Quite often this type of problem, with either keyboard or mouse presses, gives even experts difficulty and requires a lot of trial and error.
The custom ClipCopy and ClipPaste functions in the link below, try to counteract these problems as much as they can. I have used these functions in the script below, and provide the functions themselves underneath for a complete working script that should achieve your objective.
Robust copy and paste routine (function) - Scripts and Functions - AutoHotkey Community https://autohotkey.com/board/topic/111817-robust-copy-and-paste-routine-function/
Note: using the hWnd rather than the window title, to identify the window, is more reliable. Also, clip := ClipCopy(1)
would cut rather than copy the text.
-
;==================================================
#+c::
WinGet, hWnd, ID, A
clipsaved := ClipboardAll
clip := ClipCopy() ; will copy selected text via control + c
; InputBox, inputVar, Input character, Input character wHich will surround the text.
; clip = %inputVar%%clip%%inputVar%
clip = /*`r`n%clip%`r`n*/
WinActivate, ahk_id %hWnd%
ClipPaste(clip)
Clipboard := clipsaved
return
;==============================
ClipCopy(piMode := 0)
{
clpBackup := ClipboardAll
Clipboard=
if (piMode == 1)
sCopyKey := "vk58sc02D" ; Cut
else
sCopyKey := "vk43sc02E" ; Copy
SendInput, {Shift Down}{Shift Up}{Ctrl Down}{%sCopyKey% Down}
ClipWait, 0.25
SendInput, {%sCopyKey% Up}{Ctrl Up}
sRet := Clipboard
Clipboard := clpBackup
return sRet
}
;==============================
ClipPaste(ByRef psText)
{
if (psText != "")
{
clpBackup := ClipboardAll
sPasteKey := "vk56sc02F" ; Paste
Clipboard := psText
SendInput, {Shift Down}{Shift Up}{Ctrl Down}{%sPasteKey% Down}
; wait for clipboard is ready
iStartTime := A_TickCount
Sleep, % 100
while (DllCall("GetOpenClipboardWindow") && (A_TickCount-iStartTime<1400)) ; timeout = 1400ms
Sleep, % 100
SendInput, {%sPasteKey% Up}{Ctrl Up}
Clipboard := clpBackup
}
}
;==================================================
Upvotes: 2
Reputation: 15508
This should work fine:
(I commented most of the code to answer some of your questions.)
#+c::
clipsaved:= ClipboardAll ;makes a backup of the current clipboard content and stores it in the variable "clipsaved"
Send, {Win Up}{Shift Up}{c Up} ;release the windows key, shift and c key. otherwise things like ctrl+c might mess up
Send, ^c ;overwrite the clipboard with the current text selection
Sleep, 100 ; wait for 100 milliseconds to make sure the new clipbaord contents are loaded
WinGetTitle, CurrentWinTitle
InputBox, inputVar, Input character, Input character wHich will surround the text.
Clipboard = %inputVar%`r`n%Clipboard%`r`n%inputVar%
;Clipboard = `r`n%Clipboard%`r`n
WinActivate, %CurrentWinTitle%
Send, ^v
Sleep, 100
Clipboard := clipsaved ;restore the original clipboard contents that where saved to the "clipsaved" variable in the beginning
Return
The Sleeps may or may not be necessary, depending on the application and your CPU load...
Upvotes: 0