Avoid Repetitive Code for Keypress with Number Key

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JohnTravolski
Posts: 21
Joined: 22 Dec 2015, 15:10

Avoid Repetitive Code for Keypress with Number Key

14 May 2023, 11:46

I have been writing a lot of repetitive hotkeys like the following:

Code: Select all

F13 & 1::
sorted_monitors := GetMonitorIndexesSortedByLeftmost()
PerformMove(sorted_monitors[1].ind,-1,-1)
return

F14 & 1::
sorted_monitors := GetMonitorIndexesSortedByLeftmost()
PerformMoveWindow(sorted_monitors[1].ind)
return

F13 & 2::
sorted_monitors := GetMonitorIndexesSortedByLeftmost()
PerformMove(sorted_monitors[2].ind,-1,-1)
return

F14 & 2::
sorted_monitors := GetMonitorIndexesSortedByLeftmost()
PerformMoveWindow(sorted_monitors[2].ind)
return

F13 & 3::
sorted_monitors := GetMonitorIndexesSortedByLeftmost()
PerformMove(sorted_monitors[3].ind,-1,-1)
return

F14 & 3::
sorted_monitors := GetMonitorIndexesSortedByLeftmost()
PerformMoveWindow(sorted_monitors[3].ind)
return
...

and so on, all the way to the 9 number on the keyboard.

Ideally, I'd be able to write something like this instead:

Code: Select all

F13 & # in [1,2,3,4,5,6,7,8,9]::
sorted_monitors := GetMonitorIndexesSortedByLeftmost()
PerformMove(sorted_monitors[#].ind,-1,-1)
return

F14 & # in [1,2,3,4,5,6,7,8,9]::
sorted_monitors := GetMonitorIndexesSortedByLeftmost()
PerformMoveWindow(sorted_monitors[#].ind)
return
where essentially I only have to write each one twice but I can somehow specify group of keys it applies to. Is this possible to do in autohotkey, or should I just stick to the repetitive way I've been doing it?
User avatar
RDC
Posts: 112
Joined: 29 Jan 2023, 10:22

Re: Avoid Repetitive Code for Keypress with Number Key

14 May 2023, 12:20

Untested, but something like this should work...

Code: Select all

#Persistent

Loop, 9
{
    Hotkey, F13 & %A_Index%, MyLabel
}

Return

MyLabel:
    Key := GetKeyNumber(A_ThisHotKey)
    MsgBox You pressed F13 and the number %Key%.

; YOUR CODE HERE

Return

GetKeyNumber(key)
{
    StringReplace, key, key, F13 &
    Return key
}
JohnTravolski
Posts: 21
Joined: 22 Dec 2015, 15:10

Re: Avoid Repetitive Code for Keypress with Number Key

14 May 2023, 12:28

RDC wrote:
14 May 2023, 12:20
Untested, but something like this should work...

Code: Select all

#Persistent

Loop, 9
{
    Hotkey, F13 & %A_Index%, MyLabel
}

Return

MyLabel:
    Key := GetKeyNumber(A_ThisHotKey)
    MsgBox You pressed F13 and the number %Key%.

; YOUR CODE HERE

Return

GetKeyNumber(key)
{
    StringReplace, key, key, F13 &
    Return key
}
Unfortunately this just types the number key and does not make the message box show up.
User avatar
kunkel321
Posts: 1156
Joined: 30 Nov 2015, 21:19

Re: Avoid Repetitive Code for Keypress with Number Key

14 May 2023, 13:00

I can't really test this, but it might give you some ideas...

Code: Select all

;for AHK v1
F1 & 1::
F13 & 2::
F14 & 2::
F13 & 3::
F14 & 3::
;MsgBox % A_ThisHotkey
sorted_monitors := GetMonitorIndexesSortedByLeftmost()
var := strSplit(A_ThisHotkey, " & ")
;msgbox % var[2]

If (var[1] = "F13")
	params := "-1,-1"
Else 
	params := ""
	
PerformMoveWindow(sorted_monitors[%var[2]%].ind%params%)
return
EDIT: Regarding line 17, I'm not sure if variables can even be used this way, but it's worth a try.
ste(phen|ve) kunkel
User avatar
RDC
Posts: 112
Joined: 29 Jan 2023, 10:22

Re: Avoid Repetitive Code for Keypress with Number Key

14 May 2023, 13:03

I cannot help much more. I don't have a F13 key, but it seems to work with F12 to give the message box depending on keys pressed. Also, only works with QWERTY numbers and not Numpad numbers.
The below works for me...

Code: Select all

#Persistent

Loop, 9
{
    Hotkey, F12 & %A_Index%, MyLabel
}

Return

MyLabel:
    Key := GetKeyNumber(A_ThisHotKey)
    MsgBox You pressed F12 and the number %Key%.

Return

GetKeyNumber(key)
{
    StringReplace, key, key, F12 &
    Return key
}

You should get a message box similar to this...
image.png
image.png (3.06 KiB) Viewed 267 times

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], peter_ahk and 188 guests