Search code examples
autohotkeyautohotkey-2

"new" keyword is considered invalid


The new keyword is not working in AutoHotkey v2:

#Requires AutoHotkey v2.0

MsgBox "AHK Version: " A_AhkVersion

ih := new InputHook("L1 T0.5")
ih.Start()
key := ih.Wait()  ; Wait for one key press (or timeout)
ih.Stop()

MsgBox "Key pressed: " key

enter image description here

If I comment that out I get:

#Requires AutoHotkey v2.0

MsgBox "AHK Version: " A_AhkVersion

;ih := new InputHook("L1 T0.5")
;ih.Start()
;key := ih.Wait()  ; Wait for one key press (or timeout)
;ih.Stop()

;MsgBox "Key pressed: " key

enter image description here

How do I fix this?


Solution

  • The new operator has been removed. Instead, just omit the operator, as in MyClass(). To construct an object based on another object that is not a class, create it with {} or Object() (or by any other means) and set its base. __Init and __New can be called explicitly if needed, but generally this is only appropriate when instantiating a class.

    Source

    MsgBox "AHK Version: " A_AhkVersion
    
    ih := InputHook("L1 T0.5")
    ih.Start()
    key := ih.Wait()  ; Wait for one key press (or timeout)
    ih.Stop()
    
    MsgBox "Key pressed: " key