Very first Autohotkey V2 script, need advice

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
hlbnet
Posts: 2
Joined: 14 Jul 2016, 05:54

Very first Autohotkey V2 script, need advice

11 Dec 2023, 11:52

Hello everybody,

Here is my very first Autohotkey V2 script. I want to remap around 50 keys of my keyboard, plus a lot of "modified" states (with Shift, Control, Alt, Shift+Control, Shift+Alt, Control+Alt, Shift+Control+Alt). In total, I expect to have a few hundreds of hotkeys.

I started with remaping only 4 keys (without modifier): j, k, i and l. Here is my script. It seems to works well for now.

My question to experienced Autohotkey V2 users :
Is there a shorter way to get the same result with Autohotkey V2 ?
Do you think it will perform well (be responsive ...) after having added a few hundreds of other hotkeys in my script ?

Code: Select all

#SingleInstance Force
Hotkey "<", RemapAll
Hotkey "< up", RestoreAll

RemapAll(ThisHotkey)
{
	Hotkey "j", Key_j, "On"
	Hotkey "k", Key_k, "On"
	Hotkey "i", Key_i, "On"
	Hotkey "l", Key_l, "On"
}

RestoreAll(ThisHotkey)
{
	Hotkey "j", "Off"
	Hotkey "k", "Off"
	Hotkey "i", "Off"
	Hotkey "l", "Off"
}

Key_j(ThisHotkey)
{
    SendInput "{Left}"
}

Key_k(ThisHotkey)
{
    SendInput "{Down}"
}

Key_i(ThisHotkey)
{
    SendInput "{Up}"
}

Key_l(ThisHotkey)
{
    SendInput "{Right}"
}
Thanks by advance.
User avatar
mikeyww
Posts: 27323
Joined: 09 Sep 2014, 18:38

Re: Very first Autohotkey V2 script, need advice

11 Dec 2023, 13:38

Welcome to this AutoHotkey forum!

Yes, there is a shorter and easier way. Instead of calling the Hotkey function, just map your hotkeys directly.

Code: Select all

#Requires AutoHotkey v2.0
j::Left
Performance will not suffer, but your memory might suffer in trying to remember hundreds of key sequences. Using hotstrings could be one way to address this.
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Very first Autohotkey V2 script, need advice

11 Dec 2023, 14:42

This is the same script, written shorter:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance force

Hotkey("<", RemapAll)
Hotkey("< up", RestoreAll)

RemapAll(ThisHotkey){
	Hotkey("j", (*)=>(SendInput("{Left}")), "On")
	Hotkey("k", (*)=>(SendInput("{Down}")), "On")
	Hotkey("i", (*)=>(SendInput("{Up}")), "On")
	Hotkey("l", (*)=>(SendInput("{Right}")), "On")
}

RestoreAll(ThisHotkey){
	Hotkey("j", "Off")
	Hotkey("k", "Off")
	Hotkey("i", "Off")
	Hotkey("l", "Off")
}
hlbnet
Posts: 2
Joined: 14 Jul 2016, 05:54

Re: Very first Autohotkey V2 script, need advice

11 Dec 2023, 18:20

mikeyww wrote:
11 Dec 2023, 13:38
Welcome to this AutoHotkey forum!

Yes, there is a shorter and easier way. Instead of calling the Hotkey function, just map your hotkeys directly.

Code: Select all

#Requires AutoHotkey v2.0
j::Left
Performance will not suffer, but your memory might suffer in trying to remember hundreds of key sequences. Using hotstrings could be one way to address this.
Thank for your answer, but I expected the same behaviour as my script, which is not the case of your proposal. Maybe my post wasn't clear on this.
User avatar
mikeyww
Posts: 27323
Joined: 09 Sep 2014, 18:38

Re: Very first Autohotkey V2 script, need advice

11 Dec 2023, 21:37

To block (release) the modifiers, you can use Send.

Code: Select all

#Requires AutoHotkey v2.0
#HotIf GetKeyState('<', 'P')
*j::Send '{Left}'
*i::Send '{Up}'
*k::Send '{Down}'
*l::Send '{Right}'
#HotIf
Or possibly:

Code: Select all

#Requires AutoHotkey v2.0
<::<
< & j::Send '{Left}'
< & i::Send '{Up}'
< & k::Send '{Down}'
< & l::Send '{Right}'
US keyboard, but there may be key jamming with K:

Code: Select all

#Requires AutoHotkey v2.0

< Up::Send A_PriorKey = ',' ? '<' : ''

#HotIf GetKeyState('Shift', 'P')
, & j::Send '{Left}'
, & i::Send '{Up}'
, & k::Send '{Down}'
, & l::Send '{Right}'
#HotIf

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Jinjiro, LongKick64 and 32 guests