Create & Use Custom Labels as Hotkey

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
OrangeCat
Posts: 65
Joined: 14 Jun 2022, 00:47

Create & Use Custom Labels as Hotkey

Post by OrangeCat » 26 Dec 2023, 21:51

I've been searching the forums and found some hints but I haven't been able to figure out how to achieve my desired goal...
I've a ReDragon M913 Mouse, A mouse that has a total of 16 remapped buttons via its factory-software...
I remapped 13 of the 16 factory-software's remapable buttons only (leaving LButton/MButton/RButton as-is within factory software)
This left 12 side-located buttons and 1 top-located buttons to be remapped, which I remapped as follows:

NOTE: described via labels of preferences, using MB0 as the «1x top-located button» & using MB1 to MB12 as the «12x side-located buttons».

MB0 = XButton1[]
MB1 = XButton2
MB2 = Browser_Favorites
MB3 = Browser_Refresh
MB4 = Browser_Stop
MB5 = Browser_Back
MB6 = Browser_Forward
MB7 = Browser_Search
MB8 = Browser_Home
MB9 = Launch_App1
MB10 = Launch_App2
MB11 = Launch_Mail
MB12 = Launch_Media


Thing is, I prefer using single keystrokes per reprogram-able keys as this allows scripting custom combinations more easily via AHK. When I prior-used a Logitech G600, I would instead rely on F13-to-F24 and Help & etc. for my reprogram-able buttons, being «non-interfering keys» but this ReDragon Mouse doesn't support those, being why I instead used the least-used Media-keys...

Using F13 to F24 wasn't so bad for when it came to button/hotkey-identifier scripting, although this now became a bit more challenging nowadays through the use of Media Keys (now mainly relying on comments as Identifiers)

My question is...
Is it possible to create personalized hotkey identifiers? via use of above labels-of-preference?

Example:

Code: Select all


MB0 & WheelDown::Gosub, _ZoomOut
MB0 & WheelUp::Gosub, _ZoomIn
MB1 & WheelUp::Gosub, _VolumeUp
MB1 & WheelDown::Gosub, _VolumeDown
MB1 & MButton::Gosub, _VolumeMute

/*
    .--------.
####| GOSUBS |####
    '========'
allows to label-id hotkey's "action-assignments/executions", best to start with «_» for best "elsewhere-used label entries" preventions/avoid confusions.
*/

_ZoomOut:
    Send {LCtrl Down}
        Gosub, AHK_KeyDelay
    Send {WheelDown}
        Gosub, AHK_KeyDelay
    Send {LCtrl Up}
RETURN

_ZoomIn:
    Send {LCtrl Down}
        Gosub, AHK_KeyDelay
    Send {WheelUp}
        Gosub, AHK_KeyDelay
    Send {LCtrl Up}
RETURN

_VolumeUp:
    Send {Volume_Up Down}
        Gosub, AHK_KeyDuration_Small
    Send {Volume_Up Up}
RETURN

_VolumeDown:
    Send {Volume_Down Down}
        Gosub, AHK_KeyDuration_Medium
    Send {Volume_Down Up}
RETURN

_VolumeMute:
    Send {Volume_Mute}
RETURN


/*
     .---------------------------.   
#####| AHK VARS & ETC. AS GOSUBS |#####
     '==========================='  
changing value here will simultaneously apply changes everywhere where used
*/

AHK_KeyDelay:
    sleep 10
RETURN

AHK_KeyDuration_Small:
    sleep 25
RETURN

AHK_KeyDuration_Medium:
    sleep 50
RETURN

User avatar
mikeyww
Posts: 28869
Joined: 09 Sep 2014, 18:38

Re: Create & Use Custom Labels as Hotkey

Post by mikeyww » 26 Dec 2023, 22:04

If you do not want to use the actual key name, you can use a variable for a key name if you use the :arrow: Hotkey command.
User avatar
OrangeCat
Posts: 65
Joined: 14 Jun 2022, 00:47

Re: Create & Use Custom Labels as Hotkey

Post by OrangeCat » 30 Dec 2023, 16:40

I've often attempted to properly interpret that help-file's specific section
(using original-post's hotkey syntax of preference)

If not too much trouble, I'll paste one of a sub-profile's portion of my code and relative Go Subs here...
If not much trouble to provide a small portion example of how i'd go about to script a hotkey variable?

Code: Select all

;      .---------.
; #####| HOTKEYS |#####
;      '========='

; MOUSE BUTTON 1
 XButton2::Gosub, _CANCEL
 XButton2 & WheelUp::Gosub, _MPC_Audio_Prev 
 XButton2 & WheelDown::Gosub, _MPC_Audio_Next
 
; MOUSE BUTTON 2
Browser_Favorites::Gosub, Cancel

; MOUSE BUTTON 3 (set as Browser_Refresh)
Browser_Refresh::Gosub, _CANCEL
Browser_Refresh & WheelUp::Gosub, _MPC_Chap_Prev
Browser_Refresh & WheelDown::Gosub, _MPC_Chap_Next
Browser_Refresh & MButton::Gosub, _MPC_Jump_Beginning

; MOUSE BUTTON 4 (As Browser_Stop)
  Browser_Stop::Gosub, _CANCEL
  Browser_Stop & WheelUp::Gosub, _MPC_Seek_Backwards
  Browser_Stop & WheelDown::Gosub, _MPC_Seek_Forward
  Browser_Stop & MButton::Gosub, _MPC_PlayPause
  MButton & Browser_Stop::Gosub, _KBRD_PlayPause
 
 ; MOUSE BUTTON 5, 
 ; ETC. ETC. ETC.
 
 ;      .---------. 
 ; #####| GO SUBS |#####
 ;      '=========' 
_CANCEL:
	Send {Help} 
RETURN

MPC_Audio_Prev:
    Send {LShift Down}{a}{LShift Up}
RETURN

MPC_Audio_Next:
    Send {a}
RETURN

MPC_Chap_Prev:
    Send {PgUp}
RETURN

MPC_Chap_Next:
    Send {PgDn}
RETURN

MPC_Jump_Beginning:
	Send {Home}
RETURN

MPC_Seek_Backwards:
    Send {Left}
RETURN

MPC_Seek_Forward:
    Send {Right}
RETURN

MPC_PlayPause:
    Send {Space}
RETURN

KBRD_PlayPause:
	Send {Media_Play_Pause}
RETURN

Note:
The reason I use "_CANCEL / send {Help}" is because I use it as an "improper cancellation method" (where "help" doesn't have any key-assigned action via my kbrd language).
Otherwise, I found myself having a difficult time executing hotkeys when simply using "RETURN" alone.
As doing so, would have all of it's relative combo included lead-as-modifier-keys to be "RETURN" within the millisecond timestamp difference between its keystroke vs followed combo-included keystroke.
Resulting for mentioned "combo-included followed keystroke" to be executed as-per its default/non-combo included key output as if it were a solo-keystroke.

Also...
I also figured out an "improper method" for achieving desired original-post's inquiry but that would render 12 additional non-used keyboard keys to no longer be use-able for other alternative customization
(said improper-method involving the use of F13 to F24 in addition of all already-mentionned keys, such as for example: XButton2::F13, Browser_Favorites::F14, etc.).
User avatar
mikeyww
Posts: 28869
Joined: 09 Sep 2014, 18:38

Re: Create & Use Custom Labels as Hotkey

Post by mikeyww » 30 Dec 2023, 18:38

My advice would be that if you want to learn how the Hotkey command works, test with something short and simple. After it works, expand.

Code: Select all

#Requires AutoHotkey v1.1.35
MB0 := "XButton1"
Hotkey % MB0, Cancel, On
Return

Cancel:
Send help
Return
User avatar
OrangeCat
Posts: 65
Joined: 14 Jun 2022, 00:47

Re: Create & Use Custom Labels as Hotkey

Post by OrangeCat » 30 Dec 2023, 22:37

I'm gathering that the hotkey command cannot be used as part of a combination...
...unless if the combination is already included within the hotkey's related ":=" entry itself...

am I correct?

or would something similar to this be possible?

Code: Select all

Hotkey % MB0 & WheelDown, Cancel, On
==============================================================================
My understanding is that...

Code: Select all

MB0 := "XButton1"
...this creates a variable/label/or w.e it is that it's called

_
While this...

Code: Select all

Hotkey % MB0, Cancel, On
...converts label into a hotkey command with assigned instructions
a) where the % is used to look for above mentioned (focusing on ":=")
- where the inclusion of the comma is used as a perception-separator,
b) the 2nd perception/what follows the 1st comma is an entry of something that acts almost like a Gosub instruction but not quite the same per-se
- (not the same per-se as it depends on 3rd perception/what follows 2nd comma, being what the "on" is used for)

if that is more-or-so correct, then does it mean that off would de-activate it's instruction??

I'll test around with it for sure,
but the main question would be to know if something like this could be possible?

Code: Select all

Hotkey % MB0 & WheelDown, Cancel, On
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Create & Use Custom Labels as Hotkey

Post by boiler » 30 Dec 2023, 23:11

OrangeCat wrote:
30 Dec 2023, 22:37
I'm gathering that the hotkey command cannot be used as part of a combination...
...unless if the combination is already included within the hotkey's related ":=" entry itself...

am I correct?
No, you just have to use proper syntax. As always, literal strings in expressions must be quoted:

Code: Select all

#Requires AutoHotkey v1.1.35
MB0 := "XButton1"
Hotkey % MB0 " & WheelDown", Cancel, On
Return

Cancel:
Send help
Return

Or you could use legacy syntax instead of forcing an expression. This is the equivalent:

Code: Select all

Hotkey %MB0% & WheelDown, Cancel, On
User avatar
OrangeCat
Posts: 65
Joined: 14 Jun 2022, 00:47

Re: Create & Use Custom Labels as Hotkey

Post by OrangeCat » 02 Jan 2024, 12:44

Thank you kindly everyone, really appreciate your help.
mikeyww & boiler
User avatar
OrangeCat
Posts: 65
Joined: 14 Jun 2022, 00:47

Re: Create & Use Custom Labels as Hotkey

Post by OrangeCat » 23 Apr 2024, 03:14

boiler wrote:
30 Dec 2023, 23:11
No, you just have to use proper syntax. As always, literal strings in expressions must be quoted:

Code: Select all

#Requires AutoHotkey v1.1.35
MB0 := "XButton1"
Hotkey % MB0 " & WheelDown", Cancel, On
Return

Cancel:
Send help
Return

Or you could use legacy syntax instead of forcing an expression. This is the equivalent:

Code: Select all

Hotkey %MB0% & WheelDown, Cancel, On
I had some trouble at first where legacy syntax but from what I understand, it works like this... (starting with legacy)
→ MB0 = F1
→ MB0 := "F1"

Now, I've a couple question again...

Question #1

What if I wanted to use 2x variables, for example, MB0 & MB1,
I can't figure it out, these approach doesn't seem to work...

Code: Select all

MB0 := "XButton1"
Hotkey % MB0 & % MB1, Cancel, On
Return

MB0 := "XButton1"
Hotkey % MB0 & MB1, Cancel, On
Return
Question #2
The "return", I assume this is necessary for each and every hotkey variable usage?
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Create & Use Custom Labels as Hotkey

Post by boiler » 23 Apr 2024, 05:31

OrangeCat wrote: What if I wanted to use 2x variables, for example, MB0 & MB1,
You use a % followed by a space once to indicate that that parameter is an expression instead of legacy syntax. Then you are concatenating string segments. Literal strings are in quotes, and variables are not.

Code: Select all

Hotkey % MB0 " & " MB1, Cancel, On

OrangeCat wrote: The "return", I assume this is necessary for each and every hotkey variable usage?
Absolutely not. Lines after a return will not execute unless they follow some type of label and the code flow is routed to them (like the Cancel label in this script).. In this case, it marks the end of the auto-execute section. See Structure of a Script.
User avatar
OrangeCat
Posts: 65
Joined: 14 Jun 2022, 00:47

Re: Create & Use Custom Labels as Hotkey

Post by OrangeCat » 23 Apr 2024, 12:33

Okay, I meant to say that I was just thinking your Dec 30th example of "hotkey line usage" seemed to applied the same rules as a 1 lined script...
(seeing a Return included in what I assumed to be a single-line code as the rest of the same-line seems to be related the same as a gosub,
Thus I was thinking it was the same as a 1 line/no return required).

In your Dec 30th example...

Code: Select all

#Requires AutoHotkey v1.1.35
MB0 := "XButton1"
Hotkey % MB0 " & WheelDown", Cancel, On
Return
When it comes to actual scripts (the same as many other scripters too)...
...there'd be lots of "other script-stuff" separating said example's 1st and 2nd line.

Thus, I was questioning the use of that 3rd-line's Return, if it was needed or not, is all...

I'll test/play with it more now that I have an idea where-bouts to aim

If I wanted to use Legacy Syntax, I'd do:

Code: Select all

Hotkey %MB0% & %MB1%, Cancel, On
Return ; <--- or is this Return not-needed?/1 line script?
I'm re-reading again and again,
I also saw a number of hellbent tutorials on YouTube that I mean to watch soon.
I've only been using autohotkey for less than 2 years or so...
my bad.. but thanks, I appreciate the help.

What does the "On" do?,
It's just the way it is I guess with this?
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Create & Use Custom Labels as Hotkey

Post by boiler » 23 Apr 2024, 12:57

OrangeCat wrote: Thus, I was questioning the use of that 3rd-line's Return, if it was needed or not, is all...

...or is this Return not-needed?/1 line script?
You don't need a return return in your script if there are no other lines below, even if it's more than a one-line script. But you do need it to keep if from executing code below it like was under your Cancel label. So the script below would be wrong because after executing the Hotkey line, it would continue on to execute the Send line. It has nothing to do with how many lines are before the return, whether it be one or several.

Code: Select all

#Requires AutoHotkey v1.1.35
MB0 := "XButton1"
Hotkey % MB0 " & WheelDown", Cancel, On

Cancel:
Send help
Return

OrangeCat wrote: I'm re-reading again and again,
What does the "On" do?,
I would just be repeating exactly what's in the Hotkey documentation, so check it out. It's always best to look at the documentation for answers to these types of questions.
Post Reply

Return to “Ask for Help (v1)”