So what would be the best way of doing this?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
FoxEatsAces
Posts: 4
Joined: 26 Jul 2021, 12:46

So what would be the best way of doing this?

Post by FoxEatsAces » 26 Jul 2021, 13:00

I would like to assign various letter keys to do stuff, however this would prevent me from using their intended function.

I thought ok, what if I created a ahk script (that launches at start up) and used that script to open and close another script that way I can toggle my hotkey keyboard on and off.

I created Launcher.ahk which opens Hotkeys.ahk

Code: Select all

+h::
Run, open "C:\xxx\Hotkeys.ahk"

+c:: ;closes all other scripts
DetectHiddenWindows, On
WinGet, AHKList, List, ahk_class AutoHotkey
Loop, %AHKList%
{
    ID := AHKList%A_Index%
    If (ID <> A_ScriptHwnd)
        WinClose, ahk_id %ID%
}
Return
I would like to know whether there is a better way of doing this, perhaps using one script, what do you think?

and also how to use the same key '+h' to open and close.

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

Re: So what would be the best way of doing this?

Post by mikeyww » 26 Jul 2021, 13:28

You could use a single script instead of two different ones. #If enables context sensitivity to limit hotkeys to conditions. You could even use a variable or toggle to create such a condition.

In general, multi-line hotkey routines should end in Return.

FoxEatsAces
Posts: 4
Joined: 26 Jul 2021, 12:46

Re: So what would be the best way of doing this?

Post by FoxEatsAces » 28 Jul 2021, 14:40

I contemplated what you suggest and looked up toggles, but I'm not very good with code so you may have to help me a bit.

I tried

Code: Select all


toggle	:= 0

+h::

    toggle  := !toggle

    if (toggle = 1){
       m::LButton
    }
    else{
        
        
    }

return

But it doesn't seem to work as expected, the m key remains the Lbutton so then I figured I'd put m::m in the else statement but I guess you can't do that, plus that raises further questions about the nature of reality...

How would I go about this? I want to toggle some key reassignments.

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: So what would be the best way of doing this?

Post by boiler » 28 Jul 2021, 14:50

As mikeyww mentioned, you use #If for hotkey context sensitivity. The following script toggles the m hotkey on and off when Shift+h is pressed. When it is toggled on, the m key works like the left mouse button.

Code: Select all

+h::toggle := !toggle

#If toggle
m::LButton

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

Re: So what would be the best way of doing this?

Post by mikeyww » 28 Jul 2021, 17:48

I agree with boiler.

In addition to the contemplation, if you click the link in my previous post, you can find an explanation as well as additional examples.

FoxEatsAces
Posts: 4
Joined: 26 Jul 2021, 12:46

Re: So what would be the best way of doing this?

Post by FoxEatsAces » 01 Aug 2021, 09:53

I'm sorry. I'm not very good understanding this stuff - and thanks for helping me!

I just have one more functionality I'd like to add. Basically I'd like to toggle a key press that is held down for intervals of 10 seconds, this is what I came up with

Code: Select all


apple:=0

!w::apple := !apple

#If apple
	
	while (apple=1)
	{
		Send {w Down}
		sleep 10000
		Send {w Up}
		sleep, 1000
	}
but it doesn't seem to work, a little help?

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: So what would be the best way of doing this?

Post by boiler » 01 Aug 2021, 10:14

Again, #If directives are only for determining whether a hotkey is active. It would go before a hotkey to make it conditional whether the hotkey routine is run at all. It does not make code conditional, so you wouldn't put it before a block of code as you did like a regular If statement.

Regarding your new request, this seems like what you want it to do:
#MaxThreadsPerHotkey 2

Code: Select all

!w::
	apple := !apple
	while apple
	{
		Send {w Down}
		sleep 10000
		Send {w Up}
		sleep, 1000
	}
return

Pressing Alt+W a second time would stop the looping, but the thread will still need to complete its 10 seconds before releasing the W key. If you want it to immediately release the W key upon toggling it off, this should do it:

Code: Select all

!w::
	apple := !apple
	Send, {w Up}
	while apple
	{
		Send {w Down}
		sleep 10000
		Send {w Up}
		sleep, 1000
	}
return
...but the thread will still be running for the rest of its time in that loop (the remaining time of the 10 seconds plus the extra second at the end. That would probably prevent it from being available to toggle back on until it completes that loop. If that's not acceptable, then you probably need to make use of Reload in some manner.

FoxEatsAces
Posts: 4
Joined: 26 Jul 2021, 12:46

Re: So what would be the best way of doing this?

Post by FoxEatsAces » 05 Aug 2021, 03:26

That's perfect thank you so much

I have one more thing I want to explore, I'm not really sure what I want either so it's an open question and I'd like your input.

The problem is I want to know whether any of my hotkeys are active, and the only indication of a change of function on my laptop is the capslock light. So I was wonder if it was possible to make use of this somehow?

Perhaps the simplest design might be to put the caps lock on when one or more triggers are active and then if I where to press the capslock off it would turn all hotkeys off. Still I'm not even sure if the light is a mechanical trigger or can be manipulated at all.

And maybe there's a smarter way to do it. Please tell me what you think.

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: So what would be the best way of doing this?

Post by boiler » 05 Aug 2021, 04:27

I’m guessing the light reflects the actual state of the CapsLock whether or not it is pressed physically or set virtually. If that’s the case, then the following script should flash that light on and off for several seconds:

Code: Select all

loop, 10 {
	SetCapsLockState, % Mod(A_Index, 2)
	Sleep, 500
}

If that worked as expected, then it would be possible for the script to turn that light on and off when it activates and deactivates your hotkeys, and you would also be able to make the hotkeys active or not by turning that light on or off by pressing the key. The drawback, of course, is that the CapsLock would be on whenever your hotkeys are active. If that’s not an issue for you, then that approach should work.

An alternative to using that light as an indicator of your hotkeys’ status is to have an on-screen indicator, such as a small dot/light in the corner of the screen that would be yellow when they are active and grey when they are not. If you used that approach, then you could even have multiple dots/colors indicating the status of multiple hotkeys separately.

Post Reply

Return to “Ask for Help (v1)”