Edit Control: caret position before of after selection?

Get help with using AutoHotkey and its commands and hotkeys
autocart
Posts: 98
Joined: 12 May 2014, 07:42

Edit Control: caret position before of after selection?

09 Oct 2014, 02:44

Hi all,
I can get the current caret position with "ControlGet, outputVar, CurrentCol, ...". However if text is selected then it will always return the position of where the selection starts.

Now, in real life, however, the caret could be at the start or at the end of the selection. The difference in behaviour becomes clear if I continue to select more text on top of what is already selected. In this case my current selection could get (partly) deselected or not, depending if the caret was at the start or end of the current selection.

I want to code this behaviour it on my own and I found some Edit Control libraries in the forums, but when I select text with "SendMessage, 0xB1, start, end, %Control%, %WinTitle% ; EM_SETSEL" then any current selection is completely replaced instead of taken into account.

So, is there a way to find out if the caret is positioned at the start or end of a current selection?
Thx to everybody helping.

EDIT:
Found a solution: http://arstechnica.com/civis/viewtopic.php?p=3347986#p3347986
I quote the author, "ToLazyToThink": "Calling EM_SETSEL with a -1 for the start position should undo the selection, leaving the caret where it was.

If you want a non-destructive method, I don't know of one. When I needed one I used the trick above and then used a bunch of hackery to restore the previous selection."
just me
Posts: 5425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Edit Control: caret position before of after selection?

09 Oct 2014, 04:12

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

?
autocart
Posts: 98
Joined: 12 May 2014, 07:42

Re: Edit Control: caret position before of after selection?

09 Oct 2014, 11:32

awesome, "just me", thx a lot!!

EDIT:
Well, I was happy too early. It works well in the example given, but in the 3rd party app where I am intending to use it
VarSetCapacity(POINT, 8, 0)
DllCall("User32.dll\GetCaretPos", "Ptr", &POINT)
X := NumGet(POINT, 0, "Int")
Y := NumGet(POINT, 4, "Int")
always returns 0 (zero) for X and Y, regardless of where I put the caret.
I think I have read somewhere (cant remember where) that GetCaretPos does not work with alllllll windows. Is it true?
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: autocart. MouseGetPos & edit controls

10 Oct 2014, 03:44

Could use MouseGetPos (consider CoordMode)
And see msdn in code, only works on MS (rich) edit controls.
Last edited by RobertL on 10 Oct 2014, 04:29, edited 1 time in total.
我为人人,人人为己?
autocart
Posts: 98
Joined: 12 May 2014, 07:42

Re: Edit Control: caret position before of after selection?

10 Oct 2014, 03:59

thx, RobertL, I think, though, that you are confusing caret-position with cursor-position?
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: autocart A_CaretX|Y

10 Oct 2014, 04:26

autocart wrote:that you are confusing caret-position with cursor-position?
Sorry, could try A_CaretX and A_CaretY, see Variables and Expressions \ Built-in Variables(index built-in variables).
我为人人,人人为己?
autocart
Posts: 98
Joined: 12 May 2014, 07:42

Re: autocart A_CaretX|Y

11 Oct 2014, 04:27

just me wrote:Again, sorry!
viewtopic.php?f=5&t=4840&p=27979#p27979
No problem, I felt helped anyway, thx.

RobertL wrote:
autocart wrote:that you are confusing caret-position with cursor-position?
Sorry, could try A_CaretX and A_CaretY, see Variables and Expressions \ Built-in Variables(index built-in variables).
No problem, and thx for the hint regarding A_CaretX and A_CaretY. I was not aware of them but now used them in my currently working work-around that I came up with:
http://ahkscript.org/boards/viewtopic.php?f=5&t=4840&p=28056#p28056
The relevant code of my work-around for the topic of this thread is this:

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

User
Posts: 141
Joined: 26 Jun 2017, 08:12

Re: Edit Control: caret position before of after selection?

26 Oct 2017, 21:58

The function only returns the correct "Caret" string position from edit controls created by the script itself!

It returns incorrect caret string position from edit controls created by another AutoHotKey scripts or external process such as notepad.exe, etc

Notepad test bug
get Caret string position (just me - Bug).gif
get Caret string position (just me - Bug).gif (614.22 KiB) Viewed 1151 times


Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4595
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Edit Control: caret position before of after selection?

26 Oct 2017, 23:20

I have a script that seems to be working correctly on Notepad's Edit control. Btw I'm finding that the Winapi function GetCaretPos is returning '0 0' always.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User
Posts: 141
Joined: 26 Jun 2017, 08:12

Re: Edit Control: caret position before of after selection?

27 Oct 2017, 00:48

autocart wrote:.

jeeswg wrote:.


@autocart @jeeswg

I just wrote right now CaretPos(Function) that returns the exact Caret string position!

if you are interested to test it, feel free to do it and in case you find any bug, please report here:

https://autohotkey.com/boards/viewtopic ... 15#p178615
User avatar
jeeswg
Posts: 4595
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Edit Control: caret position before of after selection?

27 Oct 2017, 01:31

I have one or two functions here re. Edit control selection:
GUI COMMANDS: COMPLETE RETHINK - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893&p=138292#p138292

The key things to be aware of are left/right v. anchor/active selection points. And that sometimes if you select from right to left, go to a different window, and return, the Edit control now thinks you selected from left to right. Where the caret is located is the active point in a text selection, the other point is the anchor point, when no text is selected, both active and anchor points are the same.

Although I think you know this, I think I'll mention it anyway, there are 2 quite different things: the X/Y coordinates of where the caret is, and the character index of where the caret/selection is. I'm generally never interested in the X/Y coordinates. Note: AutoHotkey has built-in variables A_CaretX and A_CaretY which give the X/Y coordinates of the caret, I wrote a function available at the link below, that tries to retrieve the same values as those variables.

convert coordinates between Client/Screen/Window modes - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38472
User
Posts: 141
Joined: 26 Jun 2017, 08:12

Re: Edit Control: caret position before of after selection?

27 Oct 2017, 20:07

jeeswg wrote:.


Hi @jeeswg,

Can you please tell me how is it supposed to use your "JEE_EditGetRangeAnchorActive()" function ?

with my function, "ToolTip, % CaretPos(ControlId)" automatically returns the correct Caret string pos!

with your function, I tried:

ToolTip, % JEE_EditGetRangeAnchorActive(ControlId, "", "") . " - " . vPos1 . "\" . vPos2

but the function returns nothing and vPos1 and vPos2 are always blank!

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

User
Posts: 141
Joined: 26 Jun 2017, 08:12

Re: Edit Control: caret position before of after selection?

27 Oct 2017, 23:09

jeeswg wrote:.


Sorry @jeeswg, I feel so pathetic from my previous post!

I thought "ByRef" is the same as "Global" variables, that's why I wasn't understanding how to use your function!

the ByRef info from this link made me even more confused (a lot blah blah blah and no practical examples):
https://autohotkey.com/docs/Functions.htm#ByRef

and then I found a practical ByRef example (code below)

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



I don't really know if the example is accurate, but it made me understand ByRef just a little bit!

Saying that, now I can handle your function! (I like your function, but I prefer mine because mine returns a value and it does not need to store the returned value into a variable! - That's why I think I don't really like the "ByRef" approach!)

Here is your function in action:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

User
Posts: 141
Joined: 26 Jun 2017, 08:12

Re: autocart A_CaretX|Y

28 Oct 2017, 14:37

autocart wrote:.


Hi @autocart,

I used your "A_CaretX - editX, A_CaretY - editY" approach to fix "just me" function! Now the function can be used with edit controls from notepad, from external apps or from another autohotkey scripts!

Now it's really easy to use the function, you just have to use one line like "EM_CHARFROMPOS(ControlId, CaretPos, CaretLine)" and the Caret string pos is automatically stored in "CaretPos" variable and the caret current line is automatically stored in "CaretLine" variable!

The Edit control border is what cause the issues mentioned below! By using "-E0x200" to remove border, the issues are fixed!
- but I noticed that, your approach is not always accurate, for example, the function returns 10 as Caret string pos when selected string Start\End is 9\9!
- another thing I noticed is that by using your approach, the "Caretxy" pos are always 2+ higher than "Caretxy" pos from "just me" approach! (for example, if I click the first string pos, "just me" approach returns 1\1 and yours 3\3! I don't know each one is more accurate!)

Anyway, I would recommend anyone to use my CaretPos(Function) instead since it is always accurate!

EM_CHARFROMPOS(Function) from "just me" fixed! (Sometimes it is not accurate as you can see in the image below!)
just me - Fuction Fixed.gif
just me - Fuction Fixed.gif (327.55 KiB) Viewed 1071 times


Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

Last edited by User on 28 Oct 2017, 21:39, edited 2 times in total.
User avatar
jeeswg
Posts: 4595
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Edit Control: caret position before of after selection?

28 Oct 2017, 15:02

To understand the 2-pixel difference, compare the values for raw and client, in the script below, and then look over my function.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User
Posts: 141
Joined: 26 Jun 2017, 08:12

Re: Edit Control: caret position before of after selection?

28 Oct 2017, 15:48

jeeswg wrote:.


tried your script, but it's lacking a function!
User avatar
jeeswg
Posts: 4595
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Edit Control: caret position before of after selection?

28 Oct 2017, 16:22

The function is in the link. It's more reliable to keep a function only in one place, in case you need to update it.
User
Posts: 141
Joined: 26 Jun 2017, 08:12

Re: Edit Control: caret position before of after selection?

28 Oct 2017, 16:50

jeeswg wrote:.


the edit control border is what cause the 2+ difference and all the issues I mentioned in post above!

By using "-E0x200" to remove border, the issues are fixed!

Do you know how to get the size of a control border?

Sem Título.png
Sem Título.png (23.12 KiB) Viewed 1037 times
User avatar
jeeswg
Posts: 4595
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Edit Control: caret position before of after selection?

31 Oct 2017, 07:58

Re. the 2 pixel discrepancy. There is:
- the caret position relative to the client area of the Edit control
- the caret position relative to the client area of the Notepad window

There is also:
- the caret position relative to the screen
- the caret position relative to the top-left corner of the Notepad window

If you see in my example here, which is based on the AutoHotkey source code for A_CaretX/A_CaretY:
convert coordinates between Client/Screen/Window modes - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38472&p=177058#p177058

I use GetWindowThreadProcessId specifying Notepad's hWnd, and I get returned hwndCaret, which is the Edit control's hWnd.

So I then convert: caret position (relative to Edit control's client area), to caret position (relative to screen), to caret position (relative to Notepad's client area).

For some other caret position info, see:
How to get control client area XY position? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=39129

This 2-pixel issue was seriously confusing me, when I first discovered it one or two weeks ago, but I managed to figure it out after a bit. Cheers.

==================================================

[EDIT:]
EM_CHARFROMPOS message (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb761566(v=vs.85).aspx
The coordinates of a point in the control's client area. The coordinates are in screen units and are relative to the upper-left corner of the control's client area.


[This is perhaps a little misleading, I believe that by window, this can actually mean control, which is also a type of window.]
GetCaretPos function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms648402(v=vs.85).aspx
The caret position is always given in the client coordinates of the window that contains the caret.


GetWindowInfo function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633516(v=vs.85).aspx
WINDOWINFO structure (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632610(v=vs.85).aspx
The coordinates of the client area.

Return to “Ask For Help”

Who is online

Users browsing this forum: Elendiar, Google [Bot], kilpsz and 67 guests