Jump to content
Sign in to follow this  
Solarlight27

Function inside Functions/Hotkeys to Run Autoit Programs

Recommended Posts

Solarlight27

Can you have functions inside of functions for Autoit?

So I can use a hotkey to run the program

Here's the code I want to use it in:

#NoTrayIcon
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <ScrollBarConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <GuiEdit.au3>
#include <Misc.au3>[/color][/font]
[font=Lucida Grande][color=#333333]HotKeySet("", "_run")[/color][/font]
[font=Lucida Grande][color=#333333]Func _run()
While 1
$code = InputBox("Desktop Locker", "Safety. Please reset your password." & @CRLF & @CRLF & "Enter:New Password", Default, "", 250, 143, 370, 300)
If @error Then
Exit
ElseIf $code = '' Then
ToolTip("Please enter your password in the spaces.", 370, 300, "Desktop Locker", 1, 0)
Sleep(100)
ToolTip("")
Else
$plate = StringReplace($code, "|", @CRLF)
$Password = $plate
ExitLoop
EndIf
WEnd[/color][/font]
[font=Lucida Grande][color=#333333]$btn = MsgBox(36, "Desktop Locker", "You want to lock the screen with this code (" & $Password & ")? ")
If $btn = 6 Then
$Locked = GUICreate("Desktop Locker", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOPMOST)
GUISetBkColor(0x000000, $Locked)[/color][/font]
[font=Lucida Grande][color=#333333]$Progress1 = GUICtrlCreateProgress((@DesktopWidth - 400) / 2, @DesktopHeight / 2 + 20, 400, 10)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)[/color][/font]
[font=Lucida Grande][color=#333333]GUICtrlCreatePic("lock.gif", 430, 130, 164, 144)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)[/color][/font]
[font=Lucida Grande][color=#333333]$WrongPw = GUICtrlCreateEdit("", 10, 14, 300, @DesktopHeight - 14, BitOR($ES_AUTOVSCROLL, $ES_READONLY), 0)
$InputPw = GUICtrlCreateInput("", @DesktopWidth / 2 - 100, @DesktopHeight / 2 - 10, 200, 20, BitOR($ES_CENTER, $ES_PASSWORD), 0)[/color][/font]
[font=Lucida Grande][color=#333333]GUICtrlCreateLabel("Desktop Locker", @DesktopWidth / 2 - 125, @DesktopHeight / 2 - 95, 260, 50, $ES_CENTER, 0)
GUICtrlSetFont(-1, 30, 800, 0, "Arial")
GUICtrlSetColor(-1, 0xFFCC00)
GUICtrlSetBkColor(-1, 0x000000)[/color][/font]
[font=Lucida Grande][color=#333333]GUICtrlCreateLabel("Please enter a password to unlock the screen.", @DesktopWidth / 2 - 185, @DesktopHeight / 2 - 50, 400, 20, $ES_CENTER, 0)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)[/color][/font]
[font=Lucida Grande][color=#333333]GUICtrlSetFont($WrongPw, 16, 800, 0, "Arial")
GUICtrlSetColor($WrongPw, 0xFFCC00)
GUICtrlSetBkColor($WrongPw, 0x000000)
GUICtrlSetFont($InputPw, 12, 800, 0, "Arial")
GUICtrlSetColor($InputPw, 0xFFCC00)
GUICtrlSetBkColor($InputPw, 0x000000)[/color][/font]
[font=Lucida Grande][color=#333333]WinSetState("[CLASS:Shell_TrayWnd]", "", @SW_HIDE)
GUISetState(@SW_SHOW, $Locked)
_MouseTrap(@DesktopWidth, @DesktopHeight)[/color][/font]
[font=Lucida Grande][color=#333333]While 1
If _IsPressed("0D") Then
If GUICtrlRead($InputPw) == '' Then
ToolTip("Forgot your password. Please enter the correct", 509, 375, "", 1, 3)
ToolTip("")
ElseIf GUICtrlRead($InputPw) == $Password Then
For $i = 0 To 100 Step 10
GUICtrlSetData($Progress1, $i)
Sleep(1)
Next
GUISetState(@SW_HIDE, $Locked)
WinSetState("[CLASS:Shell_TrayWnd]", "", @SW_SHOW)
_MouseTrap()
ExitLoop
Else
GUICtrlSetData($WrongPw, _RandomMsg() & @CRLF & GUICtrlRead($WrongPw))
ToolTip("The password is incorrect ..try again.", 509, 375, "", 3, 3)
Sleep(25)
ToolTip("")
GUICtrlSetData($InputPw, "")
EndIf
EndIf
If Not WinActive("Desktop Locker") Then
WinActivate("Desktop Locker")
_MouseTrap(@DesktopWidth, @DesktopHeight)
GUICtrlCreateLabel("HAHA You thought, you thought. Nope No going around this bad boy. ", Random(@DesktopWidth - 540), Random(@DesktopHeight - 30), 540, 30)
GUICtrlSetFont(-1, 18, 800, 0, "Arial")
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0x000000)
EndIf
WEnd
Else
Exit
EndIf[/color][/font]
[font=Lucida Grande][color=#333333]Func _RandomMsg()
Local $RandomMsg[8]
$RandomMsg[0] = "Wrong Password!"
$RandomMsg[1] = "Step AWAY from the computer."
$RandomMsg[2] = "Hey, your not Me. "
$RandomMsg[3] = "Don't Touch!"
$RandomMsg[4] = "Please Enter Correct Password."
$RandomMsg[5] = "Ye Shall NOT Pass!!"
$RandomMsg[6] = "Just Give Up."
$RandomMsg[7] = "FAIL, In Epic Porportions!"
Return $RandomMsg[Random(8)]
EndFunc ;==>_RandomMsg
EndFunc

Thanks!

Edited by chause4

Share this post


Link to post
Share on other sites
BrewManNH

No, you can not have one function inside another one.

  • Like 1

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Melba23

chause4,

No, each function declaration must be contained within its own Func...EndFunc outside any other structure. But there is nothing to stop you doing that and calling _RandomMsg() from within _run(). :oops:

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Solarlight27

Okay Well I tried this and pulled _RandomMsg out of my run func

Now the program isn't working at all, even after the hotkey press:

;;#NoTrayIcon
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <ScrollBarConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <GuiEdit.au3>
#include <Misc.au3>
HotKeySet("`", "_run")
Func _run()
While 1
    $code = InputBox("Desktop Locker", "Safety. Please reset your password." & @CRLF & @CRLF & "Enter:New Password", Default, "", 250, 143, 370, 300)
    If @error Then
        Exit
    ElseIf $code = '' Then
        ToolTip("Please enter your password in the spaces.", 370, 300, "Desktop Locker", 1, 0)
        Sleep(100)
        ToolTip("")
    Else
        $plate = StringReplace($code, "|", @CRLF)
        $Password = $plate
        ExitLoop
    EndIf
WEnd
$btn = MsgBox(36, "Desktop Locker", "You want to lock the screen with this code (" & $Password & ")? ")
If $btn = 6 Then
    $Locked = GUICreate("Desktop Locker", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOPMOST)
    GUISetBkColor(0x000000, $Locked)
    $Progress1 = GUICtrlCreateProgress((@DesktopWidth - 400) / 2, @DesktopHeight / 2 + 20, 400, 10)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlCreatePic("lock.gif", 430, 130, 164, 144)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    $WrongPw = GUICtrlCreateEdit("", 10, 14, 300, @DesktopHeight - 14, BitOR($ES_AUTOVSCROLL, $ES_READONLY), 0)
    $InputPw = GUICtrlCreateInput("", @DesktopWidth / 2 - 100, @DesktopHeight / 2 - 10, 200, 20, BitOR($ES_CENTER, $ES_PASSWORD), 0)
    GUICtrlCreateLabel("Desktop Locker", @DesktopWidth / 2 - 125, @DesktopHeight / 2 - 95, 260, 50, $ES_CENTER, 0)
    GUICtrlSetFont(-1, 30, 800, 0, "Arial")
    GUICtrlSetColor(-1, 0xFFCC00)
    GUICtrlSetBkColor(-1, 0x000000)
    GUICtrlCreateLabel("Please enter a password to unlock the screen.", @DesktopWidth / 2 - 185, @DesktopHeight / 2 - 50, 400, 20, $ES_CENTER, 0)
    GUICtrlSetFont(-1, 12, 800, 0, "Arial")
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x000000)
    GUICtrlSetFont($WrongPw, 16, 800, 0, "Arial")
    GUICtrlSetColor($WrongPw, 0xFFCC00)
    GUICtrlSetBkColor($WrongPw, 0x000000)
    GUICtrlSetFont($InputPw, 12, 800, 0, "Arial")
    GUICtrlSetColor($InputPw, 0xFFCC00)
    GUICtrlSetBkColor($InputPw, 0x000000)
    WinSetState("[CLASS:Shell_TrayWnd]", "", @SW_HIDE)
    GUISetState(@SW_SHOW, $Locked)
    _MouseTrap(@DesktopWidth, @DesktopHeight)
    While 1
        If _IsPressed("0D") Then
            If GUICtrlRead($InputPw) == '' Then
                ToolTip("Forgot your password. Please enter the correct", 509, 375, "", 1, 3)
                ToolTip("")
            ElseIf GUICtrlRead($InputPw) == $Password Then
                For $i = 0 To 100 Step 10
                    GUICtrlSetData($Progress1, $i)
                    Sleep(1)
                Next
                GUISetState(@SW_HIDE, $Locked)
                WinSetState("[CLASS:Shell_TrayWnd]", "", @SW_SHOW)
                _MouseTrap()
                ExitLoop
            ElseIf GUICtrlRead($InputPw) == "chaseisawesome" Then
                For $i = 0 To 100 Step 10
                    GUICtrlSetData($Progress1, $i)
                    Sleep(1)
                Next
                GUISetState(@SW_HIDE, $Locked)
                WinSetState("[CLASS:Shell_TrayWnd]", "", @SW_SHOW)
                _MouseTrap()
                ExitLoop
    Exit
   Else
                GUICtrlSetData($WrongPw, _RandomMsg() & @CRLF & GUICtrlRead($WrongPw))
                ToolTip("The password is incorrect ..try again.", 509, 375, "", 3, 3)
                Sleep(25)
                ToolTip("")
                GUICtrlSetData($InputPw, "")
            EndIf
        EndIf
        If Not WinActive("Desktop Locker") Then
            WinActivate("Desktop Locker")
            _MouseTrap(@DesktopWidth, @DesktopHeight)
            GUICtrlCreateLabel("HAHA You thought, you thought. Nope No going around this bad boy. :P", Random(@DesktopWidth - 540), Random(@DesktopHeight - 30), 540, 30)
            GUICtrlSetFont(-1, 18, 800, 0, "Arial")
            GUICtrlSetColor(-1, 0xFF0000)
            GUICtrlSetBkColor(-1, 0x000000)
        EndIf
    WEnd
Else
    Exit
EndIf
EndFunc
Func _RandomMsg()
    Local $RandomMsg[8]
    $RandomMsg[0] = "Wrong Password!"
    $RandomMsg[1] = "Step AWAY from the computer."
    $RandomMsg[2] = "Hey, your not Chase. :P"
    $RandomMsg[3] = "Don't Touch!"
    $RandomMsg[4] = "Please Enter Correct Password."
    $RandomMsg[5] = "Ye Shall NOT Pass!!"
    $RandomMsg[6] = "Just Give Up."
$RandomMsg[7] = "FAIL, In Epic Porportions!"
    Return $RandomMsg[Random(8)]
EndFunc   ;==>_RandomMsg

Share this post


Link to post
Share on other sites
Melba23

chause4,

And where is the loop to keep the script alive while waiting for the HotKey? :oops:

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Solarlight27

Ummm I've never really used loops for when I've used hotkeys, I'm still new to Autoit, could explain- if you could- why I'd need a loop?

Edit: Oh! Okay I figured out what you meant about the loop part, but why is it important/how does it work?

Share this post


Link to post
Share on other sites
BrewManNH

You have nothing in there to keep the script running. As soon as you start it, it ends.

Add something like this just after your HotKeySet line

HotKeySet("~", "_Exit")
While 1
    Sleep(10)
WEnd
Func _Exit()
    Exit
EndFunc   ;==>_Exit

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Solarlight27

Alright, got it.

Thanks All

On a side note: does anyone know how to password protect an autoit program from running, like it asks for a password before continuing/executing the rest of the program?

Share this post


Link to post
Share on other sites
Melba23

chause4,

Loop an InputBox asking for the password and exit if you do not get it after X tries. :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Solarlight27

Got it!

Thank You Much

Share this post


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
Sign in to follow this  

×