Eric Eskildsen
Eric Eskildsen

Reputation: 4759

How to bind a hotkey to a class method in AutoHotkey

In AutoHotkey (1.1.29.01), how can I dynamically bind a hotkey to a class method?

class MyClass
{
    SayHi()
    {
        MsgBox Hi!
    }

    BindHotkey()
    {
        Hotkey, Enter, this.SayHi, On
    }
}

Error:

Target label does not exist

Upvotes: 2

Views: 710

Answers (1)

Eric Eskildsen
Eric Eskildsen

Reputation: 4759

Call Bind on the function, passing this, and store the result in a variable. Then pass the variable to Hotkey.

class MyClass
{
    SayHi()
    {
        MsgBox Hi!
    }

    BindHotkey()
    {
        SayHiFunc := this.SayHi.Bind(this)

        Hotkey, Enter, % SayHiFunc, On
    }
}

Upvotes: 5

Related Questions