Callback function help Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
LAPIII
Posts: 669
Joined: 01 Aug 2021, 06:01

Callback function help

Post by LAPIII » 11 Feb 2023, 14:25

I had converted this script from version 1 to version 2 and now am getting an error about this function:

Code: Select all

^+n::

{
Run("`"C:\Program Files\Notepad++\notepad++.exe`"")
WinWait("ahk_exe notepad++.exe")
Sleep(100)
Send("!{Tab}")
OnClipboardChange ClipChanged
}
ClipChanged() {
	ControlSend("^v`n", "Scintilla1", "ahk_exe Notepad++.exe")
}

#HotIf WinActive("ahk_exe notepad++.exe", )
Esc::
{
MsgBox("BYE")
ExitApp()
}
Last edited by LAPIII on 24 Feb 2024, 03:11, edited 1 time in total.

User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Callback function help

Post by boiler » 11 Feb 2023, 14:39

The callback function for OnClipboardChange requires one parameter, even if you choose not to use it in the function.

LAPIII
Posts: 669
Joined: 01 Aug 2021, 06:01

Re: Callback function help

Post by LAPIII » 24 Feb 2024, 03:25

In the latest Notepad++ v8.6.2 this no longer works. I know I didn't change the code in my script because it works perfectly in WordPad. What's happening is that when I switch to another app and copy then it will only send Enter. I tried running the script as admin and using two ControlSends, one with ^v and the other with {Enter}, neither method helped.

User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Callback function help

Post by boiler » 24 Feb 2024, 07:07

It seems to now only accept pasted text if the window is active. So you could do this where it temporarily activates the Notepad++ window before returning focus to the window you were in:

Code: Select all

ClipChanged(*) {
	CurrID := WinExist("A")
	WinActivate "ahk_exe Notepad++.exe"
	ControlSend("^v`n", "Scintilla1", "ahk_exe Notepad++.exe")
	Sleep 100
	WinActivate CurrID
}

Or just change this one line to send the contents of the clipboard instead of pasting them:

Code: Select all

	ControlSend(A_Clipboard "`n", "Scintilla1", "ahk_exe Notepad++.exe")

LAPIII
Posts: 669
Joined: 01 Aug 2021, 06:01

Re: Callback function help

Post by LAPIII » 24 Feb 2024, 10:07

Thank you very much! I like your second snippet best because it works in the background, but it will only create a line every second paste. Do you know what could fix this?

User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Callback function help

Post by boiler » 24 Feb 2024, 10:13

Mine works every time, but perhaps it needs a delay between sending the clipboard contents and the new line on your machine, so try this:

Code: Select all

ClipChanged(*) {
	ControlSend(A_Clipboard, "Scintilla1", "ahk_exe Notepad++.exe")
	Sleep 100
	ControlSend("`n", "Scintilla1", "ahk_exe Notepad++.exe")
}

LAPIII
Posts: 669
Joined: 01 Aug 2021, 06:01

Re: Callback function help

Post by LAPIII » 24 Feb 2024, 10:37

I get an error about the fourth line that says: Error: Target control not found..

EDIT: This is my solution:

Code: Select all

ClipChanged(*) {
	ControlSend("{Enter} " A_Clipboard, "Scintilla1", "ahk_exe Notepad++.exe")
}
`n Actually would take a few spaces before each paste.
Last edited by LAPIII on 24 Feb 2024, 11:01, edited 2 times in total.

User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Callback function help

Post by boiler » 24 Feb 2024, 10:50

LAPIII wrote: I get an error about the fourth line that says: Error: Target control not found..
Perhaps you left a comma out in trying to implement that. If you copy the whole function directly and replace yours, it should produce no error as long as Notepad++ is open.

LAPIII
Posts: 669
Joined: 01 Aug 2021, 06:01

Re: Callback function help  Topic is solved

Post by LAPIII » 24 Feb 2024, 10:56

No boiler, I copied and pasted your snippet perfectly. My solution:

Code: Select all

ClipChanged(*) {
	ControlSendText(A_Clipboard "`r`n", "Scintilla1", "ahk_exe Notepad++.exe")
}
This will not only work in the background but will eliminate pasting nothing when copying a modifier symbol, functions such as ^v or {Enter}

Post Reply

Return to “Ask for Help (v2)”