Jump to content

Taking user input and calling a function from the input


Recommended Posts

Hi guys,

Could someone please tell me what I'm doing wrong with this code?
I'm trying to take user input and then run a function with the same name, but it's not calling the function.

So in the below after pressing Shift+F8 I would enter the text (without quotes) "testFunc".
As you can see I've also tried this with IF statements, I'm new to using Switch/Case.

HotKeySet("+{F8}", RunManually)

Func RunManually()
   ;Use a case statement with 1 hot key and an InputBox to manually run functions
   $funcName = InputBox("Which Func to Run?", "Enter the name of the function to run")
   MsgBox(0, "Entered value", $funcName)
   ;If $funcName = testFunc Then testFunc()
   ;If $funcName = test2Func Then test2Func()
   Switch $funcName
      Case testFunc
         MsgBox(0, "Calling", "Calling Function")
         testFunc()
         MsgBox(0, "Called", "Function call finished")
      Case test2Func
         MsgBox(0, "Calling", "Calling Function")
         test2Func()
         MsgBox(0, "Called", "Function call finished")
   EndSwitch
EndFunc

While 1
   ;testFunc()
   Sleep(1000)
WEnd

Func testFunc()
      MsgBox(0, "func running", "Seems to work!")
EndFunc

Func test2Func()
      MsgBox(0, "func 2 running", "2 Seems to work!")
EndFunc

Thanks!

Link to comment
Share on other sites

Use quotation marks you are comparing against an String value which is what InputBox get...

HotKeySet("+{F8}", RunManually)

Func RunManually()
   ;Use a case statement with 1 hot key and an InputBox to manually run functions
   $funcName = InputBox("Which Func to Run?", "Enter the name of the function to run")
   MsgBox(0, "Entered value", $funcName)
   
   ;If $funcName = "testFunc" Then
    ;   testFunc()
   ;EndIf
   ;If $funcName = "test2Func" Then
       ;test2Func()
   ;EndIf
   
   Switch $funcName
      Case "testFunc"
         MsgBox(0, "Calling", "Calling Function")
         testFunc()
         MsgBox(0, "Called", "Function call finished")
     Case "test2Func"
       MsgBox(0, "Calling", "Calling Function")
      test2Func()
         MsgBox(0, "Called", "Function call finished")
   EndSwitch
EndFunc

While 1
   ;testFunc()
   Sleep(1000)
WEnd

Func testFunc()
      MsgBox(0, "func running", "Seems to work!")
EndFunc

Func test2Func()
      MsgBox(0, "func 2 running", "2 Seems to work!")
EndFunc

Note both versions works, the one with the IF and the one with the Case...
 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...