Hold Key While Pressing Other Keys Without The Held Key Stopping?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
abcjme
Posts: 65
Joined: 08 Mar 2018, 10:48

Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 06:00

For example:

- I hold s
- I press and release d while holding s
- s continues functioning after releasing d

Is there a way to make that work?
Rohwedder
Posts: 7622
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 07:00

Hallo,
try:

Code: Select all

s::
While, GetKeyState("s","P")
{
	ToolTip, s: %A_Index%
	Sleep, 200
}
Return
d::
While, GetKeyState("d","P")
{
	ToolTip, d: %A_Index%
	Sleep, 200
}
Return
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 07:17

Rohwedder wrote:Hallo,
try:

Code: Select all

s::
While, GetKeyState("s","P")
{
	ToolTip, s: %A_Index%
	Sleep, 200
}
Return
d::
While, GetKeyState("d","P")
{
	ToolTip, d: %A_Index%
	Sleep, 200
}
Return
White, GetKeyState should be avoided (https://autohotkey.com/boards/viewtopic.php?t=19745). The same thing can be done with SetTimer. I didn't understand exactly what the ops meant by what he said. But let's assume RohWedder is right.

Code: Select all

Toggle := 0
Toggle2 := 0
Svar := 0
Dvar := 0

s::
Svar++
Toggle := !Toggle
If Toggle
SetTimer, Spress, 0
Else {
SetTimer, Spress, Off
S := 0
}
return

d::
Dvar++
Toggle2 := !Toggle2
If Toggle2
SetTimer, Dpress, 0
Else {
SetTimer, Dpress, Off
D := 0
}
return

Spress:
If not Toggle 
SetTimer, S, Off
Else {
ToolTip, s : %Svar%
Sleep, 200
ToolTip
}
return


Dpress:
If not Toggle2
SetTimer, D, Off
Else {	
ToolTip, d : %Dvar%
Sleep, 200
ToolTip
}
return

		
		
esc:: ExitApp
I tried the script and wondering why tooltip's value won't refresh something is not right. Also because Svar and Dvar should be set to 0 along with SetTImer off.. What am I missing??
I am your average ahk newbie. Just.. a tat more cute. ;)
abcjme
Posts: 65
Joined: 08 Mar 2018, 10:48

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 08:03

Nwb wrote: While, GetKeyState should be avoided (https://autohotkey.com/boards/viewtopic.php?t=19745). The same thing can be done with SetTimer. I didn't understand exactly what the ops meant by what he said. But let's assume RohWedder is right.
Code
etc...
Evilc's post from that link only seems to work if you don't change the strings (e.g. if ~a::a , but not if ~a::b (or any other character/s)).

As for your code, it wouldn't run at all. I was able to run it with some modifications. However, even then, it didn't seem to function as suggested.

Modification:

Code: Select all

Toggle := 0
Toggle2 := 0
S := 0
D := 0

s::
S++
Toggle := !Toggle
If Toggle
SetTimer, S, 0
Else {
SetTimer, S, Off
S := 0
}
If not Toggle 
SetTimer, S, Off
Else {
ToolTip, s: %S%
Sleep, 50
}
return

d::
D++
Toggle2 := !Toggle2
If Toggle2
SetTimer, D, 0
Else {
SetTimer, D, Off
D := 0
}
If not Toggle2
SetTimer, D, Off
Else {	
ToolTip, d: %D%
Sleep, 50
}
return
abcjme
Posts: 65
Joined: 08 Mar 2018, 10:48

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 08:07

Rohwedder wrote:...
That would probably work in most cases, but what I'm going for is probably a bit more complicated. s is it's own hotkey, but it's also a part of a combo hotkey. Thus, the goal is to seamlessly go from s to s & d, and then back to s, ad infinitum. It's not that s should be firing constantly, such as, even at the same time that s & d is firing. Rather, it's just that s should fire immediately after d is released, without also having to release and press s again. Is this possible?

For example,
s::x
s & d:: y
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 08:18

How about use s & d:: and then under the loop use getkeystate for d and write the actions below when keystate is up?

Also does anybody know why my script didn't work? I'm clueless.
It did for me btw. The values did keep incrementing so the settimer part worked. But the values didn't reset to 0 and the tooltip did not refresh.

Two while states is not good in general because of threading, evilc's thread was just an example.
https://autohotkey.com/docs/misc/Threads.htm
I am your average ahk newbie. Just.. a tat more cute. ;)
abcjme
Posts: 65
Joined: 08 Mar 2018, 10:48

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 08:34

Nwb wrote:How about use s & d:: and then under the loop use getkeystate for d and write the actions below when keystate is up?

Also does anybody know why my script didn't work? I'm clueless.
It did for me btw. The values did keep incrementing so the settimer part worked. But the values didn't reset to 0 and the tooltip did not refresh.

Two while states is not good in general because of threading, evilc's thread was just an example.
https://autohotkey.com/docs/misc/Threads.htm
Did you edit your post? Your code works now, but it didn't earlier.

Anyway, getkeystate is essentially what I've tried. It still stops when I release d though.

Code: Select all

s::
if(GetKeyState("d")) {
        Send, y
    }
    else
    Send x
    Sleep 50
return
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 08:37

abcjme wrote:
Nwb wrote:How about use s & d:: and then under the loop use getkeystate for d and write the actions below when keystate is up?

Also does anybody know why my script didn't work? I'm clueless.
It did for me btw. The values did keep incrementing so the settimer part worked. But the values didn't reset to 0 and the tooltip did not refresh.

Two while states is not good in general because of threading, evilc's thread was just an example.
https://autohotkey.com/docs/misc/Threads.htm
Did you edit your post? Your code works now, but it didn't earlier.

Anyway, getkeystate is essentially what I've tried. It still stops when I release d though.

Code: Select all

s::
if(GetKeyState("d")) {
        Send, y
    }
    else
    Send x
    Sleep 50
return
Yes I did. And I thought that was the point (stops when you release d)??

Anyways did you try a & d:: like I said? Try doing that, if you need help I will make an example script
I am your average ahk newbie. Just.. a tat more cute. ;)
abcjme
Posts: 65
Joined: 08 Mar 2018, 10:48

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 09:11

Nwb wrote:Anyways did you try s & d:: like I said? Try doing that, if you need help I will make an example script
I'm not sure I understand what you mean. If it's not too much trouble, an example script would be great!
Rohwedder
Posts: 7622
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 10:48

Hallo,
you want s:: and s & d:: but only one should be active?
Then you do not need timers! Try:

Code: Select all

s::
While, GetKeyState("s","P")
{
	ToolTip, s: %A_Index%
	Sleep, 200
}
Return
~s & d::
While, GetKeyState("d","P")
{
	ToolTip, d: %A_Index%
	Sleep, 200
}
Return
but if you want s:: and d:: to be independent of each other, i.e. both should be able to be active at the same time:

Code: Select all

s := 0, d:= 0
Return
s::
	HotKey, s, Off
	SetTimer, STimer, 200
Return
s Up::
	SetTimer, STimer, Off
	HotKey, s, On
	ToolTip
Return
STimer:
	s++
	ToolTip, s: %s% d: %d%
Return
d::
	HotKey, d, Off
	SetTimer, DTimer, 200
Return
d Up::
	SetTimer, DTimer, Off
	HotKey, d, On
	ToolTip
Return
DTimer:
	d++
	ToolTip, s: %s% d: %d%
Return
abcjme
Posts: 65
Joined: 08 Mar 2018, 10:48

Re: Hold Key While Pressing Other Keys Without The Held Key Stopping?

17 Mar 2018, 11:45

Rohwedder wrote:...
I had forgotten about the usefulness of the tilde. That did it, thanks!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Joey5 and 188 guests