Need help in adding function to a script Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
montie
Posts: 7
Joined: 01 Dec 2022, 16:13

Need help in adding function to a script

Post by montie » 01 Dec 2022, 16:27

This code is to make a toggle key of a program into a keyhold:

Code: Select all

$insert::
send, {insert}
keywait, insert
send, {insert}
return
It needs to be so that when LButton Up is sent while Insert is held down, it won't send Insert again when it's released.

I'm not a programmer and only know the very basic stuff about AutoHotkey, so any help would be greatly appreciated.

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

Re: Need help in adding function to a script  Topic is solved

Post by mikeyww » 01 Dec 2022, 17:23

Welcome to this AutoHotkey forum!

Example with another key:

Code: Select all

$q::
hk := SubStr(A_ThisHotkey, 2)
Send {%hk%}
KeyWait, %hk%
Send {%hk%}
~LButton Up::hk := ""

montie
Posts: 7
Joined: 01 Dec 2022, 16:13

Re: Need help in adding function to a script

Post by montie » 02 Dec 2022, 09:17

Thanks! It works! But it took me a while to make it work. It seems I have to put the code in the correct order before it works.

My AHK file has two parts:

Photoshop hotkeys

Code: Select all

; Photoshop hotkeys and scripts=======================================================================
#IfWinActive, ahk_class Photoshop
#SingleInstance, Force
Return

^!+PgDn::
Run, E:\Work.exe
return


F1::Suspend, Toggle

:::Ctrl

\::^z

Insert::
Send, ^2
KeyWait, Insert            ; double press to select Mask
KeyWait, Insert, D T0.2        
if !ErrorLevel             
Send, ^\
Return


F10::x
return


-::
Send, b
KeyWait, -            ; wait for - to be released
KeyWait, -, D T0.2        ; and pressed again within 0.2 seconds
if !ErrorLevel             ; timed-out (only a single press)
Send, e
Return


!-::
Send, -
Return


F8::
Send, ^+i!smc1{Enter}
return


=::F12
return

!k::
Send, +0+0+5
return

!l::
Send, +1+0+0
return



~LAlt::
sendinput, {SC0E9 down} ;this is the scan code of an unassigned key. As long as you nor the system never use it for anything else, it can be used in THIS way to cancel the menu acceleration.
KeyWait, LAlt
return

~LAlt up::
sendinput, {SC0E9 up}
return


~RAlt::
;sendinput, {RAlt down}
sendinput, {SC0E9 down}
;;tooltip, Ralt is pressed
KeyWait, RAlt
;;tooltip, Ralt was released
return

~RAlt up::
;sendinput, {LAlt up}
sendinput, {SC0E9 up}
;;tooltip, 
return

Rwin::alt

~Rwin::
sendinput, {RAlt down}
sendinput, {SC0E9 down}
KeyWait, LWin
return

~Rwin up::
sendinput, {RAlt up}
sendinput, {SC0E9 up} 
return

AppsKey::alt

~AppsKey::
sendinput, {RAlt down}
sendinput, {SC0E9 down}
KeyWait, LWin
return

~AppsKey up::
sendinput, {RAlt up}
sendinput, {SC0E9 up} 
return

F9::^d
return

Mbutton::
send, {Space down}{LButton down}
return

Mbutton up::
send, {Space up}{LButton up}
return



myvariable = 0

+WheelUp::
if myvariable <= 0
{
Control, ChooseString, Photoshop, ComboBox2
myvariable++
return
}

if myvariable <= 1
{
Control, ChooseString, jpeg, ComboBox2
myvariable++
return
}

if myvariable <= 2
{
Control, ChooseString, png, ComboBox2
myvariable = 0
return
}
and Krita hotkeys

Code: Select all

; Krita hotkeys and scripts=======================================================================
CoordMode, Mouse, Screen
#IfWinActive, ahk_exe krita.exe
#SingleInstance, Force
Return

$Insert::
hk := SubStr(A_ThisHotkey, 2)
Send {%hk%}
KeyWait, %hk%
Send {%hk%}
~LButton Up::hk := ""

-::
Send, b
KeyWait, -            ; wait for - to be released
KeyWait, -, D T0.2        ; and pressed again within 0.2 seconds
if !ErrorLevel             ; timed-out (only a single press)
Send, e
Return

!-::
Send, -
Return

Home::
MouseGetPos, xpos, ypos
MouseClick, left, 1690, 444
MouseMove, xpos, ypos, 0
KeyWait, Home            ; wait for - to be released
KeyWait, Home, D T0.2        ; and pressed again within 0.2 seconds
if !ErrorLevel             ; timed-out (only a single press)
MouseClick, left, 1760, 444
MouseMove, xpos ,ypos, 0
Return

!Space::
send, {Space down}{RAlt down}
return

!Space up::
send, {Space up}{RAlt up}
return

So this is the code in question under Krita hotkeys

Code: Select all

$Insert::
hk := SubStr(A_ThisHotkey, 2)
Send {%hk%}
KeyWait, %hk%
Send {%hk%}
~LButton Up::hk := ""
For some reason, only that function doesn't work properly if Krita hotkeys section is placed after the Photoshop hotkeys section, but if I put Krita hotkeys before Photoshop hotkeys, it works. I originally had Photoshop hotkeys placed before Krita hotkeys so it took me a while to figure out how to make it work, but I still don't understand why it didn't work with the original arrangement.

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

Re: Need help in adding function to a script

Post by mikeyww » 02 Dec 2022, 12:48

Your code has some bugs.

1. #If directives apply only to hotkeys & hotstrings.

2. #If directives apply to all hotkeys & hotstrings below them, until the next #If.

3. Unlabeled code following Return is unreachable & never executes.

montie
Posts: 7
Joined: 01 Dec 2022, 16:13

Re: Need help in adding function to a script

Post by montie » 03 Dec 2022, 03:07

Thank you for pointing out the bugs. I honestly don't know how to fix it with the points you've listed, but I'll try to figure it out.

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

Re: Need help in adding function to a script

Post by mikeyww » 03 Dec 2022, 08:54

1. Combine the scripts into one.
2. Read documentation about auto-execute section.
3. Look for the following lines (mainly at "top level" of the script, instead of in a block): Return
4. See if what immediately follows it does not have a label (e.g., hotkey, hotstring, labeled subroutine).
5. Figure out whether to move such lines to the auto-execute section, or within a labeled subroutine.

As already noted, if a line such as a CoordMode command occurs immediately following a Return command, it is unreachable and will never execute. To understand this, follow your script line by line.

Post Reply

Return to “Ask for Help (v1)”