Jump to content

Positioning the cursor at the end of a string.


Recommended Posts

If I create an input component the default behavior (at least when I test it) is that it selects all the text I've added to it. Once I've finished creating the form I want to position to a specific input component and place the cursor at the end of the text. How do I do this?

Link to comment
Share on other sites

  • Moderators

An Inputbox halts the script until either ok is clicked or it times out. So the only way I can think of to accomplish this would be to call a second script right before the inputbox. The second script would wait for the inputbox and send a right arrow or end key.

Link to comment
Share on other sites

I think there's a better way to do this--maybe GuiCtrlSendMsg--but here's off the top of my head:

#include <GuiConstants.au3>
$GUI = GuiCreate("Example")

$inputOne = GuiCtrlCreateInput("this is an input box", 10, 10, 300, 30)
$inputTwo = GuiCtrlCreateInput("another input box", 10, 60, 300, 30)
$inputThree = GuiCtrlCreateInput("here is a third input box", 10, 110, 300, 30)

GuiSetState()

ControlFocus($GUI,"",$inputTwo);Focus
ControlSend($GUI,"", $inputTwo, "^{End}");Send Ctrl+End

While 1
    $msg = GuiGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

I think there's a better way to do this--maybe GuiCtrlSendMsg--but here's off the top of my head:

#include <GuiConstants.au3>
$GUI = GuiCreate("Example")

$inputOne = GuiCtrlCreateInput("this is an input box", 10, 10, 300, 30)
$inputTwo = GuiCtrlCreateInput("another input box", 10, 60, 300, 30)
$inputThree = GuiCtrlCreateInput("here is a third input box", 10, 110, 300, 30)

GuiSetState()

ControlFocus($GUI,"",$inputTwo);Focus
ControlSend($GUI,"", $inputTwo, "^{End}");Send Ctrl+End

While 1
    $msg = GuiGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd
Slimy slug,

Thanks for the lead-in and after playing around the following works:

GUICtrlSetState($controlGUI, $GUI_FOCUS)

Send("^{End}")

as well as:

$status = ControlSend("Title", "", $controlGUI, "^{End}")

The former is better perhaps as it works on the current form where the latter is title based.

Edited by Peter Hamilton-Scott
Link to comment
Share on other sites

  • Moderators

Slimy slug,

Thanks for the lead-in and after playing around the following works:

GUICtrlSetState($controlGUI, $GUI_FOCUS)

Send("^{End}")

as well as:

$status = ControlSend("Title", "", $controlGUI, "^{End}")

The former is better perhaps as it works on the current form where the latter is title based.

$status = ControlSend("Title", "", $controlGUI, "^{End}")

Could Be:

$status = ControlSend($GUI, "", $controlGUI, "^{End}")

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

$status = ControlSend("Title", "", $controlGUI, "^{End}")

Could Be:

$status = ControlSend($GUI, "", $controlGUI, "^{End}")

Thanks. I checked the documentation and it does not list the second method call so I'll remember that one for future use.

Link to comment
Share on other sites

Setting the cursor to the end works fine but I'm wondering how to do that in a message loop? For example, in the message loop I'd like to determine when one of six input fields receives the focus and then set the cursor to the end of the line for the field thats got the focus.

Is it possible to do that?

Link to comment
Share on other sites

Setting the cursor to the end works fine but I'm wondering how to do that in a message loop? For example, in the message loop I'd like to determine when one of six input fields receives the focus and then set the cursor to the end of the line for the field thats got the focus.

Is it possible to do that?

I found a "solution" which works. Here's some sample code fragments:

Here's the input field I create. As it's the first field created it's name is "Edit1". I don't know if that is always the case but it holds true for the six fields I created and they are implictly named "Edit1" through "Edit6".

$instance = GUICtrlCreateInput("MSDE01", 116, 28, 200, 25)

In the main message loop I get the control that currently has the focus on my main form GUI. I pass that into a function.

while 1
    $msg = GuiGetMsg()
    $control = ControlGetFocus($mainForm)
    PositionToEndOfLine($control)
    ...
    ...
wend

Finally, the function uses the main form and field GUIs to set the cursor to the end.

func PositionToEndOfLine($controlName)
    select
        case $controlName = "Edit1"
            ControlSend($mainForm, "", $instance, "^{End}")
        ...
        ...
    endselect
endfunc

Maybe there is a more efficient way but this does work.

Edit: One thing I did notice that the code works but something spooks keyboard entry. For example, the use of SHIFT key to enter uppercase text acts really weird. Sometimes it will let me enter the first character un uppercase and more often it seems "insensitive" and even with CAPS LOCK enabled, the text field does not enter uppercase text. I disabled the code and they text is then entered normally so I know it's not my keyboard or pudgy fingers that are at fault. I'm kind of suspecting a race condition as I'm calling my function after the GuiGetMsg?

Edited by Peter Hamilton-Scott
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...