Sign in to follow this  
Followers 0
mrex

Copy And Paste Blues

4 posts in this topic

Hey All,

I'm just getting started with AutoIT, but I'm already impressed with its size and scope. At the same time its doing some strange stuff when I try to do simple copy and paste work.

I have a GUI with three hot keys -- one to combine three fields on a form in a web page into one, another to pop up a drop down list and put the selected option into a field on a web page, and one to maximize the GUI.

The idea is to automate input into this web form, and reduce common tasks to hotkey combinations. The user runs the GUI, minimizes it, then brings up the web form.

The problem is, when I run the first hotkey (to combine the fields), it will end up pasting the wrong information, sometimes from other windows! I put some debugging traces in the routine, and it works perfectly when debugging is on. But as soon as I turn it off, it does random stuff.

Any ideas what I'm doing wrong? Is this related to using the GuiGetMsg function while the Hot Key function is running? Is there something I should do to sense the state of the Clipboard? Has anyone else seen this kind of problem before?

;
; ida.au3 -- Copyright 2010 *********, Inc.
;
;   iDesk Assistant -- a set of macro routines to automate keyboard tasks and provide
; QA proofing to *********.
;
; combineFields -- Combines the current field with the text of the next two fields,
; getField -- highlights the text in a field and returns it as a string
; setField -- Overwrites the text in a field on a form
; pasteSelect -- When the enter key is hit, pastes the selected item into the underlying 
; setKeys -- unhooks all hot keys, then hooks keys for either minimized or expanded use
; maximize -- Hot Key routine to maximize the window
; dropdown -- simple demo dropdown function
; pasteSelect -- When the enter key is hit, pastes the selected item into the underlying 
;
; Start main program.
;
#include <GuiConstantsEx.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>

AutoItSetOption( "MustDeclareVars", 1 )
AutoItSetOption( "SendKeyDelay", 50 )
AutoItSetOption( "SendKeyDownDelay", 10 )

dim $isMinimized            ; Main gui state

dim $inFunction = false;
dim $font
dim $logo                   ; MS graphic
dim $text
dim $mainHandle
dim $listHandle
dim $listID
dim $mainClose
dim $mainMinimize


; combineFields -- Combines the current field with the text of the next two fields,
; and pastes it into the first field
;
Func combineFields()
    
        ; Turn off this keystroke

    HotKeySet( "^+c" )
    $inFunction = true

    local $debug    = false
    if ( $debug ) then
        MsgBox( 0, "combineFields", "Starting" )
    endif
    
        ; Get the current text of the current field
        
    local $newstring = ""
    $newstring  = getField()
    if ( $debug ) then
        MsgBox( 0, "combineFields", "$newstring = [" & $newstring & "]" )
    endif
    
        ; Get the next field
        
    Send( "{TAB}" )
    $newstring  = $newstring & "_" & getField()
    if ( $debug ) then
        MsgBox( 0, "combineFields", "$newstring = [" & $newstring & "]" )
    endif
    
        ; Get the last field
        
    Send( "{TAB}" )
    $newstring  = $newstring & "_" & getField()
    ; Sleep( 500 )
    if ( $debug ) then
        MsgBox( 0, "combineFields", "$newstring = [" & $newstring & "]" )
    endif
   
        ; Get back to the first field
        
    Send( "+{TAB}+{TAB}" )  
    
        ; Paste the new string
        
    setField( $newstring )
            
        ; Turn the keystroke back on
        
    $inFunction = false
    HotKeySet( "^+c", "combineFields" )
EndFunc


;
; getField -- highlights the text in a field and returns it as a string
;
Func getField()
    local $fieldtext    = ""
    ClipPut( "" )
    
    Send( "^a^c" )
    ; Sleep( 100 )

        ; Read the clipboard

    $fieldtext  = ClipGet()
    
    return $fieldtext
EndFunc


;
; setField -- Overwrites the text in a field on a form
;
Func setField( $text )

    ClipPut( $text )
    ; Sleep( 100 )  
    
    Send( "^a^v{HOME}" )
    
EndFunc


;
; setKeys -- unhooks all hot keys, then hooks keys for either minimized or expanded use
;
Func setKeys( $isMinimized )

        ; Turn off all hotkeys
        
    HotKeySet( "^+c" )              ; combineFields
    HotKeySet( "^+h" )              ; maximize
    HotKeySet( "^+l" )              ; drop down
    
        ; Turn on keys appropriate to current state
        
    if ( $isMinimized ) then
        HotKeySet( "^+c", "combineFields" )
        HotKeySet( "^+h", "maximize" )
        HotKeySet( "^+l", "dropdown" )
    else
    endif
EndFunc


;
; maximize -- Hot Key routine to maximize the window
;
Func maximize()

    GuiSetState( @SW_RESTORE )
    
EndFunc


;
; dropdown -- simple demo dropdown function
;
Func dropdown()

    HotKeySet( "^+l" )                      ; turn off drop down
    HotKeySet("{ENTER}", "pasteSelect")     ; Catch the enter key

        ; Create a small window
        
    $listHandle = GuiCreate( "iDesk Assistant", 160, 140 )
    GuiSetIcon( @SystemDir & "\mspaint.exe", 0 )        ; Get the ********* icon
    
        ; Create a dropdown in the gui
    
    $listID = GuiCtrlCreatelist( "", 5, 5, 120, 100 )
    GUICtrlSetData( $listID, "88x31|120x240|120x60|120x600|120x90|125x125|160x600|180x150|234x60|240x400|250x250|300x100|300x250|300x600|336x280|468x60|720x300|728x90|Custom" )

        ; Show the window
        
    GuiSetState()
    While $curmsg <> $GUI_EVENT_CLOSE

            ;  Look for keys and control evnts

        $curmsg = GuiGetMsg() 
        Select

            Case $curmsg = $mainClose
                exit

                ; Titlebar Minimize

        EndSelect
    WEnd
    
        ; Switch back to the main gui window
        
    GuiSwitch( $mainHandle )
        
        ; reset keys
        
    HotKeySet( "{ENTER}" )          ; Turn off the Enter key    
    HotKeySet( "^+l", "dropdown" )  ; Slave dropdown
    
EndFunc


;
; pasteSelect -- When the enter key is hit, pastes the selected item into the underlying 
; program
;
Func pasteSelect()
    local $debug    = false;
    if ( $debug ) then
        MsgBox( 0, "pasteSelect", "Starting" )
    endif
    
        ; Turn off this keystroke
        
    HotKeySet( "{ENTER}" )
    
        ; Hide the GUI window
        
    GUISetState( @SW_HIDE );
    
    dim $curitem
    $curitem    = GUICtrlRead( $listID )
    setField( $curitem )
    
        ; Restore the GUI window
        
    GUISetState( @SW_RESTORE );
    
        ; Turn the keystroke back on
        
    HotKeySet( "{ENTER}", "pasteSelect" )
EndFunc


    ;
    ; Start main program.
    ;
    
    
    ; Create the main window.
    
$mainHandle = GuiCreate( "iDesk Assistant", 400, 400 )

    ; Draw the window
    
GUISetBkColor( 0xffffff )
;GuiSetIcon( @SystemDir & "\mspaint.exe", 0 )       ; Get/Create the ********* icon
$logo   = GUICtrlCreatePic("logo.jpg", 70, 10, 260, 100 )

    ; Add keystroke instructions
    
$font = "Verdana"
GUICtrlCreateLabel( "iDesk Assistant", 123, 120, 380, 20 )
GUICtrlSetFont( -1, 14, 800, 0, $font )             

$text = "The Assistant provides several routines to speed up your use of iDesk.  " & _
            "These routines are activated by hitting custom keystrokes while the Assistant " & _
            "is minimized."
GUICtrlCreateLabel( $text, 10, 160, 380, 50 ) 
GUICtrlSetFont( -1, 9, 400, 0, $font )              

$text   = "The current list of keystrokes are:"
GUICtrlCreateLabel( $text, 10, 211, 380, 12 ) 
GUICtrlSetFont( -1, 9, 400, 0, $font )              

$text   = "Ctrl-Shift-a: Combine Fields"
GUICtrlCreateLabel( $text, 60, 235, 380, 12 ) 
GUICtrlSetFont( -1, 9, 400, 0, $font )              

$text   = "Ctrl-Shift-l: Insert From Dropdown List"
GUICtrlCreateLabel( $text, 60, 250, 380, 12 ) 
GUICtrlSetFont( -1, 9, 400, 0, $font )              
    
$text   = "Ctrl-Shift-h: Show this window"
GUICtrlCreateLabel( $text, 60, 265, 380, 12 ) 
GUICtrlSetFont( -1, 9, 400, 0, $font )              
    
$mainMinimize   = GUICtrlCreateButton ( "Minimize", 120, 350, 75 )
$mainClose      = GUICtrlCreateButton ( "Close", 219, 350, 75 )
   
    ; Set the window state
    
$isMinimized    = false;
setKeys( $isMinimized );


    ; GUI MESSAGE LOOP

GuiSetState()

dim $curmsg
While $curmsg <> $GUI_EVENT_CLOSE
    
        ;  Look for keys and control evnts

    if ( $inFunction ) then
        Sleep( 100 )
    else
        $curmsg = GuiGetMsg() 
        Select

                ; Minimize button -- Shouldn't this just cue an event?

            Case $curmsg = $mainMinimize
                $isMinimized    = true
                setKeys( $isMinimized )
                GuiSetState( @SW_MINIMIZE );

                ; Minimize button -- Shouldn't this just cue an event?

            Case $curmsg = $mainClose
                exit

                ; Titlebar Minimize

            Case $curmsg = $GUI_EVENT_MINIMIZE
                $isMinimized    = true
                setKeys( $isMinimized )

                ; Taskbar restore

            Case $curmsg = $GUI_EVENT_RESTORE
                $isMinimized    = false
                setKeys( $isMinimized )

                ; Taskbar/title bar maximize

            Case $curmsg = $GUI_EVENT_MAXIMIZE
                $isMinimized    = false
                setKeys( $isMinimized )


        EndSelect
    endif   
    
WEnd

Share this post


Link to post
Share on other sites



While the function called by the HotKeySet() is running the rest of the script is blocked. It may be interrupted again by additional events, but will not return to main execution until that function returns.

Also, if you are working with a web page, why not use the _IE* functions? See help file.

:(


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks.

So it has nothing to do with the Gui polling loop, which I also confirmed over the last 2 hours. I'm still seeing strange results, but 2 things have helped a bit:

-- Sleep( 100 ) after cutting/pasting to the clipboard

-- Using ControlSend rather than Send

I still have an issue with stuck keys, but now I seem to be getting the text I should be.

-- Hard At It

While the function called by the HotKeySet() is running the rest of the script is blocked. It may be interrupted again by additional events, but will not return to main execution until that function returns.

Also, if you are working with a web page, why not use the _IE* functions? See help file.

:(

Share this post


Link to post
Share on other sites

Is it really necessary to use Send("^a^c"), ClipPut() and ClipGet()? What window are you working with? If it's IE, why not use the _IE* functions?

:(


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0