SendInput not buffering keypresses as expected?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
kunkel321
Posts: 1205
Joined: 30 Nov 2015, 21:19

SendInput not buffering keypresses as expected?

14 Oct 2023, 09:31

Hi Folks,
Maybe I'm just not understanding what "buffer" means(??) Please try running the below code... Now, with your right hand, "strum" the mnbv keys (which are in a row). This lets you press them in succession very quickly. My text string has 50 characters, and seems to take approx 1 sec to type out. I'm using SendInput, so I expect that the m should activate the code, then the nbv should be "held" in memory, and typed after the string is sent. Thus resulting in. 12345678901234567890123456789012345678901234567890nbv. Instead, I'm getting what you see in the block comment. The letters are interspersed with the numbers.

Am I misunderstanding what "buffer" means, or maybe doing something wrong??

I'm using RaptorX's Live Code tool, which appears to be running AHK v2.0.2.

Code: Select all

#Singleinstance
; AHK v2 

:*:m::
{
SendInput "12345678901234567890123456789012345678901234567890"
}

/*
123456789012345678901234567890123n45678901234567890bv
123456789012345n6789012345678901b2345678901234567890v
123456789n01234567890123456789012345678901234567890bv
1234n567890123456789b012345678901234567890123456v7890
1234567890123456789012n3456789012345678901234567890bv
ste(phen|ve) kunkel
RussF
Posts: 1328
Joined: 05 Aug 2021, 06:36

Re: SendInput not buffering keypresses as expected?

14 Oct 2023, 10:12

Well, according to the docs, you are interpreting it correctly:
SendInput and SendPlay: SendInput and SendPlay use the same syntax as SendEvent but are generally faster and more reliable. In addition, they buffer any physical keyboard or mouse activity during the send, which prevents the user's keystrokes from being interspersed with those being sent. SendMode can be used to make Send synonymous with SendInput or SendPlay. For more details about each mode, see SendInput and SendPlay below.
Have you tried SendPlay?

Could this be a bug?

Russ
User avatar
kunkel321
Posts: 1205
Joined: 30 Nov 2015, 21:19

Re: SendInput not buffering keypresses as expected?

14 Oct 2023, 12:17

Yeah, the doc files for SendPlay also suggest my interpretation is correct...
Like SendInput, SendPlay's keystrokes do not get interspersed with keystrokes typed by the user. Thus, if the user happens to type something during a SendPlay, those keystrokes are postponed until afterward.
Regarding using SendPlay, I'm not having any luck at all there... This (screenshot) means that my UAC is off -- correct?
Image
Also, I'm an Admin, but the string doesn't get sent. The hotstring trigger does disappear though... So the script is playing. Maybe my Windows installation has just gone wacky.

Joe Glines on the Automator group suggested just using paste.

Code: Select all

:*:m::
{
A_Clipboard := ""
A_Clipboard := "12345678901234567890123456789012345678901234567890"
SendInput "{Ctrl down}v{Ctrl up}"
}
For the purposes of this sample code, paste does work better, but for the actual project that I'm debugging, it doesn't fix it.
ste(phen|ve) kunkel
RussF
Posts: 1328
Joined: 05 Aug 2021, 06:36

Re: SendInput not buffering keypresses as expected?

14 Oct 2023, 14:03

Just out of curiosity, have you tried it in V1?

This sounds like a bug to me.

Russ
User avatar
kunkel321
Posts: 1205
Joined: 30 Nov 2015, 21:19

Re: SendInput not buffering keypresses as expected?

14 Oct 2023, 15:10

RussF wrote:
14 Oct 2023, 14:03
Just out of curiosity, have you tried it in V1?
Just tried...
Similar effect.

I'll post it in the bug subforum.

EDIT: Just posted there.
@RussF do you get a similar effect with the code?

An interesting thing... Not sure if that means anything, but if I start one key to the right, such that I'm pressing ,mnb then I do reliably get the expected ,12345678901234567890123456789012345678901234567890nb
ste(phen|ve) kunkel
RussF
Posts: 1328
Joined: 05 Aug 2021, 06:36

Re: SendInput not buffering keypresses as expected?

14 Oct 2023, 17:02

So, interesting....

On this particular laptop I have at home, I don't yet have V2 loaded (it's on my list, but, you know, honeydo's :) )

Anyway, in V1, it works perfectly no matter how quickly I "strum" the keys.

Hmmm. I have some things to do tonight, Steve, but I will endeavor to get this machine updated in the morning and test it in V2.

Russ
RussF
Posts: 1328
Joined: 05 Aug 2021, 06:36

Re: SendInput not buffering keypresses as expected?

15 Oct 2023, 08:40

Nope - just tried it in V2 - works as it should - in Notepad at least. I see @lexikos replied in the bug forum. Perhaps there is something else going on in your PC.

Russ
User avatar
kunkel321
Posts: 1205
Joined: 30 Nov 2015, 21:19

Re: SendInput not buffering keypresses as expected?

15 Oct 2023, 11:24

Thanks for checking, Russ. It certainly is a strange thing. Based on Lexikos' comments it occurred to me to try exiting all my other apps (including several ahk scripts), and trying again. The effect is way way less, but still occurs. It's strange, but when I do something like exit another app, or restart the above script, I'll often get several correct executions, followed by several with the characters interspersed.

I changed the script to use x as a trigger, and 'strummed' xcvb, since I'm actually left-handed. Also, I replaced the digits with 50 lower L characters to make the misfires easier to see. The below sample was typed into Notepad. The rate was a little over a second per line.

Code: Select all

llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
lclllllllllllllllllllllllllllllllllllllllllllllllllbv
llllllllllllllllllllllllllllllllcllllllllllllllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
clvlllllllllllllllllllllllllllllbllllllllllllllllllll
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllcllbv
llllllllllllllllllllllllllllllllllllllllllllllllcllvb
llllllllllllllllllllllllllllllllllllcllllllllllllllvb
lllllllllllllllllllllllllllllllllllllllllllllllllclbv
llllllllllllllllllllllllllllllllllllllllllllcllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
lllllllllllllllllllllllllllllllllllllllllllclllllllbv
lllllllllllllllllllllclllllllllllllllllllllllllllllvb
llllllllllllllllllllllllllllllllllllllllllllcllllllvb
llclllllllllllllllllllllllllllllllllvlllllllllllllllb
llllllllllllllllllllllllllllllllllcllllllllllllllllvb
clvlllllllllllllllllllllllllllllllllllllllllllllllllb
llllllllllllllllllllllllllllllllllllllllllllllllllcbv
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
lllllllllllllllllllllllllllllllllllllllllllllclllllvb
lllllllllllllllllllllllllllllllllclllllllllllllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllcllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllcllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllcllllllllllllllllvb
lllllllllllllllllllllllllllllllllllllllllllclllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
lllllllllllllllllllllllllllllllllllllllllllclllllllvb
llllllllllllllllllllllllllllllcllllllllllllllllllllvb
lllllllllllllllllllllllllllllllllllllllllllllclllllvb
llllllllllllllllllllllllllllllllllllllllllllcllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
lllllllllllllllllllllllllllllllllllllllllllllllclllvb
llllllllllllllllllllllllllllllcllllllllllllllllllllvb
lllllllllllllllllllllllllllllllllllllllllllclllllllvb
llllllllllllllllllcllllllllllllllllllllllllllllllllvb
llllllllllllllllllllllllllllllllllllllllllllcllllllvb
llllllllllllllllllllllllllllllllllllllllllllcllllllvb
lllllllllllllllllllllllllllllllllllllllllllllclllllvb
lllllllllllllllllllllllllllllllllclllllllllllllllllvb
lllllllllllllllllllllllllllllllllllllllclllllllllllvb
llllllllllllllllllllllllllllllllcllllllllllllllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcbv
lclllllllllllllllllllllllllllllllllllllllllllllllllvb
llllllllllllllllllllllllllllllllllllllllllllllcllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
lllllllllllllllllllclllllllllllllllllllllllllllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
lllllllllllllllllllllllllllllllllllllllllllllclllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllcllllllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllvbc
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
llllllllllllllllllllllllllllllllllcllllllllllllllllvb
llllllllllllllllllllllllllllllllllllllllllllllllllcvb
you can see how it starts with several correct executions, then consistent misfires that are interspersed with correct executions.

I also tried in Adventure, VSCode, TED Notepad, and an Outlook email. The effects were similar in all. Also updated my keyboard driver and restarted Windows.

EDIT: I asked ChatGPT4 for advice. The results were actually pretty good! For entertainment purposes, here is my meandering prompt, and the ai feedback:
Prompt:
Spoiler
Feedback:
Spoiler

Additional Update: I restarted Win 10 in Safe Mode, then double-clicked the script (which opens it for editing in VScode, per my settings). Then ran it from there. It worked 100% flawlessly! I then increased the length of my string from 50 to 300. With the longer string, the keyboard buffer took a couple of "pushes" to "push out" and send all of the text, but again, the three manually-typed characters appeared after the string on 100% of many trials. (Tried in VSCode and in TED Notepad).

So presumably I have another app that is "winning" in the battle for the keyboard hook (???) I dunno. I'll post again if I learn more.
----------

Another update: Well I exited everything I could on the Win Systray, then opened Task Manager and killed all the Google stuff, Windows Defender and several "Windows Security" things... Still the misfires persist. I dunno..

----------
Update: Just some notes for the curious... I restarted in safe mode again. The script then worked as exprected (again). I wanted to take a screenshot of what was in Task Manager, so I could compare it with Task Manager after a normal restart. Apparently neather the Windows Snipping Tool, nor SnagIt will take a screenshot of TaskMan when in safe mode -- Arghhhh....

Then (still in safe mode) I started running different apps, and systematically tried to see if any would cause propblems with the SendInput test script. I have two "MasterScripts" which contain tons of little (on not so little) sub-scripts. One of them is v1 code and contains the sub-scripts which I haven't gotten around to converting to v2, to put in my v2 MasterScript. Anyhow... Running either one of these would "break" the test script (presumably "stealing" the keyboard hook). I then systematically started commenting-out different portions of my v1 masterscript, to see if there was some particular section that was problematic. I eventually commented-out everything but the autoexe section (about 5 lines at the top), but the test script was still broken. At that point, exited the v1 masterscript, but the keyboard hook problem was still there. Then I got frustrated and quit.
ste(phen|ve) kunkel
lexikos
Posts: 9706
Joined: 30 Sep 2013, 04:07
Contact:

Re: SendInput not buffering keypresses as expected?

16 Oct 2023, 00:11

Running either one of these would "break" the test script
Of course.
If a script other than the one executing SendInput has a low-level keyboard hook installed, SendInput automatically reverts to SendEvent (or SendPlay if SendMode "InputThenPlay" is in effect). This is done because the presence of an external hook disables all of SendInput's advantages, making it inferior to both SendPlay and SendEvent. However, since SendInput is unable to detect a low-level hook in programs other than AutoHotkey v1.0.43+, it will not revert in these cases, making it less reliable than SendPlay/Event.

I eventually commented-out everything but the autoexe section (about 5 lines at the top)
Something like #InstallKeybdHook? :roll:
lexikos wrote:
14 Oct 2023, 20:19
In practice, input is not buffered if any keyboard hooks are present. This is why AutoHotkey uninstalls its own keyboard hook, or falls back to SendEvent/SendPlay if it can detect that another script has a keyboard hook.
User avatar
kunkel321
Posts: 1205
Joined: 30 Nov 2015, 21:19

Re: SendInput not buffering keypresses as expected?

16 Oct 2023, 10:26

lexikos wrote:
16 Oct 2023, 00:11
I eventually commented-out everything but the autoexe section (about 5 lines at the top)
Something like #InstallKeybdHook? :roll:
LOL. A logical conclusion, but no. I don't think #InstallKeybdHook or InstallKeybdHook is used in any of my scripts. Your reply did prompt me to try both in my own (v1, then v2) scripts. Alas, no joy.

Lexikos (or others), regarding:
However, since SendInput is unable to detect a low-level hook in programs other than AutoHotkey v1.0.43+
Does this imply that AutoHotkey is generally able to detect keyboard hooks in other AutoHotkey scripts, and uninstall them when needed?

Also, should restarting a script (one which uses keyboard hooks), usually cause it to again take precedence with respect to sending instructions to the windows keyboard hook? (Assuming that the other script does not utilize a loop to constantly reestablish a keyboard hook, or somehow establish the hook at a 'lower level').

Hopefully these are not dumb questions... I'm actually not a programmer at all.
ste(phen|ve) kunkel
lexikos
Posts: 9706
Joined: 30 Sep 2013, 04:07
Contact:

Re: SendInput not buffering keypresses as expected?

17 Oct 2023, 00:18

Does this imply that AutoHotkey is generally able to detect keyboard hooks in other AutoHotkey scripts
Yes.
and uninstall them when needed?
No.

The documentation explicitly states what happens when there are hooks installed by other scripts. It's emphasized in the quote I posted.
Also, should restarting a script (one which uses keyboard hooks), usually cause it to again take precedence with respect to sending instructions to the windows keyboard hook?
The script does not "send instructions to the windows keyboard hook". There is not one "windows keyboard hook". Each script may have its own keyboard hook, to which the system sends events. The most recently installed hook is called first for each event, and then if it calls CallNextHookEx, the next hook is called. As for the reliability of SendInput, the order that the hooks are called in is not relevant.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Draken and 21 guests