SendInput to Notepad++ not working if clipboard is empty

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Umbracinis
Posts: 7
Joined: 02 Jun 2023, 10:08

SendInput to Notepad++ not working if clipboard is empty

23 Apr 2024, 12:47

Context: Windows 11 Pro x64 (Eng) v23H2
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)
}
To test this I clear the Windows clipboard by pressing "WIN + V" to summon the
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"
It basically runs a hidden "echo 1" on the Windows command line, and since in
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?
RussF
Posts: 1280
Joined: 05 Aug 2021, 06:36

Re: SendInput to Notepad++ not working if clipboard is empty

23 Apr 2024, 13:33

When I open Notepad++, the files that I had open when I closed it last are opened again and the tab for the last file used is highlighted. That is where the focus is, not in the edit window. Pasting anything to the tab will do nothing. That is why you must first click into the edit window.

That being said, why are you using the clipboard for such a simple task anyway? Unless you have plans to expand this to much larger things, you could simply use:

Code: Select all

^f1::Send(FormatTime(, "yyyy/MM/dd"))
to get what you want. I see that you are using Windows 11. That hotkey should work for most applications, but Windows' own Notepad has issues and you may want to change from Send to SendEvent. It will be a little slower, but will work with Notepad.

Russ
User avatar
Umbracinis
Posts: 7
Joined: 02 Jun 2023, 10:08

Re: SendInput to Notepad++ not working if clipboard is empty

24 Apr 2024, 13:58

Thank you for your reply @RussF. I can clarify a few things.
Pasting anything to the tab will do nothing. That is why you must first click
into the edit window.
Allow me to clarify. Here are my steps for replicating the problem I described:
  • 1 - Run the AutoHotKey code in my first post;
  • 2 - Open Windows clipboard app by pressing "WIN + V" and clear the clipboard there by clicking on the "Clear all" button;
  • 3 - Open Notepad++ and create a new document. Click inside this new document and type something;
  • 4 - Now press 'Ctrl + F1'. A date should have appeared. Did it? For me it doesn't, and that is my problem;
  • 5 - Now lets try in another program. Again, open Windows clipboard app by pressing "WIN + V" and clear the clipboard there by clicking on the "Clear all" button;
  • 6 - Open any other program (winword, Excel, Windows Notepad) and create a new document. Click inside this new document and type something;
  • 7 - Now press 'Ctrl + F1'. A date should have appeared. It does for me.
That being said, why are you using the clipboard for such a simple task anyway? Unless you have plans to expand this to much larger things, you could simply use:

Code: Select all

^f1::Send(FormatTime(, "yyyy/MM/dd"))
I have used that code that you mentioned in the past
(Send(FormatTime(, "yyyy/MM/dd"))), and it doesn't work properly with
Windows Notepad. Others have also reported problems with that approach here:

viewtopic.php?f=82&t=116789

Yes, I know, my problem is in Notepad++, not in Windows Notepad, and your
approach will fix my problem in Notepad++, but it will stop correctly working
in Windows Notepad. I am trying to find a solution that will work on both,
and everywhere else.

I just tried your approach again (Send(FormatTime(, "yyyy/MM/dd"))),
on an updated AutoHotkey, and it still doesn't work correctly (probably
due to Windows Notepad and not AutoHotkey). That is why I am using the
clipboard trick that I wrote in my last post, because it is more compatible.

However, I did try SendEvent(FormatTime(, "yyyy/MM/dd")) and although
slow, it does seem to work in Windows Notepad, Notepad++, and everywhere else,
so that may be a workaround. I will test it further.

Thank you @RussF for your time.

Umbracinis

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Dazzer123, jackman, mikeyww, Viola, Yahoo [Bot] and 26 guests