Calling function from function with parameter Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Calling function from function with parameter

14 Feb 2020, 18:14

Hello,

The F1 and F2 part works flawlessly:

Code: Select all

F1::
KeyWait, F1, T0.2 ; Check if the key has been held down for less than 200 ms
if ErrorLevel = 1 ; Script 2 - Long press
{
  Function_2()
}
else ; Script 1 - Short press
{
  Function_1()
}
return

F2::
Short_Long_Function(func("Function_1"), func("Function_2"), A_ThisHotkey)
return

Function_1()
{
  MsgBox, short press
}
Function_2()
{
  MsgBox, long press
}
Short_Long_Function(param1, param2, key)
{
  KeyWait, %key%, T0.2 ; Check if the key has been held down for less than 200 ms
  if ErrorLevel = 1 ; Script 2 - Long press
  {
    param2.(myObject)
  }
  else ; Script 1 - Short press
  {
    param1.(myObject)
  }
}
Now I'm trying to pass parameters, but it does not work.
How to write the F3 part so that the functions get called with parameters?

Code: Select all

F3::
;Short_Long_Function(func("Function_3"), func("Function_4"), A_ThisHotkey) ; Works but without any parameter (Bug?)
;Short_Long_Function(func("Function_3(hello)"), func("Function_4(hello)"), A_ThisHotkey) ; Nothing happens when pressing F3
;Short_Long_Function(func("Function_3("hello")"), func("Function_4("hello")"), A_ThisHotkey) ; It says "Illegal character"
;Short_Long_Function(func("Function_3"(hello)), func("Function_4"(hello)), A_ThisHotkey) ; Works, but no parameter!
;Short_Long_Function(Function_3("hello"), Function_4("hello"), A_ThisHotkey) ; Both functions get called with parameter but short/long does not work
return

Function_3(text)
{
  MsgBox, short press %text%
}
Function_4(text)
{
  MsgBox, long press %text%
}

Short_Long_Function(param1, param2, key)
{
  KeyWait, %key%, T0.2 ; Check if the key has been held down for less than 200 ms
  if ErrorLevel = 1 ; Script 2 - Long press
  {
    param2.(myObject)
  }
  else ; Script 1 - Short press
  {
    param1.(myObject)
  }
}
My main aim is to avoid plenty of code lines like done in F1, because I use a lot of short/long key presses.
Having such a Short_Long function seems to be very handy!

Thanks for any help and best regards!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Calling function from function with parameter  Topic is solved

14 Feb 2020, 18:41

Code: Select all

F3::Short_Long_Function(func("Function_3").bind("hello"), func("Function_4").bind("hello"), A_ThisHotkey)

Function_3(text)
{
    MsgBox, short press %text%
}
Function_4(text)
{
    MsgBox, long press %text%
}

Short_Long_Function(param1, param2, key)
{
    KeyWait, %key%, T0.2 ; Check if the key has been held down for less than 200 ms
    if ErrorLevel = 1 ; Script 2 - Long press
    {
        param2.(myObject)
    }
    else ; Script 1 - Short press
    {
        param1.(myObject)
    }
}
HTH
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Calling function from function with parameter

15 Feb 2020, 05:30

Great, it works! :thumbup:
Thanks for the fast answer.

Best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: cjsmile999, doodles333, Lamron750 and 341 guests