script for "play paste" the clipboard causes AHK bug

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
mikagenic
Posts: 93
Joined: 16 Sep 2014, 18:26

script for "play paste" the clipboard causes AHK bug

13 Feb 2024, 22:20

I wrote the following script to send the content of the clipboard one char at a time with a short delay. I need this to paste in places where ctrl+v doesn't work (ie password fields etc):

Code: Select all

^p:: {
  clip := A_Clipboard
  Loop parse, clip
  {
      SendText A_LoopField
      sleep 10
  }
}


This works however after a few uses I start to suddenly see very strange effects, it is as if the control stays pressed and is not released. Sometimes pressing control a few times solves it but other times I have to exit AHK and start it again ! what am I doing wrong here ?
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: script for "play paste" the clipboard causes AHK bug

13 Feb 2024, 22:30

You might try adding this as the first line in your function:

Code: Select all

KeyWait 'Ctrl'
mikagenic
Posts: 93
Joined: 16 Sep 2014, 18:26

Re: script for "play paste" the clipboard causes AHK bug

13 Feb 2024, 23:19

I will try but this seems like a hack. Why is this happening ? is this a bug in AHK ? if yes where do I report it ?
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: script for "play paste" the clipboard causes AHK bug

13 Feb 2024, 23:32

You can report potential bugs in the Bug Reports sub-forum.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: script for "play paste" the clipboard causes AHK bug

14 Feb 2024, 05:33

I would have a look at your :arrow: KeyHistory to see what is happening with your keys.

Code: Select all

#Requires AutoHotkey v2.0

^p Up:: {
 KeyWait 'Ctrl'
 SendEvent '{Text}' A_Clipboard
}
Yes, unexpected things can sometimes happen when you hold Ctrl or other modifiers while text is sent. It is not necessarily a bug but might relate to the timing of sending a long string of text, with programmed delays, while you are pressing other keys at the same time. Again, KeyHistory will show. Software conflicts may also arise with other programs.

The script above has several differences.
  1. Simpler
  2. Waits for both keys to be released: often fixes the problem
  3. Uses text mode to avoid interpreting special characters that may be on the clipboard
  4. Uses SendEvent, which has a similar effect (though not entirely identical) to sleeping for 10 ms after each character or key
  5. Uses no additional variables because such variables are not needed
A general rule of thumb about text mode is that if you are using Send to send variable contents without knowing whether they contain special characters (e.g., "#", "!", "+", "^"), and you do not want the special characters to be interpreted, use text mode. There are some additional situations where text mode is also useful. The documentation contains additional details.
If SetKeyDelay is not used, the default delay is 10 for the traditional SendEvent mode and -1 for SendPlay mode. The default press duration is -1 for both modes. SetKeyDelay is not obeyed by SendInput; there is no delay between keystrokes in that mode. This same is true for Send when SendMode Input is in effect. A short delay (sleep) is done automatically after every keystroke sent by Send or ControlSend. This is done to improve the reliability of scripts because a window sometimes can't keep up with a rapid flood of keystrokes.
During the delay (sleep), the current thread is made uninterruptible.
mikagenic
Posts: 93
Joined: 16 Sep 2014, 18:26

Re: script for "play paste" the clipboard causes AHK bug

14 Feb 2024, 16:13

Thanks @mikeyww ! your script is indeed better, I will test it today and see if it solves the issue.

That said I want to ask a more general question - why the need for the Up and for KeyWait ? shouldn't it be that once a subroutine is entered it is _guaranteed_ that the hotkey itself is released ?

That is to me when doing

Code: Select all

^p:: {
  ; if ctrl is pressed here this is a bug in AHK
}
[Mod edit: Added [code][/code] tags.]
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: script for "play paste" the clipboard causes AHK bug

14 Feb 2024, 19:23

mikagenic wrote: shouldn't it be that once a subroutine is entered it is _guaranteed_ that the hotkey itself is released ?
No, it shouldn't because AHK has features specifically for when keys are pressed vs. when they are released. Hotkeys fire as soon as they're pressed not when they're released, unless you specify that. Try this script and hold down the A key for a bit before you release it:

Code: Select all

#Requires AutoHotkey v2.0

a::SoundBeep 600

a up::MsgBox 'Released'
mikagenic
Posts: 93
Joined: 16 Sep 2014, 18:26

Re: script for "play paste" the clipboard causes AHK bug

15 Feb 2024, 01:37

@boiler ] @mikeyww Thanks

OK so that makes me understand why Up is needed, but why the need for the `KeyWait 'Ctrl'` ?
That is what can be different between:

Code: Select all

^p Up:: {
 KeyWait 'Ctrl'
 SendEvent '{Text}' A_Clipboard
}
and

Code: Select all

^p Up:: {
 SendEvent '{Text}' A_Clipboard
}
?

Secondly and more importantly I again had the issue now with the new script. Again it looks like ctrl is "stuck" though I am not 100% sure. I have to close AHK and open it again for my keyboard to return to working properly :(

Thanks for your help
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: script for "play paste" the clipboard causes AHK bug

15 Feb 2024, 02:12

mikagenic wrote: OK so that makes me understand why Up is needed, but why the need for the `KeyWait 'Ctrl'` ?
That is what can be different between:
...
?
It's easy enough to run both so you can see the difference yourself. The one with the KeyWait is waiting for the Ctrl key to be released before moving on and executing the SendEvent line. The one with the Up moves on after releasing the P key even if you still have Ctrl held down. That's why I specifically used KeyWait so it wouldn't execute the SendEvent line until the Ctrl key is also released.

mikagenic wrote: Secondly and more importantly I again had the issue now with the new script. Again it looks like ctrl is "stuck" though I am not 100% sure. I have to close AHK and open it again for my keyboard to return to working properly :(
When you're 100% sure, post your exact script or a simplified one that exhibits the same issue. Make sure you have no other scripts (or other applications that have to do with simulating keystrokes) running when evaluating the effects of your script.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Lpanatt and 59 guests