Jump to content

Input-control: How To Insert Text At Cursor Position?


 Share

Recommended Posts

I'd like to insert text into an Input control at the current position of the text cursor. The text should be inserted by pressing a button. I tried this, but I can't get it work:

#include <GUIConstants.au3>
AutoItSetOption("WinTitleMatchMode", 4)
AutoItSetOption("GUIOnEventMode", 1)

$frm = GUICreate("Test ControlSend", 200, 60)
$txt = GUICtrlCreateInput("Given text in a input control", 10, 10, 180, 20)
$btnA = GUICtrlCreateButton("A", 10, 35, 20, 20)
$btnB = GUICtrlCreateButton("B", 35, 35, 20, 20)
GUICtrlSetState($btnA, $GUI_FOCUS)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUICtrlSetOnEvent($btnA, "_UpdateTxtA")
GUICtrlSetOnEvent($btnB, "_UpdateTxtB")
GUISetState(@SW_SHOW)

While 1
WEnd

Func _Exit()
    GUIDelete($frm)
    Exit(0)
EndFunc

Func _UpdateTxtA()
    $ins = "[" & GUICtrlRead(@GUI_CtrlId) & "]"
    ControlSend($frm, "", $txt, $ins, 0)
;~  ControlSend($frm, "", $txt, $ins, 1)
;~  ControlSend($frm, "", $txt, "+{END}")
    Return
EndFunc

Func _UpdateTxtB()
    GUICtrlSetState($txt, $GUI_FOCUS)
    Send("[")
    Send(GUICtrlRead(@GUI_CtrlId))
    Send("]")
    Return
EndFunc

For the Send function (Func _UpateTxtB) I need to set focus to the $txt-control first. This marks all text content, which is lost after first Send operation.

The ControlSend function (Func _UpdateTxtA) inserts the sent text at the right position, but sends wrong text. In the example script it should insert "[A]", but it inserts "8a9" instead. On a german keyboard "[" is "AltGr+8", "]" is "AltGr+9" and "A" is "Shift+a". I assume, that ControlSend sends raw keyboard strokes without any qualifiers?

I tried the functions from the Include file GuiEdit.au3 too, but unfortunately without success (I throws many errors, and even if I ignore them, I don't get a result).

I am using AutoIT beta-version 3.1.1.111 (V3.1.1.0 have bugs with tristate checkboxes, so I need to use this beta... in the history list for V3.1.1.112 and newer I didn't see, that problems with ControlSend has been solved... or do I miss anything?)

Any ideas, how I can solve the problem?

Many thanks in advance!

Link to comment
Share on other sites

not sure what errors you were getting but this works for me

#include <GUIConstants.au3>
#include <GuiEdit.au3>

AutoItSetOption("WinTitleMatchMode", 4)
AutoItSetOption("GUIOnEventMode", 1)

$frm = GUICreate("Test ControlSend", 200, 60)
$txt = GUICtrlCreateInput("Given text in a input control", 10, 10, 180, 20)
$btnA = GUICtrlCreateButton("A", 10, 35, 20, 20)
$btnB = GUICtrlCreateButton("B", 35, 35, 20, 20)
GUICtrlSetState($btnA, $GUI_FOCUS)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUICtrlSetOnEvent($btnA, "_UpdateTxt")
GUICtrlSetOnEvent($btnB, "_UpdateTxt")
GUISetState(@SW_SHOW)

While 1
WEnd

Func _Exit()
    GUIDelete($frm)
    Exit(0)
EndFunc

Func _UpdateTxt()
    $ins = "[" & GUICtrlRead(@GUI_CtrlId) & "]"
     _GUICtrlEditReplaceSel($txt,True,$ins)
    Return
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Hello gafrost,

thanks a lot for your reply!

I had reinstalled AutoIT+AutoIT.beta+SciTe4AutoIT several times, and I think, that I follwed the given instructions for beta-installations in each try. But for all that I never get a proper installation. Now I have installed AutoIT.beta not in a seperate beta subdirectory, but above the stable 3.1.1 version.

And now it works, at least concerning AutoIT :)

When I start a script from within SciTe I still get a bunch of messages like "ERROR: {func()}: undefined function" (with {func()} = HWnd(), IsHWnd, DllStructCreate() and so on). This problem concerns only SciTe (my installation / configuration, respectively) : When I ignore these errors, the script runs anyway. So I'll learn to live with these error messages :mellow:

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...