[SOLVED] How To Paste Into an EditBox?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Capbat
Posts: 101
Joined: 17 May 2014, 13:33
Location: Québec Canada

[SOLVED] How To Paste Into an EditBox?

04 Feb 2015, 21:18

Hi All.

A Simple example follows, the intention is to present an EditBox then ask for the user to select a file, via 'FileSelectFile' and Get File Button
and paste the result in the cursor position of the editbox.
Problem is that after Executing the FileSelectFile the EditBox losses focus and when it gets back the Command Ctrl-V does not work. But if we paste using the Keyboard Ctrl-V it does.
How can I paste it automatically.

Thanks.

Code:

Code: Select all

#Singleinstance Force


Gui,Add, Edit, x10 y10 w630 h250 vEditNewProcedure
Gui,Add, Button, x10 y270 vSaveProcedureBtn , Save
Gui,Add, Button, x55 y270 vGetFileBtn gGetfileBtn , Get File
Gui,Add, Text, x110 y275 vGetFileInstruction , Type Click 'Get File' Place Cursor in Position and Press Ctrl-V
Gui,Show ,w650 h300, Edit Procedure
Return

GetFileBtn:			
{
	FileSelectFile, SelectedDocumentationFile,1,,Select Documentation to be associated.,*.*
	Clipboard=,%SelectedDocumentationFile%
	ControlFocus,EditNewProcedure,Edit Procedure
	SendInput ^v     		;This does not work.
	return
}
Last edited by Capbat on 06 Feb 2015, 13:37, edited 2 times in total.
enthused
Posts: 94
Joined: 27 Dec 2014, 03:28

Re: How To Paste Into an EditBox?

04 Feb 2015, 22:24

I am not sure I understand but is this what you are going for?

Code: Select all

#Singleinstance Force


Gui,Add, Edit, x10 y10 w630 h250 vEditNewProcedure
Gui,Add, Button, x10 y270 vSaveProcedureBtn , Save
Gui,Add, Button, x55 y270 vGetFileBtn gGetfileBtn , Get File
Gui,Add, Text, x110 y275 vGetFileInstruction , Type Click 'Get File' Place Cursor in Position and Press Ctrl-V
Gui,Show ,w650 h300, Edit Procedure
Return

GetFileBtn:        
{
    FileSelectFile, SelectedDocumentationFile,1,,Select Documentation to be associated.,*.*
    Clipboard=,%SelectedDocumentationFile%
    ControlFocus, %EditNewProcedure%
    SendInput ^v            ;This does not work.
    return
}
User avatar
boiler
Posts: 16964
Joined: 21 Dec 2014, 02:44

Re: How To Paste Into an EditBox?

04 Feb 2015, 22:47

@Capbat: ControlFocus doesn't work on the variable name you assigned to the control. It's the class name and instance of the control that you would find using Window Spy, or you can use the HWND of the control.

@enthused: I think you may have misread that line to be a GuiControl line or something because the change you suggested looks like you're trying to stuff the contents of the edit box into the control, but ControlFocus is just putting focus onto it, if you were to identify it correctly.

I believe the thing you want to do is use Gui, Submit, NoHide so that EditNewProcedure will contain what was edited so far, then insert the text contained in SelectedDocumentationFile into the right place, and place it back into the edit box using GuiControl. The problem is inserting it into the right point. Maybe you can have the user indicate the place where it belongs by leaving a placeholder like "<>" and your script will replace "<>" with the contents of SelectedDocumentationFile before stuffing it back into the edit control.
User avatar
boiler
Posts: 16964
Joined: 21 Dec 2014, 02:44

Re: How To Paste Into an EditBox?

04 Feb 2015, 22:55

This works.

Code: Select all

#Singleinstance Force


Gui,Add, Edit, x10 y10 w630 h250 vEditNewProcedure
Gui,Add, Button, x10 y270 vSaveProcedureBtn , Save
Gui,Add, Button, x55 y270 vGetFileBtn gGetfileBtn , Get File
Gui,Add, Text, x110 y275 vGetFileInstruction , Place '<>' where you want the file name inserted then click 'Get File'.
Gui,Show ,w650 h300, Edit Procedure
Return

GetFileBtn:         
	Gui, Submit, NoHide
	FileSelectFile, SelectedDocumentationFile,1,,Select Documentation to be associated.,*.*
	StringReplace, EditNewProcedure, EditNewProcedure, <>, %SelectedDocumentationFile%
	GuiControl,, EditNewProcedure, %EditNewProcedure%
return
Edit: cleaned up the instructions
enthused
Posts: 94
Joined: 27 Dec 2014, 03:28

Re: How To Paste Into an EditBox?

04 Feb 2015, 23:09

boiler wrote:@enthused: I think you may have misread that line to be a GuiControl line or something because the change you suggested looks like you're trying to stuff the contents of the edit box into the control, but ControlFocus is just putting focus onto it, if you were to identify it correctly.
I may have misunderstood the question as I thought he wants to use the clipboard and ctrl v to paste into Edit control.
User avatar
boiler
Posts: 16964
Joined: 21 Dec 2014, 02:44

Re: How To Paste Into an EditBox?

04 Feb 2015, 23:15

Yeah, I just don't think that's going to work the way he wants it.
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: How To Paste Into an EditBox?

05 Feb 2015, 01:45

paste the result in the cursor position of the editbox

Code: Select all

#Singleinstance Force

Gui,Add, Edit, x10 y10 w630 h250 vEditNewProcedure hWndhEdit ;  added hWndhEdit in case you choose to use the Control's handle
Gui,Add, Button, x10 y270 vSaveProcedureBtn , Save
Gui,Add, Button, x55 y270 vGetFileBtn gGetfileBtn , Get File
Gui,Add, Text, x110 y275 vGetFileInstruction , Type Click 'Get File' Place Cursor in Position and Press Ctrl-V
Gui,Show ,w650 h300, Edit Procedure
Return

GetFileBtn:
    FileSelectFile, SelectedDocumentationFile,1,,Select Documentation to be associated.,*.*
	Gui, +LastFound ; set the last found window to our gui
	Control, EditPaste, %SelectedDocumentationFile%, Edit1
	
	; you can play it better if:
	; you give the Edit controla handle and use it like this
	; Control, EditPaste, %SelectedDocumentationFile%,, ahk_id %hEdit%
return
User avatar
Capbat
Posts: 101
Joined: 17 May 2014, 13:33
Location: Québec Canada

Re: How To Paste Into an EditBox?

05 Feb 2015, 20:18

Thanks All for all your response.
@enthused:
I agree the question might not have been clear. I did was thinking of using Ctrl-V but it was a second choice, considering all the problems I had to insert it automatically in the EditBox, MJs came with the right idea.
@Boiler:
Thanks You helped me understand the ControlFocus better, specificly with Variables. And your idea about <> is interesting I will remember that for future script or [...]. And fortunatly MJs showed us that it is possible. Thanks.
@MJs
I am happy that you came along, again, because it was exactly what I wanted originally. You made me go back to TGB (The Good Book) and learn some more about Handles, +Lastfound and even EditPaste that I was not aware of. You are NO longer my mentor, you are my GURU.

You will all be cited in Collaborators section of my Epic App. ;)

Thanks Again.

Bat
User avatar
boiler
Posts: 16964
Joined: 21 Dec 2014, 02:44

Re: How To Paste Into an EditBox?

05 Feb 2015, 20:38

Capbat wrote:@Boiler:And your idea about <> is interesting I will remember that for future script or [...]. And fortunatly MJs showed us that it is possible.
I don't know if you tried my script, but it is already fully working.
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: How To Paste Into an EditBox?

05 Feb 2015, 21:11

boiler wrote:I don't know if you tried my script, but it is already fully working.
yes, it is fully working, there is nothing wrong with
@Boiler:
Thanks You helped me understand the ControlFocus better, specificly with Variables. And your idea about <> is interesting I will remember that for future scrip
either, but your approach has some cavities, one of which it's not always the right pos, since <> can exist more than once, and the first might not be the one that's needed, and the need to put characters in the control :o
just to replace them later
User avatar
boiler
Posts: 16964
Joined: 21 Dec 2014, 02:44

Re: How To Paste Into an EditBox?

06 Feb 2015, 01:07

Well, they can choose any unique indicator they want. I chose <>, but they can choose any character(s) which they are sure not to appear elsewhere. I actually think it's pretty nice that they can type up the whole entry and not worry about where they leave the cursor because the location is positively identified in the text itself. I guess you could call having to type "<>" a cavity, but it seems like a pretty positive way to ensure the file name will go exactly where you want it.

If they want to avoid typing any extra characters, it can easily be changed to append to the end of the text that was typed when they pressed the button for selecting the file. Then they can continue typing after that.
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: How To Paste Into an EditBox?

06 Feb 2015, 03:05

I'm not getting what you're debating, I answered for IS IT WORKING?, now you're saying it's practical solid approach, and there is no problem with at all, you don't see a problem with what you're doing?
if you were a user would like a programmer to ask you:
Please put bla somewhere to put a file's path in its place when you select one, even though it should just be where ever the caret is, and one more thing please make sure the bla isn't in the text more than once, unless you want me to put the file's path in the first bla, OK?
but it seems like a pretty positive way to ensure the file name will go exactly where you want it.
define positive way, is it working way? it is working
If they want to avoid typing any extra characters, it can easily be changed to append to the end of the text that was typed when they pressed the button for selecting the file. Then they can continue typing after that.
how would go and do that?
- get the text in the edit control, by Gui, Submit, GuiControlGet, ControlGetText or Control+C :(
- append file path to the text from the edit control...
- put the text back to edit control, by GuiControl, Control or Control+V :(
that would work too.
Capbat's idea would work too, and good idea too, had he known the right commands and used them, his approach works too, just without the user interference, like this:

Code: Select all

#Singleinstance Force

Gui,Add, Edit, x10 y10 w630 h250 vEditNewProcedure
Gui,Add, Button, x10 y270 vSaveProcedureBtn , Save
Gui,Add, Button, x55 y270 vGetFileBtn gGetfileBtn , Get File
Gui,Show ,w650 h300, Edit Procedure
Return

GetFileBtn:        
    Gui, Submit, NoHide
    FileSelectFile, SelectedDocumentationFile,1,,Select Documentation to be associated.,*.*
    OldClip:=ClipboardAll ; save the content of the clipboard for the user, since he's not expecting his clipboard to change by selecting a file
	Clipboard:=""  ; empty the clipboard to get the error if it doesn't get written to
	Clipboard:=SelectedDocumentationFile ; put file's path in the clipboard
	ClipWait, 3 ; 3 sec waiting for the clip to have somthing
	if ErrorLevel {
		MsgBox, can't copy file path, well you should have used a better way, there must be better way
		Clipboard:=OldClip ; let's hope we didn't ruin anything, there is no ClipWaiting since we don't want to inform the user that we screw his clipboard content
		return
	}
	GuiControl, Focus, EditNewProcedure ; set focus to the edit control
	Send, ^V ; send the edit control a Ctrl+V, to paste the file path, wherever the heck that caret is
	Clipboard:=OldClip ; clipboard back to just before we changed it (hoping nothing went wrong)
return
User avatar
boiler
Posts: 16964
Joined: 21 Dec 2014, 02:44

Re: SOLVED How To Paste Into an EditBox?

06 Feb 2015, 08:14

I'm not debating anything. Your script does exactly what he wants. Well done.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], TAC109 and 91 guests