OS build: 22631.3447
AutoHotkey x64 v2.0.13.0
Notepad++ v8.6.5 64-bit
Greetings.
I wrote a script that does multiple tasks, one of them uses the clipboard to
type the current date. It saves the current Windows clipboard, then replaces
it with the current date, pastes the date, and then restores the previously
saved clipboard. Very easy, very standard, lots of examples on the forum. That
part of the script works fine on all apps I have tried so far.
But, on Notepad++, IF the clipboard is completely empty, then it
does not work. It works with an empty Windows clipboard (or if there is
anything in the clipboard) on all other programs I have tested it , but in
Notepad++ it only works if I have something already copied into the Windows
clipboard, not an empty one.
Here is my code:
Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
#Warn
Persistent
fDateTimeSignature(vTmp)
{
vClipSaved := ClipboardAll()
A_Clipboard := vTmp
;MsgBox "Test." ; This works after closing the msgbox
;Sleep 5000 ; This works if you click in window before sleep ends
;RunWait A_ComSpec " /c echo 1 > nul",,"Min" ; This works
SendInput "^v"
Sleep 100 ; Wait long enough to just issue the command 'SendInput ^v'
A_Clipboard := vClipSaved
vClipSaved := ""
}
Ctrl & F1::
{
vCurrentDateTime := FormatTime(, "yyyy/MM/dd")
fDateTimeSignature(vCurrentDateTime)
}
windows clipboard app and clear the clipboard there. I then go to Notepad++ and
hit 'Ctrl + F1'. Nothing.
I have found out, however, that if I add a 'msgbox' right before the 'SendInput'
line, then it does work. So I theorized that it was because Windows was
leaving the Notepad++ window to grant focus on the msgbox, and once that was
closed it returned focus to Notepad++, and that worked.
So I then replaced the 'msgbox' with a 'Sleep 5000' (5 second wait), and after
the 5 second Sleep period it still doesn't work, BUT, if during Sleep period I
click inside Notepad++ (even though it has never lost focus), then when the
sleep period ends it works (it pastes the date string).
Based on all this I have created an ugly hack that does work. Right before the
'SendInput' line, I added this line:
Code: Select all
RunWait A_ComSpec " /c echo 1 > nul",,"Min"
order to do so it shifts the focus from whatever app it is on (including
Notepad++) to that command and then back to the previous app when it is
finished, then it works. And since no command line windows open up and it
happens so quickly, the only thing you see is a very quick flicker of the app
you are on, as it looses focus (to run that command) and regains focus (once
the command finishes).
Yes, I know what you are thinking: just make sure to copy something into the
clipboard before using the hotkeys and forget this ugly hack. But it isn't an
elegant solution, and I have spent OVER 4 HOURS trying to fix this and it has
become somewhat personal, so now I decided to ask for help. Does anyone know
a better way to fix this?