Page 1 of 1

GetCaretPos

Posted: Fri May 03, 2019 8:35 am
by BlindMan
Hi

I have a few questions about this code to Get Caret Position that I have put together from various sources.

Why are some Carets not detected? Eg. Caret in Notepad is detected. Caret in various web browsers is not.

Why is it that when this code is running, it is not possible to double click a desktop icon to launch an app?

How might this code be improved?

All advice greatly appreciated.

Code: Select all


Define StringCaretPos.Point

OpenWindow(0, 0, 0, 200, 160, "GetCaretPos", #PB_Window_SystemMenu)

EditorGadget(0, 0, 0, 200, 50, #PB_Editor_WordWrap)
SetGadgetText(0, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")

TextGadget(1, 20,  80, 160, 25, "")
TextGadget(2, 20, 110, 160, 25, "")

Repeat
  
  Event = WaitWindowEvent(100) 
  
  Select Event
      
    Case #PB_Event_CloseWindow
 
      End
      
    Default
             
      ThisID = GetCurrentThreadId_() 
      ForeID = GetWindowThreadProcessId_(GetForegroundWindow_(), 0) 
      
      If ThisID <> ForeID
        AttachThreadInput_(ThisID, ForeID, #True)
      EndIf     
      
      GetCaretPos_(@StringCaretPos)
      
      If (StringCaretPos\x <> 0) Or (StringCaretPos\y <> 0)
        focusedHandle = GetFocus_()      
        ClientToScreen_(focusedHandle, @StringCaretPos)
      EndIf      
      
      If ThisID <> ForeID
        AttachThreadInput_(ThisID, ForeID, #False)
      EndIf
      
      SetGadgetText(1, "Caret X:" + Str(StringCaretPos\x) + ", Y:" + Str(StringCaretPos\y)) 
      
      iters = iters + 1
      SetGadgetText(2, "Iteration " + Str(iters))
      
  EndSelect
  
ForEver

Re: GetCaretPos

Posted: Sun May 05, 2019 10:13 am
by RASHAD
You are using Windows
So you can use global mouse hook to get the mouse position at any place
But the position will be relative to the desktop
Or
Use another snippet for the web browser

Re: GetCaretPos

Posted: Mon Mar 16, 2020 5:12 am
by BarryG
BlindMan wrote:Why are some Carets not detected? Eg. Caret in Notepad is detected. Caret in various web browsers is not.
I can't help, but I can say this: PhraseExpress knows the caret location in any web browser, so it's certainly possible.

Re: GetCaretPos

Posted: Mon Mar 16, 2020 11:29 am
by Mijikai
Maybe this helps :)

The code gets the window under the cursor and then calculates
the actual cursor position within the windows client area.

If u modify the code u can restrict it to only worl for the current foreground window.

Code:

Code: Select all

EnableExplicit

Procedure.i GetWindowAndCursorPos(*Point)
  Protected hwnd.i
  Protected hwnd_test.i
  Protected mouse.i
  If GetCursorPos_(@mouse)
    hwnd = WindowFromPoint_(mouse)
    If hwnd
      hwnd_test = GetAncestor_(hwnd,#GA_ROOT)
      If hwnd_test
        hwnd = hwnd_test
      EndIf
      If ScreenToClient_(hwnd,@mouse)
        CopyMemory(@mouse,*Point,SizeOf(POINT))
        ProcedureReturn hwnd
      EndIf
    EndIf
  EndIf
  ProcedureReturn #Null
EndProcedure

Global pnt.POINT

Debug GetWindowAndCursorPos(@pnt);returns the window handle of the window under the mouse

Debug pnt\x;cursor x offset / client window
Debug pnt\y;cursor x offset / client window

End

Re: GetCaretPos

Posted: Mon Mar 16, 2020 11:41 am
by BarryG
We're not talking about mouse position, but caret position. The mouse can be at 0,0 but we want to know where we're typing.

Re: GetCaretPos

Posted: Mon Mar 16, 2020 11:54 am
by Mijikai
BarryG wrote:We're not talking about mouse position, but caret position. The mouse can be at 0,0 but we want to know where we're typing.
Only had a quick look :)
but it will work the same just swap the API

Re: GetCaretPos

Posted: Mon Mar 16, 2020 12:22 pm
by BarryG
WindowFromPoint_() and GetAncestor() won't get webpage elements, which is the goal.