Cycle Keys

Get help with using AutoHotkey and its commands and hotkeys
foguetes
Posts: 17
Joined: 19 Oct 2018, 04:58

Cycle Keys

16 Apr 2019, 08:30

Hi

I am working on a script that will cycle through a list of key combos. Right now if I press Ctrl+Alt+Shift+F1 it cycles through the combos forwards. The combos are Ctrl + (numpad1-9) and the Ctrl + Shit + (numpad1-7) and then it goes back to zero which is Ctrl + numpad0.

My question is: how can I make it cycle backwards if I press Ctrl + Alt + F1 ?

Code: Select all

^!+F1::
 key++                         ; this will help cycling through the keys depending on its value
 if key = 1
    Send, ^{Numpad1}
 else if key = 2
    Send, ^{Numpad2}
 else if key = 3
	Send, ^{Numpad3}
 else if key = 4
	Send, ^{Numpad4}
 else if key = 5
	Send, ^{Numpad5}
 else if key = 6
	Send, ^{Numpad6}
 else if key = 7
	Send, ^{Numpad7}
 else if key = 8
	Send, ^{Numpad8}
 else if key = 9
	Send, ^{Numpad9}
 else if key = 10
	Send, ^+{Numpad1}
 else if key = 11
	Send, ^+{Numpad2}
 else if key = 12
	Send, ^+{Numpad3}
 else if key = 13
	Send, ^+{Numpad4}
 else if key = 14
	Send, ^+{Numpad5}
 else if key = 15
	Send, ^+{Numpad6}
 else if key = 16
	Send, ^+{Numpad7}
else if key = 17
 {
    Send, ^{Numpad0}
    key = 0                    ; return to the original state... do this on the last hotkey you would like to send
 }
return
Last edited by tank on 16 Apr 2019, 08:38, edited 1 time in total.
Reason: Added subject and moved to ask for help
User avatar
evilC
Posts: 4730
Joined: 27 Feb 2014, 12:30

Re: Cycle Keys

16 Apr 2019, 09:31

Code: Select all

num := 0
return

F1::
    num++
    if (num > 16)
        num := 0
    DoSend(num)
    return

F2::
    num--
    if (num < 0)
        num := 16
    DoSend(num)
    return

DoSend(num){
    pfx := "^"
    key := num
    if (num > 9){
        pfx .= "+"
        key -= 9
    }
    str := pfx "{Numpad" key "}"
    Send % str
    ;~ Tooltip % str
}
Rohwedder
Posts: 2204
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Cycle Keys

16 Apr 2019, 09:58

Hallo,
try:

Code: Select all

key = 0
Return
^!+F1::
IF key++ > 16
	key = 0
key2 := Mod(key,10)+(key>9)
Send, ^{Numpad%key2%}
Return
^!F1::
IF key-- < 0
	key = 16
key2 := Mod(key,10)+(key>9)
Send, ^{Numpad%key2%}
Return
foguetes
Posts: 17
Joined: 19 Oct 2018, 04:58

Re: Cycle Keys

17 Apr 2019, 02:38

@Rohwedder's script works perfectly. There's a lot I still need to learn about autohotkey.
foguetes
Posts: 17
Joined: 19 Oct 2018, 04:58

Re: Cycle Keys

25 Apr 2019, 07:38

Rohwedder wrote:
16 Apr 2019, 09:58
Hallo,
try:

Code: Select all

key = 0
Return
^!+F1::
IF key++ > 16
	key = 0
key2 := Mod(key,10)+(key>9)
Send, ^{Numpad%key2%}
Return
^!F1::
IF key-- < 0
	key = 16
key2 := Mod(key,10)+(key>9)
Send, ^{Numpad%key2%}
Return
Actually I was wrong. Maybe I didn't pay enough attention. The script cycles through the first 10 key combinations but then it doesn't continue to the remaining 7.
Rohwedder
Posts: 2204
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Cycle Keys

25 Apr 2019, 11:28

Hallo,
the error was the post-increment/decrement: key++ and key--.
Now with pre-increment/decrement: ++key and --key.
Try:

Code: Select all

^!+F1::
IF ++key > 16
	key = 0
key2 := Mod(key,10)+(key>9)
Send, ^{Numpad%key2%}
Return
^!F1::
IF --key < 0
	key = 16
key2 := Mod(key,10)+(key>9)
Send, ^{Numpad%key2%}
Return
;key	1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16  0  1...
;key2	1  2  3  4  5  6  7  8  9  1  2  3  4  5  6  7  0  1...
foguetes
Posts: 17
Joined: 19 Oct 2018, 04:58

Re: Cycle Keys

02 May 2019, 03:57

Sorry to bother you about this, but it's still not working. I don't seem to understand how it's working. Every time I make a change the script keeps behaving the same way.

So, as a reminder, the sequence of keys should be:
^(Numpad 0-9) and then ^+(Numpad 1-7)

!+F1 Is the shortcut for increments

^!F1 Is the shortcut for decrements

Thanks!
User avatar
evilC
Posts: 4730
Joined: 27 Feb 2014, 12:30

Re: Cycle Keys

02 May 2019, 04:21

@Rohwedder You only ever send Ctrl + key, whereas for the second set he wants Ctrl+Shift+key

What was wrong with my version? It seems fine

Code: Select all

[15088] AHK| Sending ^{Numpad1}
[15088] AHK| Sending ^{Numpad2}
[15088] AHK| Sending ^{Numpad3}
[15088] AHK| Sending ^{Numpad4}
[15088] AHK| Sending ^{Numpad5}
[15088] AHK| Sending ^{Numpad6}
[15088] AHK| Sending ^{Numpad7}
[15088] AHK| Sending ^{Numpad8}
[15088] AHK| Sending ^{Numpad9}
[15088] AHK| Sending ^+{Numpad1}
[15088] AHK| Sending ^+{Numpad2}
[15088] AHK| Sending ^+{Numpad3}
[15088] AHK| Sending ^+{Numpad4}
[15088] AHK| Sending ^+{Numpad5}
[15088] AHK| Sending ^+{Numpad6}
[15088] AHK| Sending ^+{Numpad7}
[15088] AHK| Sending ^{Numpad0}
[15088] AHK| Sending ^{Numpad1}
Rohwedder
Posts: 2204
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Cycle Keys

02 May 2019, 07:34

Hallo,
I completely overlooked the shift.

Code: Select all

+q:: ;^!+F1::
IF ++key > 16
	key = 0
key2 := Mod(key,10)+(key>9)
p := (key>9) ? "+":
Send, ^%p%{Numpad%key2%}
Return
q:: ;^!F1::
IF --key < 0
	key = 16
key2 := Mod(key,10)+(key>9)
p := (key>9) ? "+":
Send, ^%p%{Numpad%key2%}
Return
But take evilC's version for safety's sake.
He apparently uses a verification tool.
foguetes
Posts: 17
Joined: 19 Oct 2018, 04:58

Re: Cycle Keys

18 Jul 2019, 09:08

I was having a problem where this script didn't cycle through all the keys when NumLock was active (which is always).

So I made it turn NumLock off and on again using SetNumLockState.

Code: Select all

num := 0
return

^!+F1::
SetNumLockState, Off
    num++
    if (num > 16)
        num := 0
    DoSend(num)
SetNumLockState, On
    return

^!F1::

SetNumLockState, Off
    num--
    if (num < 0)
        num := 16
    DoSend(num)
SetNumLockState, On
    return

DoSend(num){
    pfx := "^"
    key := num
    if (num > 9){
        pfx .= "+"
        key -= 9
    }
    str := pfx "{Numpad" key "}"
    Send % str
    ;~ Tooltip % str
}

Return to “Ask For Help”

Who is online

Users browsing this forum: AHK_user, Google [Bot] and 60 guests