Insert text at current position Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JustinNL
Posts: 28
Joined: 01 Sep 2020, 13:22

Insert text at current position

22 Jun 2021, 13:30

Hi guys,

I'm using AHK and using the text replacement a lot.

However, when I'm using the 'send' function it is quite slow at times.

I've changed this with a function SetText() or AddText()

Code: Select all

SetText(text) {
ControlGetFocus, current, A
ControlSetText, %current%, %text%, A
Send ^{end}
}

AddText(text) {
Sleep, 50
ControlGetFocus, current, A
ControlGetText, currenttext, %current%, A
ControlSetText, %current%, % currenttext . text, A
Send ^{end}
}
Settext replaces the entire textbox with the text of choice
Addtext adds the new text to the existing text
This is a lot faster than using send.

However, sometimes when I'm working in the middle of a textbox and I want to add the text at the current position (so with text before and after the cursor)
existing text,
(cursor is here and I want to place the new text here)
existing text
Is there a way to get the text before and after the cursor as a variable? so you can do:

Code: Select all

ControlSetText, %current%, % textbeforecursor . text . textaftercursor, A
Thanks in advance for your suggestions!
Best,
Justin
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: Insert text at current position

22 Jun 2021, 15:09

Untested. The idea is to insert something that is unique (here qwqwqw which I assume is never going to appear in any text you'll type/process) before getting the text, then replace it with the text you want to insert at that location.

Code: Select all

InsertText(text) {
Sleep, 50
Send qwqwqw
ControlGetFocus, current, A
ControlGetText, currenttext, %current%, A
currenttext:=StrReplace(currenttext,"qwqwqw",text)
ControlSetText, %current%, % currenttext . text, A
Send ^{end}
}
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Insert text at current position

22 Jun 2021, 15:56

JustinNL wrote:

Code: Select all

ControlGetFocus, current, A
What is the control name you are working with? The current variable contains it.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Insert text at current position

22 Jun 2021, 18:36

Instead of sending the text, which is rather slow, you can copy the text to the clipboard and just send ^v
JustinNL
Posts: 28
Joined: 01 Sep 2020, 13:22

Re: Insert text at current position

23 Jun 2021, 05:25

Thank you both for the suggestions!

I ended up with these codes

Code: Select all

InsertText1(text) {
Clipboard := text
Send ^v
}


InsertText2(text) {
Sleep, 50
Send ***
ControlGetFocus, current, A
ControlGetText, currenttext, %current%, A
currenttext:=StrReplace(currenttext,"***",text)
ControlSetText, %current%, % currenttext, A
;Send ^{end}
}
Both work fine, but I think I prefer nr 1 because it leaves the cursor at the current position. (there is a minor hesitation between typing the command and its effect but that is workable)

@teadrinker The control name differs quite a lot, it is RichTextWndClassxx, where xx is any number between 1 and 40.
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Insert text at current position  Topic is solved

23 Jun 2021, 06:43

JustinNL wrote: it is RichTextWndClassxx
This may work:

Code: Select all

InsertText(text) {
   ControlGetFocus, current, A
   SendMessage, EM_REPLACESEL := 0xC2, true, &text, % current, A
}
JustinNL
Posts: 28
Joined: 01 Sep 2020, 13:22

Re: Insert text at current position

23 Jun 2021, 13:05

@teadrinker
Thanks so much! This works even better, instantaneous text insertion and cursor remains at the 'current' position

Could you maybe elaborate what this does?
I could find EM_replacesel in the microscft docs, but what does the rest of the command do?
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Insert text at current position

23 Jun 2021, 13:29

@JustinNL
Could you specify, what the parameter is unclear?
JustinNL
Posts: 28
Joined: 01 Sep 2020, 13:22

Re: Insert text at current position

24 Jun 2021, 14:54

Actually just the 0xC2 :)

Or more specifically, why does

Code: Select all

EM_REPLACESEL := 0xC2
send the requested text to the textbox?
Just trying to comprehend a bit better why it does exactly work as it should. Its really useful!
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Insert text at current position

24 Jun 2021, 15:08

EM_REPLACESEL := 0xC2 serves just to document that the value 0xC2 which is passed as first parameter to sendmessage corresponds to the constant EM_REPLACESEL. For the program to work, you could omit EM_REPLACESEL := . This does exactly the same, as the variable EM_REPLACESEL is not used in the program after this assignment:

Code: Select all

 SendMessage, 0xC2, true, &text, % current, A
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Insert text at current position

24 Jun 2021, 15:19

JustinNL wrote: Just trying to comprehend a bit better why it does exactly work as it should
EM_REPLACESEL is a window message, when an edit (or edit-like) control receives this message, it knows what to do. :)
MSDN wrote:Replaces the selected text in an edit control or a rich edit control with the specified text.
...
If there is no selection, the replacement text is inserted at the caret.
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Insert text at current position

24 Jun 2021, 15:25

In addition, you can use such approach to append the text:

Code: Select all

AppendText(text) {
   static EM_SETSEL := 0xB1, EM_REPLACESEL := 0xC2
   ControlGetFocus, current, A
   SendMessage, EM_SETSEL, -2, -1, % current, A ; moves the caret to the end of text
   SendMessage, EM_REPLACESEL, true, &text, % current, A
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: iamMG and 133 guests