# Move text field cursor to end of text

**URL:** <https://forum.inductiveautomation.com/t/move-text-field-cursor-to-end-of-text/23011>\
**Category:** Ignition\
**Created:** [March 2, 2019, 5:12am UTC](https://forum.inductiveautomation.com/t/move-text-field-cursor-to-end-of-text/23011 "2019-03-02T05:12:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![juang](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@juang](https://forum.inductiveautomation.com/u/juang)\
**Post date:** [March 2, 2019, 5:12am UTC](https://forum.inductiveautomation.com/t/move-text-field-cursor-to-end-of-text/23011/1 "2019-03-02T05:12:06Z")

</div>

I Have a Text Field component , that has a characters remaining label, and also limits the amount  
of characters allowed. i have it working but the problem is that when the limit is reached(20 characters) the Cursor of the text box goes to the first character in the field

This is the Script that i found that works for the limit, but the cursor goes to the first character, I want the text cursor to stay in the last character.

```
if event.propertyName == 'text': 
    if len(event.newValue) > 20:
        def doLate(evt=event): 
	       evt.source.text = evt.oldValue
        system.util.invokeLater(doLate)

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/c/c21467e324952039d3ce8a0454660a7941bb808a.png)

in this example i would type two more characterts it would at 1 more character and then the cursor moves to before the M. i need to stay at then end.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 2, 2019, 12:47pm UTC](https://forum.inductiveautomation.com/t/move-text-field-cursor-to-end-of-text/23011/2 "2019-03-02T12:47:46Z")

</div>

Turn off ‘Defer Update’ and use java.awt.Robot:

```python
if event.propertyName == 'text': 
  if len(event.newValue) > 20:
    from java.awt import Robot
    from java.awt.event import KeyEvent
    robot = Robot()
    robot.keyPress(KeyEvent.VK_BACK_SPACE)
    robot.keyRelease(KeyEvent.VK_BACK_SPACE)

```

If you need the Defer Update functionality, you’ll need to add scripts to the keyPressed and focusLost events.

Here is a demo to illustrate. Text Box 1 is limited to 5 characters, and pressing Enter or shifting focus writes the contents to Text Box 2.

[Limit Textbox\_2019-03-02\_0735.proj](https://us1.discourse-cdn.com/inductiveautomation/original/2X/9/9d3c40692cde1ed372db487e698612fca17e813f.proj) (6.1 KB)

![2019-03-02_7-45-06](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/179ecb4e7303a6e1be19ddf4fbb8e27a296a75a7.jpeg)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 2, 2019, 4:26pm UTC](https://forum.inductiveautomation.com/t/move-text-field-cursor-to-end-of-text/23011/3 "2019-03-02T16:26:00Z")

</div>

I like your original event, but I’d add calls to set the selection to the 20th character, and I’d keep the last character as the new 20th. Like so:

```auto
if event.propertyName == 'text': 
	if len(event.newValue) > 20:
		def doLate(evt=event): 
			evt.source.text = event.newValue[:19]+event.newValue[-1]
			evt.source.selectionEnd = 20
			evt.source.selectionStart = 19
	system.util.invokeLater(doLate)

```

See [JTextComponent.setSelectionStart()](https://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#setSelectionStart-int-) for the details.

---

<div class="post-metadata">

**Author:** ![juang](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@juang](https://forum.inductiveautomation.com/u/juang)\
**Post date:** [March 3, 2019, 8:33pm UTC](https://forum.inductiveautomation.com/t/move-text-field-cursor-to-end-of-text/23011/4 "2019-03-03T20:33:57Z")

</div>

Fyi. i ended up using this.

```
if event.propertyName == 'text': 
	if len(event.newValue) > 20:
    	def doLate(evt=event): 
	    	evt.source.text = evt.oldValue
	        evt.source.selectionEnd = 20
	    	evt.source.selectionStart = 20
	    system.util.invokeLater(doLate)

```

the reason why i used the oldValue, is that this is for a password field, so let’s say if the user is typing and does not look that field, if he types 22 characters, with your solution the character twenty will be replaced and if they approved that password, then they will not know that it was actually changed automatically with the script, so next login they will have incorrect password. i thougth it would be better to keep the old value, that way if the password it’s type next login if they type the same first 20 characters, password will be correct.

---

<div class="post-metadata">

**Author:** ![MaxDeVos](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/maxdevos/32/63192_2.png) [@MaxDeVos](https://forum.inductiveautomation.com/u/MaxDeVos)\
**Post date:** [June 3, 2024, 2:56am UTC](https://forum.inductiveautomation.com/t/move-text-field-cursor-to-end-of-text/23011/5 "2024-06-03T02:56:23Z")

</div>

I apologize for necroposting, but I think it's worth doing in this case. I would advise being _ **extremely cautious** _ about using an approach like this. If the user were to tab/click/navigate away from the text box during the time window in which those KeyEvents were being processed (which is longer than you might think), the JVM will simulate those key presses wherever the user's focus is at - leading to unexpected, potentially dangerous behavior.

Generally speaking, it is virtually never a good idea to use `Java.Robot` outside of UI unit testing. Using it to bandaid over a text selection issue in an HMI application? You're just asking for trouble.
