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 post
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 post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ahha
      I seem to have lost my sense of how the Global scope works.  In this example $p is Global but I can't seem to set $p in function __One and then pass that version of $p to function __Two.  I thought you could set a Global to a new value from within a function and it would affect the Global value. 
      I've stared and played with this code for over an hour and I'm missing something really basic.  Help appreciated.
      #AutoIt3Wrapper_run_debug_mode=Y ; use this to debug in console window <--- LOOK Global $p = 999 MsgBox(0, "DEBUG", "MAIN - $p = '" & $p & "'") __One() Exit Func __One() ;Global $p ;this seems to make no difference - comment it out to see - how does one affect the Global in a function? MsgBox(0, "DEBUG", "Entering Func __One() - $p = '" & $p & "'") For $p = 1 to 3 MsgBox(0, "DEBUG", "IN Func __One() - $p = '" & $p & "'") __Two() Next EndFunc Func __Two() MsgBox(0, "DEBUG", "Entering __Two() $p = '" & $p & "'") EndFunc  
    • By PeterVerbeek
      This topic give you access to an AutoIt functions library I maintain which is called PAL, Peter's AutoIt Library. The latest version 1.26 contains 214 functions divided into these topics:
      window, desktop and monitor GUI, mouse and color GUI controls including graphical buttons (jpg, png) logics and mathematics include constants string, xml string and file string dialogues and progress bars data lists: lists, stacks, shift registers and key maps (a.ka. dictionaries) miscellaneous: logging/debugging, process and system info Change log and files section  on the PAL website (SourceForge).
      A lot of these functions were created in the development of Peace, Peter's Equalizer APO Configuration Extension, which is a user interface for the system-wide audio driver called Equalizer APO.
    • By abberration
      Software Installer
      Version: 2.0
      It's been been a long year, but I finally got some time to rework this project. I re-wrote everything from scratch because the old version was getting too complicated with so many options and sub-menus. This new version is much easier to use and I have been testing for a few days and it seems very stable.
      For those who are new to this software, it helps you install software silently/unattended. This new version tries to determine the silent switch automatically. You can also re-organize the order in which the software installs by dragging & dropping them in the listview. It now supports creating profiles and checks for missing software (and automatically unchecks them, so it does not attempt to install non-existent software). One feature I included was because I have seen several people on Youtube talk about disliking bright screens at night. So, now you can choose from a few color theme (half of them are dark). I dabbled a bit more into GDI+ to draw a few things and show my logo with a transparent background (hint: I'm not good at GDI+).
      Under the Help menu, you will find a User Guide, which goes through most of it's features. I included a new icon if you want to use when you compile the script (in the Assets > Misc folder).
      If you have questions, comments or suggestions, all are welcome. Hope you enjoy!
      Here it is in action:

       
      Software_Installer_2.0.zip
    • By Jemboy
      Recently I was working on a script with icons using GuiCtrkCreatIcon.
      I decided to change the sub folder name of the icons to a more meaning name, however made a typo.

      I tested the .exe on my test computer and it worked flawlessly (because both icon folder where on my test computer) 😁
      But after I installed the script on the intended computers , I got chaos!😵
      Zooming into the problem, I discovered, that because the icons could not be found, the ControlID were returned with a value of 0
      and thus played havoc within the GuiGetMsg() switch/case statement.
      I have been able to reproduce this  (see example)
      #include <GUIConstantsEx.au3> ;============================================================================================================ ; PLEASE, do not save this example in the example folder: C:\Program Files (x86)\AutoIt3\Examples\Helpfile ;============================================================================================================ Example() Func Example() GUICreate(" My GUI Icons", 250, 250) $Icon1 = GUICtrlCreateIcon("shell32.dll", 10, 20, 20) $Icon2 = GUICtrlCreateIcon(@ScriptDir & '\Extras\horse.ani', -1, 20, 40, 32, 32) $Icon3 = GUICtrlCreateIcon("shell32.dll", 7, 20, 75, 32, 32) GUISetState(@SW_SHOW) ;$Icon2 = -1 ; ==> When this line is uncommented the script "works", so -1 could be a potential fix. ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $Icon2 Beep (500,500) EndSwitch WEnd GUIDelete() EndFunc ;==>Example If you save the above script outside the Autoit example folder and run it, it will keep beeping because GuiCtrlCreatIcon did not find horse.ani and return $Icon2=0.
      At the moment GUICtrlCreateIcon () only returns the conntrolID on success and 0 on failure.
      I would like to propose a return of -1 on failure, so a existing and working script won't go awry when the icon can not be found.
       
    • By meety
      My au3 script program needs administrator privileges on win10 to be executed correctly, because it needs to be added to the boot---Script code:RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", @ScriptName, "REG_SZ", @ScriptFullPath)
      I need to let the program automatically get administrator privileges, so I use a function of #RequireAdmin. Although the program can be added to the boot, but does not execute after system boot up? why is that?
      What should I do?
      --------------------------------------------------------------------
      Update:
      I found that after using the #RequireAdmin function, the program cannot be executed after system boot up.
      An example is given below. after it is compiled, it will not be executed after system boot up in the following two ways.
      1. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\
      2. RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", @ScriptName, "REG_SZ", @ScriptFullPath)
      why is that?
      example
      #include <MsgBoxConstants.au3>
      #RequireAdmin
      Func test()
          MsgBox(0, "", "this is test message")
      EndFunc   ;==>test
      test()
       
       
×
×
  • Create New...