Holding down RCtrl+Up not always captured Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Holding down RCtrl+Up not always captured

24 Nov 2017, 16:04

Project: https://github.com/ltGuillaume/Redshift-Tray

I'm setting the volume via RCtrl + Up / Down:

Code: Select all

>^Up::Send {Volume_Up}
>^Down::Send {Volume_Down}
However, when I hold down the keys to repeatedly fire Volume_Up/Volume_Down, sometimes it doesn't get captured and a "regular" Ctrl+Up/Down slips through the cracks. It especially presents itself when the system is busy starting a program or is playing a video.
To counter this, I've tried:

Code: Select all

>$^Up::Send {Volume_Up}
>$^Down::Send {Volume_Down}
and/or (also with value 400 or even 999)

Code: Select all

#MaxHotkeysPerInterval, 200
and/or

Code: Select all

SetBatchLines, -1
but none of these things prevent it.
Last edited by Guillaume on 25 Nov 2017, 07:23, edited 2 times in total.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Holding down RCtrl+Up not always captured

24 Nov 2017, 17:45

Try adding "SendMode Input" to the top of your script. I'm experiencing similar issues with the default Event mode and Input seems to resolve them in my tests.
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Re: Holding down RCtrl+Up not always captured

24 Nov 2017, 21:37

After some rudimentary testing, SendInput instead of Send seems to work in 1 of the 2 use cases. Thank you!

EDIT: Some more testing revealed that the problem persists.
Last edited by Guillaume on 25 Nov 2017, 07:25, edited 1 time in total.
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Re: Holding down RCtrl+Up not always captured

24 Nov 2017, 21:43

In another part of the project, I'm changing the volume of the local machine while an RDP client window is active (full-screen). Alas, this method presents the same issues.

- Setting: I have a full-screen RDP session as active window.
- First I suspend, then "unsuspend" the locally active script, so that it is the boss over the RDP client's keyboard hook
- Then:

Code: Select all

#If, WinActive("ahk_class TscShellContainerClass")
>^Up::SetVolume("+2")
>^Down::SetVolume("-2")

SetVolume(value) {
	SoundSet, %value%
	SoundGet, volume
	Tooltip, % Round(volume)`%
	SetTimer, RemoveToolTip, 1000
}
What happens now is that the normal RCtrl-Up/Down will seep through on the remote system once in a while.
Last edited by Guillaume on 25 Nov 2017, 07:28, edited 1 time in total.
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Re: Holding down RCtrl+Up not always captured

25 Nov 2017, 07:07

Did some more testing, unfortunately, SendInput does not work either; the regular key combination Ctrl+Up/Down still seeps through.

It's just that my test method (AIDA64 stress test, then hold RCtrl+Up) does not do the trick. When I start a program, like Firefox or AIDA64, while for example the Total Commander window is active, there's a small time window where all key strokes aren't caught by AutoHotkey (Ctrl+Up then means "open new tab with current folder").

So, in both non-RDP and RDP cases, it still doesn't work.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Holding down RCtrl+Up not always captured

25 Nov 2017, 13:31

When I was using SendEvent to raise and lower the volume with Ctrl+Up/Down, I noticed that, when I held them down past 0 or 100, the cursor would start moving. For example, once the volume got to 100, the text cursor active in a document started to move up lines, and the reverse happened when the volume got to 0. That stopped happening when I switched to SendInput, which is why I recommended that. Regardless, could that be happening to you? If so, it's not exactly "seeping" through, but an annoying, unwanted behavior that may give you an idea of how to fix it or may not be fixable with {Volume_Up} and {Volume_Down}. Perhaps bypassing AHK's commands finding out how to call and control the volume applet directly (via DLL call or something) would work better.
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 11:38

- Nope, it still happens with SendInput.
- I have seen this behavior when the volume was between 0 and 100, too.
- I also see this behavior with SoundSet, so by now I'd say it's got nothing to do with what kind of command I use, but with a problem of how AutoHotkey captures the key combination. I know that Autohotkey lets the script sleep every so often, that's why I thought that SetBatchLines, -1 would perhaps fix it. Unfortunately, it doesn't, so I probably misinterpreted that command; it's probably about when it walks through the script.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 15:25

Maybe it would help to define hotkeys for the up and down arrow keys that don't do anything when the Ctrl key is physically pressed down:

Code: Select all

#If, WinActive("ahk_class TscShellContainerClass")

~Up::
~Down::
  If GetKeyState("RCtrl", "P")
  return
return

>^Up::SetVolume("+2")
>^Down::SetVolume("-2")

SetVolume(value) {
	SoundSet, %value%
	SoundGet, volume
	Tooltip, % Round(volume)`%
	SetTimer, RemoveToolTip, 1000
}
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 15:49

Good one! I'll do some more testing later on, but it seems to work on one of my systems!
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 15:54

Nice.
On second thought, I'm not sure how that's any different than the following::

Code: Select all

#If, WinActive("ahk_class TscShellContainerClass")

~Up::
~Down::return

>^Up::SetVolume("+2")
>^Down::SetVolume("-2")

SetVolume(value) {
	SoundSet, %value%
	SoundGet, volume
	Tooltip, % Round(volume)`%
	SetTimer, RemoveToolTip, 1000
}
You might just try that. In fact, I just disabled those extra hotkeys and even the script entirely and RCtrl + Up/Down doesn't move my cursor at all, so I'm not sure what's going on. I was able to reproduce your issue (or a similar one) yesterday and, now, I can't reproduce it, so I guess that I have to leave it up to you to test and see what works.

That said, if you find that neither method actually works, try the following, which makes more sense as to why it would do what I originally wanted it to do:

Code: Select all

#If, WinActive("ahk_class TscShellContainerClass")

Up::
Down::
  If not GetKeyState("RCtrl", "P")
    Send, {%A_ThisHotkey%}
return

>^Up::SetVolume("+2")
>^Down::SetVolume("-2")

SetVolume(value) {
	SoundSet, %value%
	SoundGet, volume
	Tooltip, % Round(volume)`%
	SetTimer, RemoveToolTip, 1000
}
Last edited by Osprey on 26 Nov 2017, 16:55, edited 1 time in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 16:49

I did a little script to experiment, does anyone have any idea why sometimes a key will slip? By contrast, something like q::SendInput, w was very reliable.
[EDIT:] Hmm, even ^q::SendInput, w slipped up sometimes.

Code: Select all

q::
MsgBox, % vCount " " vCountX
vCount := vCountX := 0
return

~Up::
~Down::
vCountX++
ToolTip, % vCount " " vCountX
return

$^Up::
vCount++
Send, {Volume_Up}
return

$^Down::
vCount++
Send, {Volume_Down}
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 18:01

SendInput didn't work for me either. That makes sense, because it's not that the sending is the problem, the problem is with capturing the key combination reliably. Nope, none of these options work. I'm thinking this is bug (that can't be circumvented).
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 18:30

Have you tried adding key delay to increase the reliability? That was going to be my original suggestion, but it didn't make a difference in my test, but I wasn't really testing the "seeping" through that you're experiencing.

Code: Select all

SendMode Event
SetKeyDelay, 20, 50
Those are pretty obscene delays, but, if the problem doesn't occur, you can dial them towards 0 to see how low you can go until the seeping starts happening.
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 22:11

Makes it significantly worse. SetKeyDelay, -1 seems to be helping (default is 10 according to help file), but it isn't completely gone. I noticed a difference when thinkering with the SetKeyDelay setting, namely that now instead of RControl+Up, just Up seeps through at times.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 23:03

In coding, the only bad results are no results. At least you got a result, even if it was "significantly worse" :D. Ok, sorry, I'm out of ideas and just lightening the mood so that you don't end up like your avatar over this ;).
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Holding down RCtrl+Up not always captured

26 Nov 2017, 23:14

I thought Osprey's script was looking pretty good.
Here's a slight modification, I've replaced GetKeyState with A_TickCount. In case it helps at all.

Code: Select all

#If, WinActive("ahk_class TscShellContainerClass")

$Up::
$Down::
	if (A_TickCount - SetVolumeTickCount > 2000)
		Send, % "{" SubStr(A_ThisHotkey, 2) "}"
return

>^Up::SetVolume("+2")
>^Down::SetVolume("-2")

SetVolume(value) {
	global SetVolumeTickCount := A_TickCount
	SoundSet, %value%
	SoundGet, volume
	ToolTip, % Round(volume) "%"
	SetTimer, RemoveToolTip, 1000
}
Simple version of the script, demonstrating the principle.

Code: Select all

$q::
if (A_TickCount - vTickCount > 2000)
	SendInput, q
return

^q::
SendInput, w
vTickCount := A_TickCount
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Re: Holding down RCtrl+Up not always captured

27 Nov 2017, 14:29

Thanks to you both for all your help! I still think it's rather strange to have to go through these hoops in order to get it working, and though there's easily observable improvement, it's still not airtight. Here's what I did:

Code: Select all

SetKeyDelay, -1 ; Absolutely very important, otherwise the Up or down arrow is simulated quite often while holding Ctrl+Up/Down resp.

;The following gets a few more Ups and Downs that seep through the cracks, but not all
Global volumetick = 0

#If, hotkeys
Up::
Down::
	If (A_TickCount - volumetick) > 1000
		Send, {%A_ThisHotkey%}
Return

#If, hotkeys And WinActive("ahk_class TscShellContainerClass")
>^Up::SetVolume("+2")
>^Down::SetVolume("-2")

SetVolume(value) {
	SoundSet, %value%
	SoundGet, volume
	volumetick = %A_TickCount%
	Tooltip, % Round(volume)`%
	SetTimer, RemoveToolTip, 1000
}
I also tried forcing a key hook ($Up:: and $Down::), but that did not have any further effect.
User avatar
Guillaume
Posts: 20
Joined: 24 Nov 2017, 14:06

Re: Holding down RCtrl+Up not always captured

27 Nov 2017, 15:02

It's absurd and totally unreliable (and probably highly dependent on the system speed): if I have

Code: Select all

If (A_TickCount - volumetick) > 1000
then there's one or two Ups during testing, while

Code: Select all

If (A_TickCount - volumetick) > 2000
gets me a tsunami of Ctrl+Ups.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Holding down RCtrl+Up not always captured

27 Nov 2017, 15:28

Perhaps it would help to move the volumetick = %A_TickCount% line to either the top or the bottom of the function. The commands above and below it now are probably relatively processor intensive.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Holding down RCtrl+Up not always captured

27 Nov 2017, 16:17

Try adding these 2 lines to the top of the hotkey subroutine:

Code: Select all

if (A_TimeIdle > 200)
	return
What happens is, if you don't press any keys for a while, those queued hotkeys are immediately discarded when the hotkey is launched.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jaka1 and 146 guests