Jump to content
Sign in to follow this  

How to get "word" from edit's caret position??

Recommended Posts



A simple question is:

How to get "word" from edit's caret position??


- I have some text in notepad ("word1 word2 word3 word4...")

Now I set caret position somewhere inside "word2", and I want return from control only this "word2"

It's something like use ControlCommand function with "GetSelected", "" but I want return text without marking word

This function was used eg. AutoIt3 FuncPopUp

Share this post

Link to post
Share on other sites


Here is a way to do it: :idea:

#Include <GuiEdit.au3>

; Start Notepad and enter some text, then start script

; Wait for Notepad to be active
; Get handle for Edit control
$hNotePadEdit = ControlGetHandle("[CLASS:Notepad]", "", "[CLASS:Edit; INSTANCE:1]")

; Where is caret?
$aCaretPos = _GUICtrlEdit_GetSel($hNotePadEdit)
; Where is beginning of line?
$iLineStartPos = _GUICtrlEdit_LineIndex($hNotePadEdit, -1)

; Get text of the line
$sLine = _GUICtrlEdit_GetLine($hNotePadEdit, _GUICtrlEdit_LineFromChar($hNotePadEdit, -1))

; Find ends of current word
$iStartPos = StringInStr($sLine, " ", 0, -1, $aCaretPos[0] - $iLineStartPos)
$iEndPos = StringInStr($sLine, " ", 0, 1, $aCaretPos[0] - $iLineStartPos)
If $iEndPos = 0 Then $iEndPos = StringLen($sLine) + 1
; Extract word
$sWord = StringMid($sLine, $iStartPos + 1, $iEndPos - $iStartPos - 1)

; Display word
MsgBox(0, "Found", $sWord)

Ask if anything is unclear. :)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
Share on other sites

Another way without GuiEdit.au3

Global $hNotePad = WinGetHandle("[Class:Notepad]")

HotKeySet("{Esc}", "_Exit")
HotKeySet("^g", "_GetWordFromCaret")

While 1

Func _GetWordFromCaret()
 Local $sRet
 Local $iLine = ControlCommand($hNotePad, "", "Edit1", "GetCurrentLine", "")
 Local $iCol = ControlCommand($hNotePad, "", "Edit1", "GetCurrentCol", "")
 Local $sLine = ControlCommand($hNotePad, "", "Edit1", "GetLine", $iLine)
 If StringMid($sLine, $iCol, 1) = " " Then $iCol -= 1
 Local $iFirst = StringInStr($sLine, " ", 0, -1, $iCol)
 $sLine = StringTrimLeft($sLine, $iFirst)
 Local $iLast = StringInStr($sLine, " ", 0, 1)
 If $iLast <> 0 Then
  $sLine = StringLeft($sLine, $iLast - 1)
 ConsoleWrite($sLine & @CR)

Func _Exit()

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this