Hacking an AHK edit box with PostMessage / SendMessage

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
subtropic man
Posts: 4
Joined: 30 Aug 2017, 20:31

Hacking an AHK edit box with PostMessage / SendMessage

23 Sep 2018, 00:18

I'm trying to add some hacks to select the neighboring word in the edit box. This is what I've got so far:

Code: Select all

Left::
Send {Right}
Send ^{Left}
Send {Left}
Send ^+{Left}
Return

Right:
Send {Left}
Send ^{Right}
Send ^{Right}
Send {Left}
Send ^+{Left}
Return
The problem with this is it stops working when you try to go past the first or last word. I can think of a few tricks to fix this but the easiest would be if I could somehow determine the text cursor position to see if it's on the first or last position, and add an extra keystroke if it is. Can you do this with PostMessage / SendMessage? I remember finding a PostMessage trick to make it scroll to the bottom of a text area
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Hacking an AHK edit box with PostMessage / SendMessage

23 Sep 2018, 03:21

Nice idea. Here's a way:

Code: Select all

Left:: ;Edit control - set selection to previous word
Right::  ;Edit control - set selection to next word
ControlGet, hCtl, Hwnd,, Edit1, A
SendMessage, 0xB1, -1, 0,, % "ahk_id " hCtl ;EM_SETSEL := 0xB1 ;deselect text
SendInput, % InStr(A_ThisHotkey, "Left") ? "^+{Left}" : "^+{Right}"
return

;some text for testing:
;aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa
;aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa
;aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa
;aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa
From an old version of this webpage I had saved (the new version states a different number instead of -1):
EM_SETSEL message | Microsoft Docs
https://docs.microsoft.com/en-us/window ... /em-setsel
If the start is 0 and the end is –1, all the text in the edit control is selected. If the start is –1, any current selection is deselected.
Last edited by jeeswg on 23 Sep 2018, 03:27, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Hacking an AHK edit box with PostMessage / SendMessage

23 Sep 2018, 03:27

Hallo,
why PostMessage / SendMessage?
Try:

Code: Select all

Gui, Add, Edit, r9 vMyEdit w135, The problem with this is it stops working when you try to go past the first or last word.
Gui, Show

Left::
X := A_CaretX, Y := A_CaretY
Send {Right}
Send ^{Left}
Send {Left}
Send ^+{Left}
If (X = A_CaretX And Y = A_CaretY)
{
	Send ^{End}
	Goto Right
}
Return

Right::
X := A_CaretX, Y := A_CaretY
Send {Left}
Send ^{Right}
Send ^{Right}
Send {Left}
Send ^+{Left}
If (X = A_CaretX And Y = A_CaretY)
{
	Send ^{Home}^{Right}
	Goto Left
}
Return
subtropic man
Posts: 4
Joined: 30 Aug 2017, 20:31

Re: Hacking an AHK edit box with PostMessage / SendMessage

23 Sep 2018, 20:10

Thanks guys, finally got it to work properly. I had a lot of trouble with single-character words, I needed the script to work with them while also not selecting neighboring spaces. I think this finally works:

Code: Select all

Gui, Add, Edit, r9 vMyEdit w135 +hwndhctl, T problem w this is it stops working when x you try to go past the first or last w
Gui, Show

Left::
X := A_CaretX, Y := A_CaretY
Send {Right}
Send ^{Left}
Send {Left}
Send ^+{Left}
Sleep, 1 ; A_Caret* is a little slow
If (X = A_CaretX And Y = A_CaretY)
{
	Send ^{Right}
	Send {Left}
	Send +{Home}
}
Return

Right::
firstchar := 0, lastchar := 0
SendMessage, 0xB0, &firstchar, &lastchar,, % "ahk_id " hctl ; EM_GETSEL - can get the length of selected text
If (Asc(lastchar) - Asc(firstchar) > 1) ; if I don't do this, it skips words following one-character words ; Asc is probably not the right way to read a DWORD
	Send {Right}
Send ^{Right}
Send ^+{Right}
X := A_CaretX, Y := A_CaretY ; check if we're at the end of the text
Send {Right}
Sleep, 1
If !(X = A_CaretX And Y = A_CaretY)
	Send {Left 2} ; we've gone one word too far
Send ^+{Left}
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 301 guests