Announcement

Collapse
No announcement yet.

Find screen coordinates of text insertion point

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Find screen coordinates of text insertion point

    Is there an API that will return the current screen coordinates of the text cursor (that is, the text insertion point)?

    I know that GetCursorPos returns the current screen position of the mouse cursor.

    In my program, I move the mouse to various points on the screen, left click at each point, and enter text. When done, I would like the mouse to left click at the original text insertion point so that any text the user types at that time will go to the same place the text was going before my program ran.

    I can't use GetCursorPos because that will give me the position of the mouse cursor, which may be different than the text insertion point.

    Also, the active window is the user's web browser. It is not my GUI (my application has no GUI), so there is no opportunity for me to do something special in the GUI design to allow the text insertion point to be determined.

  • #2
    See: GetCaretPos

    Regards,

    Pete.

    Comment


    • #3
      GetCaretPos not working right

      GetCaretPos is not working properly. It always returns the coordinates of the insertion point as 0,0. I tried it into different programs. Both programs always give the 0,0 result, although one program indicates that an error was encountered (error code 126) while the other program says no error was encountered.

      I did find a source code example in this forum for using GetCaretPos within a dialog box that the power basic program itself created. GetCaretPos in that program works properly when I run it, but when I use the same GetCaretPos syntax in my programs, I get these problems. Is there a different way of using GetCaretPos when I need to get the insertion point for the active window (where that window goes with an application I did not write) versus getting the insertion point for a dialog box created by the same power basic program? Even so, I don't understand why one of my programs always gives me an error code and the other one never gives me an error code.

      I've included the source code for both short programs below. Can anyone tell me what I need to do to get the valid insertion point coordinates?

      This program always returns the insertion point coordinates as 0,0 (and indicates that no error occurred on GetLastError). I call the program when the active window (containing the text insertion point) is Word, Notepad, etc. I run the program by typing a text sequence that is intercepted by the Autohotkey application, which in turn executes this program without making any changes in the active window or text insertion point.

      Code:
      #COMPILE EXE
      #DIM ALL
      #INCLUDE "WIN32API.INC"
      FUNCTION PBMAIN () AS LONG
      LOCAL pt AS POINTAPI
      SetLastError(0)
      GetCaretPos pt              ' grab the insertion point position
      msgbox "erorr code= "+STR$(GetLastError) ' check any errors
      MSGBOX STR$(pt.x)+$CRLF+STR$(pt.y) ' display position
      END FUNCTION
      The next program activates a window and types text into it before attempting to get the insertion point position. It also always returns the coordinates as 0,0 but after getting the coordinates the GetLastError returns error 126.

      Code:
      #COMPILE EXE
      #DIM ALL
      #INCLUDE "WIN32API.INC"
      FUNCTION PBMAIN () AS LONG
      
      dim programname as string
      dim windowname as string
      LOCAL pt AS POINTAPI
      
      SetLastError(0)
      
      ' ------ THESE LINES MAKE THE NOTEPAD WINDOW ACTIVE AND TYPES SOME TEXT INTO IT --------
      programname="sendkeystrokestowin.exe"
      windowname = "Notepad"
      SHELL programname+  " "+"|"+windowname+"|a|0|sample text for testing",0
      ' -----------------------------------------------------------------------------------------------------
      
       ' grab the insertion point position
      GetCaretPos pt
      msgbox "erorr code= "+STR$(GetLastError)
      MSGBOX STR$(pt.x)+$CRLF+STR$(pt.y) ' display position
      
      END FUNCTION

      Comment


      • #4
        Hi,

        Give this a try.

        Code:
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "WIN32API.INC"
        
        FUNCTION PBMAIN () AS LONG
           LOCAL hNotepadWindow, hNotepadThreadID, hFocusWindow AS DWORD, pt AS POINT
        
           SLEEP 3000 ' To allow time to switch to Notepad etc. for testing
        
           hNotepadWindow = GetForegroundWindow()
        
           hNotepadThreadID = GetWindowThreadProcessID( hNotepadWindow, BYVAL 0 )
        
           IF AttachThreadInput( GetCurrentThreadID, hNotepadThreadID, %True ) THEN
              hFocusWindow = GetFocus()
              IF GetCaretPos(pt) THEN
                 MSGBOX "Caret Pos: " & FORMAT$(pt.X) & ", " & FORMAT$(pt.Y)
              END IF
        
              AttachThreadInput GetCurrentThreadID(), hNotepadThreadID, %False
           END IF
        END FUNCTION
        Regards,

        Pete.

        Comment


        • #5
          [Carets Overview]
          The system provides one caret per message queue
          I can't find it in so many words in the rest of the doc, but if there is one caret per message queue the GetCaretPos() function cannot possibly work when called from another thread. The call parameters do not support a thread parameter, so I would think the only caret pos you can get would be that of the current thread.

          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Ooh, while I was typing... neat idea, AttachThreadInput()...

            note however...

            The AttachThreadInput function fails if either of the specified threads does not have a message queue

            The calling program will have to make a GDI call. (code not shown).
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Thanks Pete. That works fine...I integrated into my code. I surmise from your use of AttachThreadInput that my powerbasic program and the active window had separate input threads so that my program was not reading the position of the caret in the other thread.

              Also a follow up question. If the active window is totally maximized and if the mouse cursor is at the same position as the caret, then the x-screen coordinate from GetCursorPos equals the x-client coordinate from GetCaretPos. (If the active window is not maximized, then of course these two values are not equal.) But the y-coordinates for the two functions on a maximized window are not equal. Do you know why the x coordinates are equal but not the y coordinates? My program is OK regardless, because it has already separately determined the y screen coordinates of the text field so I really only need the x part from GetCaretPos, but I was just curious.

              Comment

              Working...
              X