Jump to content

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

Cursor position in Edit control?


  • Please log in to reply
3 replies to this topic
jsherk
  • Members
  • 65 posts
  • Last active: Jul 02 2014 12:27 PM
  • Joined: 11 Jun 2011
Is it possible to determine the cursors position in an Edit control? By position I mean how many characters? So if the edit control has no text in it, the position would be 0 or 1.

Thanks

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
Here's an example. I used the ControlGet commands so it should work on any Gui.

Gui, Add, Edit, w300 h300,
(
Go ahead
Put the cursor anywhere
I dare ya'
)
Gui, Add, Edit, vPos
Gui, Show
return


~Right::
~Left::
~Up::
~Down::
ControlGet, Line, CurrentLine,, edit1, A
ControlGet, Col, CurrentCol,, edit1, A
ControlGetText, Text, edit1, A

Pos := Col
Loop, Parse, Text, `n, `r
{
	If (A_Index = Line)
		break
	else
		Pos += StrLen(A_LoopField)
}
GuiControl,, Pos, Pos: %Pos%
return

GuiClose:
ExitApp

aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

jsherk
  • Members
  • 65 posts
  • Last active: Jul 02 2014 12:27 PM
  • Joined: 11 Jun 2011
ControlGet, CurrentCol is exactly what I needed! I was looking at GuiControlGet not ControlGet!

Thanks a lot

EDIT: Interesting issue is that when the Edit control is empty, it returns a column position of 4 (instead of 1), but as soon as I add any characters it will correctly show the position (2,3,4,5,etc). If you hit backspace, it will still show correctly until you get back to position 1 (empty) when it shows 4 again!

jonathan scott james
  • Members
  • 24 posts
  • Last active: May 04 2018 03:25 PM
  • Joined: 23 Feb 2015
what i'm wandering is if "x" "y" refers to the line, character, or to the screen x and screen y?
this is my way of returning the text insertion caret position in any size edit.
the return value is also the absolute character position of the caret in the edit control v variable(unless you add insert of delete some text without updating the user variable associated with the edit control associated "v" variable which to me is of the same name somehow but not equal  to it.
its the instr(myedit,findthistext,startposition) position -1
so  you can search for text at the returned value +1 with instr, then when you find it add another 1 to that value(like adding 2) and search for the next occurrence 
it stores it in "caretpos"
but it turns the edit blue for a split second. if someone knows a better way, please help
 
;***determine caret position****
;save clipboard
ClipboardB4:=Clipboard
;clear clipboard
CLIPBOARD
=
;copy edit text from first character to caret
SendInput,^+{home}^{Insert}
;determine length of clipboard copy(0 = instr pos 1)
caretpos:= strlen(Clipboard)
;restore clipboard
Clipboard=%ClipboardB4%
;restore caret
SendMessage, 0xB1, caretpos, caretpos,, ahk_id %hEdit1%
;*****end determine caret position***************