how to get the word at the cursor position

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

how to get the word at the cursor position

08 Feb 2016, 04:25

Code: Select all

ControlGet,count,CurrentLine,,Edit1, ahk_class Notepad
ControlGet,text,Line,% count,Edit1, ahk_class Notepad
Msgbox % text
It'll get the text of CurrentLine,but how to get the word at the cursor position(ahk_class Notepad)
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: how to get the word at the cursor position

08 Feb 2016, 07:46

Code: Select all

Send ^{right}^+{left}^c{right} ;move to end of word, move to start of word, selecting text, copy, leave cursor at end of word.
sleep, 100 ; wait for clipboard to update
If (SubStr(clipboard, 0) = " ") ;check if space at end of clipboard, if so remove it
	StringTrimRight, clipboard, clipboard, 1
MsgBox % clipboard
return
There may be a cleaner way to do it, but this seems to work for me. Although it will leave the text selected afterwards. If that's a problem, you can just send another ^{right} to move back to the end of the word without it being selected.

EDIT: Tidied up code a little
Last edited by SifJar on 08 Feb 2016, 11:56, edited 3 times in total.
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

Re: how to get the word at the cursor position

08 Feb 2016, 09:47

@SifJar
thx.but I wish it coded using window api.
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: how to get the word at the cursor position

08 Feb 2016, 11:51

xuezhe wrote:@SifJar
thx.but I wish it coded using window api.
Is there any particular reason? I don't know if that's possible or not, but I suspect it may well not be, and this works for sure.

EDIT: I also improved the code I posted before slightly, still a somewhat "hacky" method, but it works quite nicely. It now also trims a space from the end of the word if there is one (happens when there is a space after the word in the text that is open), and deselects the text afterwards. The only issue is that the cursor is not replaced where it was in the word. Unfortunately, I don't think there's a way around that, not that I can think of anyway.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: how to get the word at the cursor position

08 Feb 2016, 13:03

try this

Code: Select all

ControlGet,count,CurrentLine,,Edit1, ahk_class Notepad
ControlGet,text,Line,% count,Edit1, ahk_class Notepad
CaretX := floor(A_CaretX/8)
if RegExMatch(text, "^.{0," CaretX "}\h+\K\S+", Word)
	MsgBox % Word
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

Re: how to get the word at the cursor position

08 Feb 2016, 20:45

@SifJar yeah,right.I want to design a gui for Grammar tips in Emeditor.
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

Re: how to get the word at the cursor position

08 Feb 2016, 20:51

@AlphaBravo thx.but it only gets the last word.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: how to get the word at the cursor position

08 Feb 2016, 22:26

xuezhe wrote:@AlphaBravo thx.but it only gets the last word.
did you try it on Notepad per your original example?

Please show an example of some text, the position of carat and the value from this code

Code: Select all

MsgBox % A_CaretX
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

Re: how to get the word at the cursor position

08 Feb 2016, 22:31

now,I make it to following:

Code: Select all

ControlGet,count,CurrentLine,,Edit1, ahk_class Notepad
ControlGet,text,Line,% count,Edit1, ahk_class Notepad
ControlGet,count,CurrentCol,,Edit1, ahk_class Notepad

RegExMatch(SubStr(text,1,count-1),"([^,,\s\.\t]*$)",str)
RegExMatch(SubStr(text,count),"^[^,,\s,\.\t]*",string)
word:=str string
MsgBox % word
I've no idea about "\h\K" .

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk, Robt800 and 169 guests