Jump to content

Problems with a Semi-Simple Macro


Recommended Posts

Hey there! I'm new to the forum and using AutoIt. I've been trying to create a program where I can enter numbers into Input Boxes and have them be sent to a different window. So far, I've been successful with the code below. However, as soon as the job is done, the program exits. I, as well as my friend (Computer Science major), have been trying to find a way to get it to stop exiting after it's finished.

Also, when the program exits, it seems to lock up my computer - as in when I click around, everything gets highlighted (like desktop icons). The only way I can get this to stop is if I press the CTRL button (or whatever button I used in the _IsPressed command) to get it to "unlock." Is there a way to get rid of this hindrance?

Here's my code.

#Include <WinAPI.au3>
#Include <Misc.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#Include <WinAPI.au3>
#Include <Misc.au3>
#include <String.au3>
$dll = DllOpen("user32.dll")

Opt('MustDeclareVars', 1)

Global $h_my_script = WinGetHandle(@ScriptName), $input1, $input2, $input3, $input4, $input5, $input6, $input7, $input8, $input9, $input10, $focused_window, $hWnd, $msg

Example1()

Func Example1()
    
    GUICreate("Webreg", 200, 320, -1, -1)
    GUISetBkColor(0xCC1100)
    GUICtrlCreateLabel('Enter Course Registration Numbers', 25, 15)
    GUICtrlCreateLabel('in the fields below!', 60, 30)
    $input1 = GUICtrlCreateInput('', 40, 60, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 1', 43, 85)
    $input2 = GUICtrlCreateInput('', 120, 60, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 2', 123, 85)
    $input3 = GUICtrlCreateInput('', 40, 100, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 3', 43, 125)
    $input4 = GUICtrlCreateInput('', 120, 100, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 4', 123, 125)
    $input5 = GUICtrlCreateInput('', 40, 140, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 5', 43, 165)
    $input6 = GUICtrlCreateInput('', 120, 140, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 6', 123, 165)
    $input7 = GUICtrlCreateInput('', 40, 180, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 7', 43, 205)
    $input8 = GUICtrlCreateInput('', 120, 180, 40, 20,0x2000)
    GUICtrlCreateLabel('Input 8', 123, 205)
    $input9 = GUICtrlCreateInput('', 40, 220, 40, 20,0x2000)
    GUICtrlCreateLabel('Input 9', 43, 245)  
    $input10 = GUICtrlCreateInput('', 120, 220, 40, 20,0x2000)
    GUICtrlCreateLabel('Input 10', 121, 245)
    GUICtrlCreateLabel('Written By: Alex Chiang', 88, 305)
    GUICtrlCreateLabel('Now press CTRL to fill in and submit', 25, 270)
    GUICtrlCreateLabel('your query!', 70, 285)
    GUISetState()

    While 1
        $msg = GUIGetMsg()
        $focused_window = winGetTitle("[ACTIVE]")
        $hWnd = WinGetHandle($focused_window)
        sleep(100)
                        If _IsPressed("11", $dll) Then
                            If $h_my_script <> $hWnd Then
                            _WinAPI_SetFocus($hWnd)
                                send(GUICtrlRead($input1))
                                send('{TAB}')
                                send(GUICtrlRead($input2))
                                send('{TAB}')
                                send(GUICtrlRead($input3))
                                send('{TAB}')
                                send(GUICtrlRead($input4))
                                send('{TAB}')
                                send(GUICtrlRead($input5))
                                send('{TAB}')
                                send(GUICtrlRead($input6))
                                send('{TAB}')
                                send(GUICtrlRead($input7))
                                send('{TAB}')
                                send(GUICtrlRead($input8))
                                send('{TAB}')
                                send(GUICtrlRead($input9))
                                send('{TAB}')
                                send(GUICtrlRead($input10))
                                send('{ENTER}')
                                ExitLoop
                            EndIf
                        _WinAPI_SetFocus($h_my_script)
                        EndIf
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete()
EndFunc

If my code is too complex or has too many extraneous things that are unnecessary, please feel free to modify my code (to better it)! Comments and tips are appreciated as well!

Thanks in advance.

Edited by DeltaZer0
Link to comment
Share on other sites

Remove the ExitLoop.

I tried that already, but...

If I do, the program just keeps looping and looping and sending the numbers a billion times over.

And then I can't even close the program without killing its process or ending the task.

^_^

P.S. That's the ExitLoop after the send('{ENTER}') that I got rid of which made that happen. I didn't get rid of the one in the "If" after that.

Edited by DeltaZer0
Link to comment
Share on other sites

So the _isPressed is registering as true even when the key is not held down? This may seem like a stupid question, but is the key stuck down?

The _IsPressed is registering as true even when I don't hold it down - I press it once and it runs through everything in the input fields and then presses enter and closes the program. So I only need to press CTRL and it sends everything (and then closes - but I don't want it to close)!

Did you consider using HotKeySet, or maybe accelerators? (They're heaps of fun, but the user's manual has the best examples.)

HotKeySet and Accelerators can't be used when focusing on a different window, I believe. The program is used for copying what is put into the input field and to be simulated onto another window. Example: If I set 23451 for Index 1 in my macro, I can minimize it or put it in the background, go to Microsoft Word, and press CTRL. 23452 will be copied to Microsoft Word instantaneously.

--

I mean, these problems are pretty minor, but I feel that if they aren't fixed, this macro is only 95% finished.

Edited by DeltaZer0
Link to comment
Share on other sites

Its closing because of the exitloop.

Hotkeys do not require a specific window to be in focus to work.

I know that it's closing because of the exitloop, but if I don't have the ExitLoop there the program just keeps looping and I can't close it at all without ending the task or stopping its process.

Can you show me an example of using HotKeySet that does not require a specific window to be in focus to work? I can't seem to get it to work for me.

Link to comment
Share on other sites

I didn't test this, but quickly skimming it, I think this should work.

Give it a try.

#Include <WinAPI.au3>
#Include <Misc.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#Include <WinAPI.au3>
#Include <Misc.au3>
#include <String.au3>
$dll = DllOpen("user32.dll")

Opt('MustDeclareVars', 1)

Global $h_my_script = WinGetHandle(@ScriptName), $input1, $input2, $input3, $input4, $input5, $input6, $input7, $input8, $input9, $input10, $focused_window, $hWnd, $msg

HotKeySet("{ESC}", "Example1")

While 1
   Sleep(100)
WEnd

Func Example1()
   
    GUICreate("Webreg", 200, 320, -1, -1)
    GUISetBkColor(0xCC1100)
    GUICtrlCreateLabel('Enter Course Registration Numbers', 25, 15)
    GUICtrlCreateLabel('in the fields below!', 60, 30)
    $input1 = GUICtrlCreateInput('', 40, 60, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 1', 43, 85)
    $input2 = GUICtrlCreateInput('', 120, 60, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 2', 123, 85)
    $input3 = GUICtrlCreateInput('', 40, 100, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 3', 43, 125)
    $input4 = GUICtrlCreateInput('', 120, 100, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 4', 123, 125)
    $input5 = GUICtrlCreateInput('', 40, 140, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 5', 43, 165)
    $input6 = GUICtrlCreateInput('', 120, 140, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 6', 123, 165)
    $input7 = GUICtrlCreateInput('', 40, 180, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 7', 43, 205)
    $input8 = GUICtrlCreateInput('', 120, 180, 40, 20,0x2000)
    GUICtrlCreateLabel('Input 8', 123, 205)
    $input9 = GUICtrlCreateInput('', 40, 220, 40, 20,0x2000)
    GUICtrlCreateLabel('Input 9', 43, 245) 
    $input10 = GUICtrlCreateInput('', 120, 220, 40, 20,0x2000)
    GUICtrlCreateLabel('Input 10', 121, 245)
    GUICtrlCreateLabel('Written By: Alex Chiang', 88, 305)
    GUICtrlCreateLabel('Now press CTRL to fill in and submit', 25, 270)
    GUICtrlCreateLabel('your query!', 70, 285)
    GUISetState()

    While 1
        $msg = GUIGetMsg()
        $focused_window = winGetTitle("[ACTIVE]")
        $hWnd = WinGetHandle($focused_window)
        sleep(100)
                        If _IsPressed("11", $dll) Then
                            If $h_my_script <> $hWnd Then
                            _WinAPI_SetFocus($hWnd)
                                send(GUICtrlRead($input1))
                                send('{TAB}')
                                send(GUICtrlRead($input2))
                                send('{TAB}')
                                send(GUICtrlRead($input3))
                                send('{TAB}')
                                send(GUICtrlRead($input4))
                                send('{TAB}')
                                send(GUICtrlRead($input5))
                                send('{TAB}')
                                send(GUICtrlRead($input6))
                                send('{TAB}')
                                send(GUICtrlRead($input7))
                                send('{TAB}')
                                send(GUICtrlRead($input8))
                                send('{TAB}')
                                send(GUICtrlRead($input9))
                                send('{TAB}')
                                send(GUICtrlRead($input10))
                                send('{ENTER}')
                                ExitLoop
                            EndIf
                        _WinAPI_SetFocus($h_my_script)
                        EndIf
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete()
EndFunc
Link to comment
Share on other sites

I didn't test this, but quickly skimming it, I think this should work.

Give it a try.

#Include <WinAPI.au3>
#Include <Misc.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#Include <WinAPI.au3>
#Include <Misc.au3>
#include <String.au3>
$dll = DllOpen("user32.dll")

Opt('MustDeclareVars', 1)

Global $h_my_script = WinGetHandle(@ScriptName), $input1, $input2, $input3, $input4, $input5, $input6, $input7, $input8, $input9, $input10, $focused_window, $hWnd, $msg

HotKeySet("{ESC}", "Example1")

While 1
   Sleep(100)
WEnd

Func Example1()
   
    GUICreate("Webreg", 200, 320, -1, -1)
    GUISetBkColor(0xCC1100)
    GUICtrlCreateLabel('Enter Course Registration Numbers', 25, 15)
    GUICtrlCreateLabel('in the fields below!', 60, 30)
    $input1 = GUICtrlCreateInput('', 40, 60, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 1', 43, 85)
    $input2 = GUICtrlCreateInput('', 120, 60, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 2', 123, 85)
    $input3 = GUICtrlCreateInput('', 40, 100, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 3', 43, 125)
    $input4 = GUICtrlCreateInput('', 120, 100, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 4', 123, 125)
    $input5 = GUICtrlCreateInput('', 40, 140, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 5', 43, 165)
    $input6 = GUICtrlCreateInput('', 120, 140, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 6', 123, 165)
    $input7 = GUICtrlCreateInput('', 40, 180, 40, 20, 0x2000)
    GUICtrlCreateLabel('Input 7', 43, 205)
    $input8 = GUICtrlCreateInput('', 120, 180, 40, 20,0x2000)
    GUICtrlCreateLabel('Input 8', 123, 205)
    $input9 = GUICtrlCreateInput('', 40, 220, 40, 20,0x2000)
    GUICtrlCreateLabel('Input 9', 43, 245) 
    $input10 = GUICtrlCreateInput('', 120, 220, 40, 20,0x2000)
    GUICtrlCreateLabel('Input 10', 121, 245)
    GUICtrlCreateLabel('Written By: Alex Chiang', 88, 305)
    GUICtrlCreateLabel('Now press CTRL to fill in and submit', 25, 270)
    GUICtrlCreateLabel('your query!', 70, 285)
    GUISetState()

    While 1
        $msg = GUIGetMsg()
        $focused_window = winGetTitle("[ACTIVE]")
        $hWnd = WinGetHandle($focused_window)
        sleep(100)
                        If _IsPressed("11", $dll) Then
                            If $h_my_script <> $hWnd Then
                            _WinAPI_SetFocus($hWnd)
                                send(GUICtrlRead($input1))
                                send('{TAB}')
                                send(GUICtrlRead($input2))
                                send('{TAB}')
                                send(GUICtrlRead($input3))
                                send('{TAB}')
                                send(GUICtrlRead($input4))
                                send('{TAB}')
                                send(GUICtrlRead($input5))
                                send('{TAB}')
                                send(GUICtrlRead($input6))
                                send('{TAB}')
                                send(GUICtrlRead($input7))
                                send('{TAB}')
                                send(GUICtrlRead($input8))
                                send('{TAB}')
                                send(GUICtrlRead($input9))
                                send('{TAB}')
                                send(GUICtrlRead($input10))
                                send('{ENTER}')
                                ExitLoop
                            EndIf
                        _WinAPI_SetFocus($h_my_script)
                        EndIf
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete()
EndFunc
Thanks for the help, but my friend (who is working on this program with me) told me that:

1) You have to press ESC to open the GUI (which we don't want - we want the GUI to open when we execute the program)

2) It runs a single instance, but after that single instance, you must fill in the input fields again (they are not saved)

3) If you press ESC more than once everything gets "messed up" again

So I don't think that's the best way of handling this situation without changing the actual method of operation (press ESC) - but I am new so what do I know, haha. If there was a better way to fix this, it'd be much appreciated though!

The example in the helpfile never creates a gui.

Right, but we want a GUI for it, haha. So I don't know what to do from here! >.<

So frustrating!

Link to comment
Share on other sites

Can you show me an example of using HotKeySet that does not require a specific window to be in focus to work?

If no gui is created then it clearly does not require a specific window to function so it can be used with ur gui.

Thanks for the help, but my friend (who is working on this program with me) told me that:

1) You have to press ESC to open the GUI (which we don't want - we want the GUI to open when we execute the program)

2) It runs a single instance, but after that single instance, you must fill in the input fields again (they are not saved)

3) If you press ESC more than once everything gets "messed up" again

Were not mind readers. Simply separate the code you want invoked by the hotkey. It should be fairly easy as he has done most of the work.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...