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
Cursor position in Edit control?
Started by
jsherk
, Jun 19 2011 01:14 AM
3 replies to this topic
#1
-
Posted 19 June 2011 - 01:14 AM
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
#2
-
Posted 19 June 2011 - 02:00 AM
aboutscript ⋰ apps ⋱ scripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
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!
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!
#3
-
Posted 19 June 2011 - 03:07 AM
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***************
;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***************
#4
-
Posted 01 September 2015 - 12:51 AM