Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Sometimes ControlClick doesn't work.


  • Please log in to reply
4 replies to this topic
nnesori
  • Members
  • 16 posts
  • Last active: Jul 23 2009 05:03 PM
  • Joined: 14 Oct 2006
Hi! :)

I'm using ControlClick and ControlSend to automate some windows
The problem is
When moving the mouse during a ControlClick,
Sometimes ControlClick doesn't work.


so, I wrote my test script.
Below examples retrieves full fileName from the notepad.

ex) C:\temp\AhkTest\wtest.txt



To test below script.
Create a empty file with "wtest.txt" file name.

Then, open "wtest.txt" file with Notepad.exe

Run below script.

Sorry for my english.
Thanks for reading.


loop{
       ifWinExist, wtest.txt ahk_class Notepad
	      break
}

WinGet , editorid, ID

loop{
       strFullFileName:=getFullFileNameFromNotepad(editorID)
       outputdebug ,  %strFullFileName%
}
return


getFullFileNameFromNotepad( notepadId ) 
{    
       ;I use korean version Notepad.exe. so I am not sure below about Notepad.exe's texts.  If it doesn't work , you will have to adjust the texts.
;      strSaveAs:="다른 이름으로 저장"
;      strSaveIn:="저장 위치"
;      strYes:="예(&y)"

      strSaveAs:="Save As"
      strSaveIn:="Save in"
      strYes:="Yes(&y)"


      setTitleMatchmode, slow  
      DetectHiddenWindows, off   

      WinMenuSelectItem, ahk_id %notepadid%, , 1&, 4&  ; select "Save As"
      WinWait, %strSaveAs% ahk_class #32770
      WInGet, pop1id, ID  



      ControlClick, Button2, ahk_id %pop1id%    ; Click Save(&S)  


      WinWait, %strSaveAs% ahk_class #32770,  ,  ,  ,  %strSaveIn%    ;<----When mouse cursor  hover the Notepad window, script will stop at this line.
      Winget, pop2id, ID, %strSaveAs% ahk_class #32770,,,,     %strSaveIn%
      WinGetText, Output,  ahk_id %pop2id%
      if Not inStr(Output, strYes) 
      {
	         soundBeep, 750, 1000 
	         outputDebug, Wrong window !!!   pop1id=%pop1id%, pop2id=%pop2id%
	         pause
      }else{
	         Loop, parse, Output, `n,`r
	         {
	                  pos:=InStr( A_LoopField, ".txt")
	                  if(pos > 0){
	                     stringLeft, strEditorFullFileName, A_LoopField, pos+3
	      	      break
	                  }
	          }
      } 
      ControlSend,  ,{Alt Down}n{Alt Up}, ahk_id %pop2id%     ; select No
      WinWaitclose, ahk_ID %pop2id%
      ControlSend, ,{esc}, ahk_id %pop1id%
      WinWaitClose, %strSaveAs% ahk_id %pop1id%


        return strEditorFullFileName
}




so, I corrected.
Below source works fine.


loop{
       ifWinExist, wtest.txt ahk_class Notepad
	      break
}

WinGet , editorid, ID

loop{
       strFullFileName:=getFullFileNameFromNotepad(editorID)
       outputdebug ,  %strFullFileName%
}
return


getFullFileNameFromNotepad( notepadId ) 
{    
       ;I use korean version Notepad.exe. so I am not sure below about Notepad.exe's texts.  If it doesn't work , you will have to adjust the texts.
;      strSaveAs:="다른 이름으로 저장"
;      strSaveIn:="저장 위치"
;      strYes:="예(&y)"

      strSaveAs:="Save As"
      strSaveIn:="Save in"
      strYes:="Yes(&y)"


      setTitleMatchmode, slow  
      DetectHiddenWindows, off   

       WinMenuSelectItem, ahk_id %notepadid%, , 1&, 4&  ; select "Save As"
       WinWait, %strSaveAs% ahk_class #32770
       WInGet, pop1id, ID  


     ControlGet, OutVar, Hwnd , , Button2, ahk_id %pop1id% 
     subMyControlClickById( OutVar )  ;<-------------------it will works fine.


      WinWait, %strSaveAs% ahk_class #32770,  ,  ,  ,  %strSaveIn%    
      Winget, pop2id, ID, %strSaveAs% ahk_class #32770,,,,     %strSaveIn%
      WinGetText, Output,  ahk_id %pop2id%
      if Not inStr(Output, strYes) 
      {
	         soundBeep, 750, 1000 
	         outputDebug, Wrong window!!!    pop1id=%pop1id%, pop2id=%pop2id%
	         pause
      }else{
	         Loop, parse, Output, `n,`r
	         {
	                  pos:=InStr( A_LoopField, ".txt")
	                  if(pos > 0){
	                     stringLeft, strEditorFullFileName, A_LoopField, pos+3
	      	      break
	                  }
	          }
      } 
      ControlSend,  ,{Alt Down}n{Alt Up}, ahk_id %pop2id%     ; select No
      WinWaitclose, ahk_ID %pop2id%
      ControlSend, ,{esc}, ahk_id %pop1id%
      WinWaitClose, %strSaveAs% ahk_id %pop1id%


        return strEditorFullFileName
}

subMyControlClickById( h ){    
   postMessage, 0x201, 1,0x0 , , ahk_id %h%        ;WM_LButtonDOWN 
   postMessage, 0x202, 0,0x0 , , ahk_id %h%        ;WM_LButtonUp
}



Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
This is great because for about 3 years ControlClick has been unreliable and I never knew why. Thanks to you, I've discovered the reasons and documented them:

To improve reliability -- especially during times when the user is physically moving the mouse during the ControlClick -- try one or both of the following:

1) Use SetControlDelay -1 prior to ControlClick. This avoids holding the mouse button down during the click, which in turn reduces interference from the user's physical movement of the mouse.

2) Specify the string NA anywhere in the sixth parameter (Options). This avoids activating the window, and also avoids merging the script's input processing with that of the target window (which as a side-effect prevents physical movement of the mouse from interfering, but usually only when the target window is not active). [this mode requires the next version so it's not yet available]

However, these modes might not work for all types of windows and controls.

Thanks for reporting this.

nnesori
  • Members
  • 16 posts
  • Last active: Jul 23 2009 05:03 PM
  • Joined: 14 Oct 2006

1) Use SetControlDelay -1 prior to ControlClick.
This avoids holding the mouse button down during the click
, which in turn reduces interference from the user's physical movement of the mouse.


My original code works now.
Thanks Chris. :D

BullFrog
  • Guests
  • Last active:
  • Joined: --
And do you know how can i make SHIFT+CLICK with controlclick? :)

i tried {Shift Down} controlclick etc {Shift Up} but didnt work ><

also i tried with this +LEFT at the end of line :D didnt work ^^

any other idea?

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Something like the following might work in some applications like Notepad:
#space::  ; Win+Space hotkey.

ControlSend, Edit1, {Shift Down}, A

ControlClick, Edit1, A

ControlSend, Edit1, {Shift Up}, A

return
Maybe there are better ways, such as with PostMessage. Also, the window might have to be active and/or the mouse might have to be moved to the right position via MouseMove. But that might defeat your reason for using ControlClick.