# requestFocusInWindow() swallows first key stroke in text field

**URL:** <https://forum.inductiveautomation.com/t/requestfocusinwindow-swallows-first-key-stroke-in-text-field/32812>\
**Category:** Ignition\
**Created:** [March 4, 2020, 6:52pm UTC](https://forum.inductiveautomation.com/t/requestfocusinwindow-swallows-first-key-stroke-in-text-field/32812 "2020-03-04T18:52:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbanzet](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cbanzet/32/6207_2.png) [@cbanzet](https://forum.inductiveautomation.com/u/cbanzet)\
**Post date:** [March 4, 2020, 6:52pm UTC](https://forum.inductiveautomation.com/t/requestfocusinwindow-swallows-first-key-stroke-in-text-field/32812/1 "2020-03-04T18:52:47Z")

</div>

I have been trying to move the focus to the next logical text field on my window when the user presses the ALT key.

I put the following code in the **keyReleased** event:

```auto
if event.keyCode == 18:
	event.source.parent.getComponent('tfRptDt').requestFocusInWindow()

```

This will transfer the focus to the desired text field. However, the first character I type in the text field does not appear, but the second one does.

I have tried using “Invoke Later” under **Advanced Settings** on the event handler. I have even tried a double “Invoke Later” by using “Invoke Later” under **Advanced Settings** on the event handler **AND** using _system.util.invokeLater()_ by using the following code:

```auto
if event.keyCode == 18:
	def focus():
		event.source.parent.getComponent('tfRptDt').requestFocusInWindow()
	system.util.invokeLater(focus)
	

```

I get the same behavior.

I also tried:

```auto
if event.keyCode == 18:
	nc = event.source.parent.getComponent('tfRptDt')
	nc.requestFocusInWindow()
	s=nc.text
	nc.text = ' '
	nc.text = s
	

```

Just to see if changing the text in the text field would make a difference. The first character I type in the text field still disappears.

Any ideas?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 4, 2020, 7:04pm UTC](https://forum.inductiveautomation.com/t/requestfocusinwindow-swallows-first-key-stroke-in-text-field/32812/2 "2020-03-04T19:04:51Z")

</div>

Have you tried using the tab key without any `requestFocusInWnindow()` ? The tab key will swap fields left to right then top to bottom, but I believe you can customize the tab order by grouping components and get a different tab order that might work for you.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 4, 2020, 7:15pm UTC](https://forum.inductiveautomation.com/t/requestfocusinwindow-swallows-first-key-stroke-in-text-field/32812/3 "2020-03-04T19:15:38Z")

</div>

Try:

```auto
if event.keyCode == 18:
	target = event.source.parent.getComponent('tfRptDt')
	target.requestFocusInWindow()
	for listener in target.keyListeners:
		listener.keyReleased(event)

```

---

<div class="post-metadata">

**Author:** ![cbanzet](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cbanzet/32/6207_2.png) [@cbanzet](https://forum.inductiveautomation.com/u/cbanzet)\
**Post date:** [March 5, 2020, 12:45am UTC](https://forum.inductiveautomation.com/t/requestfocusinwindow-swallows-first-key-stroke-in-text-field/32812/4 "2020-03-05T00:45:32Z")

</div>

Well, trying to use the default tab order wasn’t going to work, that’s why I ended up going this route. I didn’t know grouping would affect the default tab order, but it still wouldn’t work for me, because at least one move I need to make is up and to the right. Also, if I start grouping components now, a **ton** of existing scripting is going to need to change…way more than I want to deal with.

---

<div class="post-metadata">

**Author:** ![cbanzet](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cbanzet/32/6207_2.png) [@cbanzet](https://forum.inductiveautomation.com/u/cbanzet)\
**Post date:** [March 5, 2020, 12:46am UTC](https://forum.inductiveautomation.com/t/requestfocusinwindow-swallows-first-key-stroke-in-text-field/32812/5 "2020-03-05T00:46:17Z")

</div>

Paul - I copied your code in exactly and I am still getting the same behavior.

---

<div class="post-metadata">

**Author:** ![cbanzet](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cbanzet/32/6207_2.png) [@cbanzet](https://forum.inductiveautomation.com/u/cbanzet)\
**Post date:** [March 5, 2020, 12:48am UTC](https://forum.inductiveautomation.com/t/requestfocusinwindow-swallows-first-key-stroke-in-text-field/32812/6 "2020-03-05T00:48:38Z")

</div>

The issue stems from my use of the ALT key. I switched the key to CTRL+A and I don’t have the issue. My user seems to be satisfied with that, so I am going to leave it. I’m just going to make a note to avoid “special” keys.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 5, 2020, 1:08am UTC](https://forum.inductiveautomation.com/t/requestfocusinwindow-swallows-first-key-stroke-in-text-field/32812/7 "2020-03-05T01:08:42Z")

</div>

It was a guess - I figured the first component was ‘consuming’ the key event, so re-emitting it _might_ work to get the second component to use it.
