Just started working with ahk and I guess I don't understand the syntax?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Blyke_baylo
Posts: 3
Joined: 22 Mar 2023, 21:10

Just started working with ahk and I guess I don't understand the syntax?

Post by Blyke_baylo » 22 Mar 2023, 21:14

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#InstallMouseHook
#installKeybdHook
Coordmode, MouseGetPos, Relative
#HotkeyInterval 1000 ; Prevent Script from bugging out when you press the keys too quickly in succession.
Esc::ExitApp  ; Exit script with Escape key
^!s::Suspend  ; Suspend script with Ctrl+Alt+S
w::Up
s::Down
a::Left
d::Right
MouseGetPos, xpos, ypos
if xpos != 0 and ypos != 0
{
if GetKeyState(LButton, Down) == true
{
    if Abs(xpos) > Abs(ypos)
    {
        if xpos > 0
        {
            ControlSend,, Right
        }
        if xpos < 0
        {
            ControlSend,, Left
        }
    }
    if Abs(xpos) < Abs(ypos)
    {
        if ypos > 0
        {
            ControlSend,, Right
        }
        if ypos < 0
        {
            ControlSend,, Left
        }
    }
}
}
Looking to basically, while the mouse is help, depending on which coordinate it favors, press the arrow key corresponding to that direction. It's launching without errors, but the only part working is the arrow key rebinding to wasd.


[Mod action: Topic moved from "Ask for Help (v2)" since this is v1 code.]

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

Re: Just started working with ahk and I guess I don't understand the syntax?

Post by boiler » 22 Mar 2023, 21:22

The part you have below the hotkeys will never run. It needs to be in the Auto-execute Section above all the hotkeys as described in the Structure of a Script of the documentation.

Also, when you do move that code to the auto-execute section, it will only execute once. If you want it to keep checking, you need to put it in a loop or use SetTimer.

And you are using ControlSend without identifying a window, so you would be sending things to nowhere. And what you are sending are the words "Left", "Right", etc., not the keys. You need to put them in braces.

By the way, while it won't cause a problem, you have a bunch of lines repeated at the top for no reason.

Blyke_baylo
Posts: 3
Joined: 22 Mar 2023, 21:10

Re: Just started working with ahk and I guess I don't understand the syntax?

Post by Blyke_baylo » 22 Mar 2023, 21:41

Code: Select all

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#InstallMouseHook
#installKeybdHook
Coordmode, MouseGetPos, Relative
MouseGetPos, xpos, ypos
Loop
{

if xpos != 0 and ypos != 0
{
if GetKeyState(LButton, Down) == true
{
    if Abs(xpos) > Abs(ypos)
    {
        if xpos > 0
        {
            ControlSend,, {Right}
        }
        if xpos < 0
        {
            ControlSend,, {Left}
        }
    }
    if Abs(xpos) < Abs(ypos)
    {
        if ypos > 0
        {
            ControlSend,, {Right}
        }
        if ypos < 0
        {
            ControlSend,, {Left}
        }
    }
}
}
}
#HotkeyInterval 1000 ; Prevent Script from bugging out when you press the keys too quickly in succession.
Esc::ExitApp
^!s::Suspend 
w::Up
s::Down
a::Left
d::Right
this is what i'm looking at rn, and the same problem is continuing.

Blyke_baylo
Posts: 3
Joined: 22 Mar 2023, 21:10

Re: Just started working with ahk and I guess I don't understand the syntax?

Post by Blyke_baylo » 22 Mar 2023, 21:43

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#InstallMouseHook
#installKeybdHook
Coordmode, MouseGetPos, Relative
Loop
{
MouseGetPos, xpos, ypos
if xpos != 0 and ypos != 0
{
if GetKeyState(LButton, Down) == true
{
    if Abs(xpos) > Abs(ypos)
    {
        if xpos > 0
        {
            ControlSend,, {Right}
        }
        if xpos < 0
        {
            ControlSend,, {Left}
        }
    }
    if Abs(xpos) < Abs(ypos)
    {
        if ypos > 0
        {
            ControlSend,, {Right}
        }
        if ypos < 0
        {
            ControlSend,, {Left}
        }
    }
}
}
}
#HotkeyInterval 1000 ; Prevent Script from bugging out when you press the keys too quickly in succession.
Esc::ExitApp  ; Exit script with Escape key
^!s::Suspend  ; Suspend script with Ctrl+Alt+S
w::Up
s::Down
a::Left
d::Right
this is what I'm looking at and it's still having the same problem of only executing the bottom hotkeys

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

Re: Just started working with ahk and I guess I don't understand the syntax?

Post by mikeyww » 22 Mar 2023, 21:53

Hello,

You are looking at your script but perhaps did not look at each of boiler's points.
And you are using ControlSend without identifying a window.
1. Example of how to use If with expressions.

https://www.autohotkey.com/docs/v1/lib/IfExpression.htm#ExOne

2. You can design a new script for purposes of testing. It will have a single line: one ControlSend command. Use the proper syntax.

https://www.autohotkey.com/docs/v1/lib/ControlSend.htm

See if you can get the single line to work. If it doesn't work, then adding another 30 lines will not help it. After it works, you can expand the script.

My test is below. You can see if it works in Notepad.

Code: Select all

#Requires AutoHotkey v1.1.33
ControlSend Edit1, test, ahk_exe notepad.exe
How do you know what is executed? How do you know the values of your variables and function calls? You can use the script to find out, or use the AHK debugging tools.

Post Reply

Return to “Ask for Help (v1)”