GetCaretPos

Just starting out? Need help? Post your questions and find answers here.
BlindMan
User
User
Posts: 32
Joined: Thu Aug 30, 2018 11:34 am

GetCaretPos

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4996
Joined: Sun Apr 12, 2009 6:27 am

Re: GetCaretPos

Post 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
Egypt my love
BarryG
Addict
Addict
Posts: 4226
Joined: Thu Apr 18, 2019 8:17 am

Re: GetCaretPos

Post 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.
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: GetCaretPos

Post 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
BarryG
Addict
Addict
Posts: 4226
Joined: Thu Apr 18, 2019 8:17 am

Re: GetCaretPos

Post 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.
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: GetCaretPos

Post 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
BarryG
Addict
Addict
Posts: 4226
Joined: Thu Apr 18, 2019 8:17 am

Re: GetCaretPos

Post by BarryG »

WindowFromPoint_() and GetAncestor() won't get webpage elements, which is the goal.
Post Reply