unworking script (and it's weird)

Ask gaming related questions (AHK v1.1 and older)
Flyjet
Posts: 10
Joined: 06 Jan 2023, 05:48

unworking script (and it's weird)

Post by Flyjet » 23 Mar 2023, 05:39

Hello everyone,



I'm looking for some help with a script I wrote to make it easier for me to play video games. I have a motor disability, which makes it difficult for me to hold down the W key in order to move my character forward.



Here's the script I came up with:


Code: Select all


#NoEnv

#SingleInstance force

#Persistent



; Creation of the Windows speech synthesis object

voice := ComObjCreate("SAPI.SpVoice")



; Initialization of the automatic walking variable to false

automatic_walk := false



; Assigning the shortcut to the console key (scan code 029)

$SC029::

    ; Inverting the value of the automatic_walk variable

    automatic_walk := !automatic_walk



    ; Announcing the state of the variable using speech synthesis

    if (automatic_walk) {

        voice.Speak("Automatic walking activated")

    } else {

        voice.Speak("Automatic walking deactivated")



        ; Sending the W key up if the variable is false

        SendInput, {w up}

    }

return



; Sending the W key down continuously if the variable is true

While automatic_walk{

    SendInput, {w down}

}



Sleep, 100 ; Pause of 100 milliseconds to avoid slowing down the computer



When I press the designated key to toggle the script on and off, the variable changes states correctly and the script announces whether it is on or off. However, even when the variable is true, the W key is not being sent.



I'm pretty sure it's a silly mistake, but I just can't seem to find it. That's why I'm reaching out for help. Can anyone see what I'm doing wrong?



Thank you in advance for your assistance.

[Mod edit: Moved topic to AHK v1 help, based on posted code.]

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

Re: unworking script (and it's weird)

Post by mikeyww » 23 Mar 2023, 06:57

Hello,

You have the wrong idea about how to create the loop, as you put it after a Return command, which means that it will never execute. A timer is a good fit with your goal.

Code: Select all

#Requires AutoHotkey v1.1.33
voice := ComObjCreate("SAPI.SpVoice")

SC029::
If GetKeyState("w") {  ; w is down
 SetTimer go, Off
 Send {w up}
 voice.Speak("Automatic walking deactivated")
} Else {               ; w is up
 SetTimer go, 100
 go()
 voice.Speak("Automatic walking activated")
}
Return

go() {
 Send {w down} ; Paused while voice is speaking
}

Flyjet
Posts: 10
Joined: 06 Jan 2023, 05:48

Re: unworking script (and it's weird)

Post by Flyjet » 24 Mar 2023, 04:22

Okay, I see. I thought the whole script was a loop and that it would work.

I just have to translate the audio message into French because that's my language, and it will be fine. However, as you may have noticed, I am not very familiar with the language, there are other features that I would like to implement, I will try by myself first, but I may come back to the community.

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

Re: unworking script (and it's weird)

Post by mikeyww » 24 Mar 2023, 06:12

If timing is important, I wouldn't bother with the speech. You could use a ToolTip, sound, or both as an alert. As noted in the script, the speech will interrupt the key sequences that you are sending. Even a simple SoundBeep is interruptive, as its default duration is 150 ms.

Flyjet
Posts: 10
Joined: 06 Jan 2023, 05:48

Re: unworking script (and it's weird)

Post by Flyjet » 25 Mar 2023, 08:34

Hey everyone,

After some trial and error and some success with not-too-complicated features, I have a new difficulty with an automatic sprint feature. The part of the script that concerns this feature should activate/deactivate a sprint mode when the Caps Lock key is pressed and hold down the left Shift key when any of the four movement keys (Z, Q, S, D) are pressed. However, it doesn't seem to work properly at the moment.

Here is the part of the code that should handle the automatic sprint feature:

Code: Select all

; Initialisation des variables
course := false

; Boucle principale
Loop
{
    ; Vérifie l'état de la touche Caps Lock
    if (GetKeyState("CapsLock", "T"))
    {
        ; Bascule le mode de course
        course := !course
        if (course)
        {
            voice.Speak("Mode course activé.")
        }
        else
        {
            voice.Speak("Mode course désactivé.")
            Send {LShift up}
        }
    }

    ; Si le mode de course est activé, vérifie l'état des touches de déplacement
    if (course)
    {
        if (GetKeyState("z", "P") || GetKeyState("q", "P") || GetKeyState("s", "P") || GetKeyState("d", "P"))
        {
            Send {LShift down}
        }
    }

    ; Pause pour éviter de surcharger le processeur
    Sleep 10
}
I have added some French audio messages and comments in this language. The execution time is not necessarily very important, so I may remove the audio messages once the feature is working.

Thanks in advance for your help!

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

Re: unworking script (and it's weird)

Post by mikeyww » 25 Mar 2023, 10:25

Code: Select all

#Requires AutoHotkey v1.1.33

CapsLock::
+CapsLock::
If !sprint := !sprint {
 Send {Shift up}
 sprint := False
 SoundBeep 1000
} Else SoundBeep 1500
Return

#If sprint
z::
q::
s::
d::
Send {Shift down}
SoundBeep 2000
Return
#If

Flyjet
Posts: 10
Joined: 06 Jan 2023, 05:48

Re: unworking script (and it's weird)

Post by Flyjet » 25 Mar 2023, 13:01

Thank you, you gave me the answer directly. Reading the code, there are some things I don't understand. Since I don't want to be just an aided, and I want to learn, can I ask you some questions.



Why didn't my code work in the first place? I have a feeling that it still has something to do with loops. But I don't know why I can't figure it out in this language when I have no problem with others

what do the second and third line of the script mean? I'm not sure I understand (I don't count the directive for the required version.)

I understood that you were doing conditional shortcuts but why does the language need #if, wouldn't it have been enough to put if, I know it wouldn't, but I want to know why.

Do you have any resources other than the documentation to learn? Documentation is good when you know what you are looking for. I would like to become a little more independent. I know there are tutorials on the Internet but I would like your opinion

Translated with www.DeepL.com/Translator (free version)

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

Re: unworking script (and it's weird)

Post by mikeyww » 25 Mar 2023, 15:45

You were using GetKeyState inside a loop. GetKeyState with the "P" parameter provides an instantaneous check of a physical key state. In a loop setting, you might get lucky and catch that key state properly, or you might not, because a key-down state is usually brief. My script uses hotkeys instead. That is more effective because the hotkey will always be triggered, given its context. When the goal is, "I want ___ to happen when key _____ is pressed," that is usually a good fit with a hotkey.

Line 3 of my script defines a hotkey for CapsLock. Line 4 defines a hotkey for Shift+CapsLock. As you can see, the subroutine is the same; there is only a single subroutine for both hotkeys. You can stack hotkeys in this manner. The second hotkey is necessary because the other keys send the Shift key down. When a modifier key is held, regardless of whether physically or logically, hotkeys will trigger only if they include the modifier or the wildcard (*).

The hotkey could include an If statement instead of using an #If context. One difference is that if the #If context is not met, then the keys will work normally, as they usually do. This is often desirable. In some cases, such as this one, the choice is often a stylistic preference, but there are sometimes small differences in performance, or other effects, too.

Documentation is good when you know what you need to find, when you want to learn all about a particular topic, or when you want to learn about all of the functions and syntax. You can read the documentation in installments (sections), and combine it with reading forum posts that look interesting or useful. If you examine the posts, you can see many examples and learn a lot of techniques to complement the documentation reading. That is what I have done. I then go back to the documentation iteratively, to learn about areas where I want more details than a forum post might provide.

Flyjet
Posts: 10
Joined: 06 Jan 2023, 05:48

Re: unworking script (and it's weird)

Post by Flyjet » 28 Mar 2023, 08:35

Hello, I have finished my script. It works correctly except for one detail. When I have the sprint mode activated, and I press Z even if the automatic walk mode is not activated, when I release Z, the character keeps moving. I have to press Z again to stop the walk. I don't see this behavior described anywhere in the code. I changed the auto walk function to include a variable because I thought that might be it but apparently not. So I deleted the go function too but it works the same.



I don't understand because when the sprint mode is activated, it shouldn't automatically enter in the case where the variable auto walk is true but everything happens as if it was the case. I searched a lot but I can't find why, so I come back here, I'm sorry if the solution is trivial.



I join the complete code with the comments translated into English and even the names of the variables.


Code: Select all


 This script allows to configure mouse buttons for video games using AutoHotkey.

; Definition of variables for the mouse and movement
leftButtonPressed := false
rightButtonPressed := false
sprint := false

; Enabling/disabling auto movement by pressing the SC029 key (or the console key).
; When auto movement mode is activated, the "Z" key is held down.
autoMove := false
SC029::
autoMove := !autoMove
if (autoMove == false) {
Send {z up}
SoundBeep, 500, 50
} else {
Send {z down}
SoundBeep, 1000, 50
}
Return

; Enabling/disabling sprint mode by pressing the CapsLock key.
CapsLock::
+CapsLock::
if !sprint := !sprint {
Send {Shift up}
sprint := False
SoundBeep, 500, 50
} else {
sprint := True
SoundBeep, 1000, 50
}
Return

; In sprint mode, the Shift key is held down when the Z, Q, S, or D keys are pressed.
#If sprint
z::
q::
s::
d::
Send {Shift down}
Return
#If

; If the X1 mouse button is pressed, it presses and releases the left mouse button.
XButton1::
If leftButtonPressed {
leftButtonPressed := false
Send {LButton up}
} else {
leftButtonPressed := true
Send {LButton down}
}
Return

; If the X2 mouse button is pressed, it presses and releases the right mouse button.
XButton2::
If rightButtonPressed {
rightButtonPressed := false
Send {RButton up}
} else {
rightButtonPressed := true
Send {RButton down}
}
Return

; The PageDown key scrolls the mouse wheel down.
$PgDn::Send {WheelDown}

; The PageUp key scrolls the mouse wheel up.
$PgUp::Send {WheelUp}

; If the minus (-) key on the numpad is pressed, it presses and releases the middle mouse button.
NumpadSub::
if (middleButtonPressed) {
middleButtonPressed := false
Send {MButton up}
} else {
middleButtonPressed := true
Send {MButton down}
}
Return


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

Re: unworking script (and it's weird)

Post by mikeyww » 28 Mar 2023, 10:06

Suggestions for you:
1. Test in Notepad. That helps you to visualize what is happening, and remove effects of games themselves.
2. Since the previous script works, see what you changed, because what you changed likely broke the script.
3. Find out which sections are executing, and what the values of your variables are. Add lines to the script to do that, or use one of the debugging tools.
4. Simplify & shorten your script to the essential parts so that you can fix the problem. After it's fixed, expand your script.

What is the Shift state before you press Z? What is the state afterwards? Are any hotkeys triggered, or none? And so on.

Flyjet
Posts: 10
Joined: 06 Jan 2023, 05:48

Re: unworking script (and it's weird)

Post by Flyjet » 31 Mar 2023, 04:58

Hi there,

I wanted to follow up on our previous conversation regarding the script I was having trouble with. I wanted to apologize for potentially bothering you with my last post on the thread. After some further testing, I realized that the error I believed to be in the code was actually caused by my own using mistake.

I also wanted to thank you for the help you provided me with at the beginning of the development process. Your assistance was greatly appreciated.

I do still have one remaining error to resolve, but I know how to fix it.

Thank you again for your help and patience.

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

Re: unworking script (and it's weird)

Post by mikeyww » 31 Mar 2023, 07:05

You are welcome. Glad it worked out.

Post Reply

Return to “Gaming Help (v1)”