# Gain focus on a numeric text field when barcode enters a value

**URL:** <https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403>\
**Category:** Ignition\
**Tags:** scripting, database, ignition80, vision, perspective\
**Created:** [July 30, 2021, 3:38pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403 "2021-07-30T15:38:12Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 30, 2021, 3:38pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/1 "2021-07-30T15:38:12Z")

</div>

Hello everyone,

For a project, I have a barcode and multiple numeric entry text fields on my screen. Currently, the barcode is entering the value into whatever numeric entry box I am in by clicking the mouse on it. However, I want to be able to choose the tag the barcode should enter a value in. The operator might not even have access to a screen or a mouse.  
I know that there is a way to do this in Ignition? (Both in vision and perspective) but not sure about it.  
[https://docs.inductiveautomation.com/display/DOC81/Focus+Manipulation](https://docs.inductiveautomation.com/display/DOC81/Focus+Manipulation)

I am using a USB barcode scanner.

Thank you!

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [July 30, 2021, 6:41pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/2 "2021-07-30T18:41:48Z")

</div>

In vision you can use:

```auto
event.source.requestFocusInWindow()

```

In perspective

```auto
self.focus()

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 30, 2021, 6:43pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/3 "2021-07-30T18:43:57Z")

</div>

Thank you very much for your response!  
Where should I use this script?  
Script editor of the numeric text field? and in what event? focus?

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [July 30, 2021, 6:52pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/4 "2021-07-30T18:52:06Z")

</div>

In Vision you can add a custom property (name: scan) to the text field and bind it to the tag, then add a script on property change:

```auto
if event.propertyName == "scan":
     event.source.requestFocusInWindow()

```

In Perspective is similar, right click on custom property, also binding it to the tag, and click on _Add Change Script_  
then add the script:

```auto
self.focus()

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 30, 2021, 7:24pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/5 "2021-07-30T19:24:18Z")

</div>

Thank you!  
When the barcode wants to enter the value, non of my text field gets the value, unless I click on one of them and then the barcode value is entered into that field.

So this is what I did:  
I created a numeric text field and bound its “Long” value to a memory tag called “barcode scan”. I also created a property for that text field as you mentioned and put its data type to Long:

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/4/e4774c02edf4de9fe1b924a36b98b86d058e93b2.png)

I bound that property to the “barcode scan” tag I mentioned above. I wrote the script for the property change of that numeric text field using the script editor.

```auto
if event.propertyName == "scan":
     event.source.requestFocusInWindow()

```

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [July 30, 2021, 7:44pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/6 "2021-07-30T19:44:18Z")

</div>

Oh! If you are have a binding already to the tag on value property, you can delete the custom property and use “value” property instead of “scan”.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 30, 2021, 7:47pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/7 "2021-07-30T19:47:59Z")

</div>

You mean I should write this instead?

```auto
if event.propertyName == "LongValue":
     event.source.requestFocusInWindow()

```

cause it is not working either.

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [July 30, 2021, 7:50pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/8 "2021-07-30T19:50:43Z")

</div>

```auto
if event.propertyName == "longValue":
     event.source.requestFocusInWindow()

```

with lower case, but yes

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 30, 2021, 7:53pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/9 "2021-07-30T19:53:17Z")

</div>

Unfortunately, it is not working. I do not see the cursor on that numeric text field when I launch the project, and all of the text fields are the same and I cannot enter the barcode value unless I click on one of them.

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [July 30, 2021, 7:59pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/10 "2021-07-30T19:59:28Z")

</div>

It works! I just tested it.  
Let me try to understand what you want. When ever the value of the barcode changes, you want to cursor be on numeric text field ready to type?  
If that is what you need I already did it and it works.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 30, 2021, 8:08pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/11 "2021-07-30T20:08:50Z")

</div>

Yes and No.

So if I want the cursor to be on a numeric text field whenever the value of the barcode changes, I should use the same approach you told me, so basically:

1. create a numeric text field
2. bound its long value to a memory tag called “barcode scan”
3. do scripting for that numeric text field component as below:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/b/9bc443c0b8b747cc53bfa280e083b987e1c4fde6.png)

And it should work! But it is not working ☹ When I launch the project that numeric text field does not have the cursor.

The other thing that I want is that whenever the window opens I want a specific numeric field to listen to the barcode. So for this application, we have the barcode far from the screen, and the computer and operator cannot click on the text field, basically, the screen should constantly wait for the barcode to be entered. When the barcode is entered it should automatically go to that numeric field. I was hoping to use this:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/9/d96098bb3ed239cba6de43c356535f7f42c9e37d.png)  
`(system.gui.getParentWindow(event).getComponentForPath('Root Container.BarcodeScanner').longValue).requestFocusInWindow()`  
based on this link:  
[https://docs.inductiveautomation.com/display/DOC81/Focus+Manipulation](https://docs.inductiveautomation.com/display/DOC81/Focus+Manipulation)

But it is not working as my script is wrong

---

<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:** [July 30, 2021, 8:20pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/12 "2021-07-30T20:20:24Z")

</div>

When I have trouble with property change events, sometimes its helpful to put this script at the very top in the propertyChange script window to verify that the property you expect is actually firing.

```auto
print event.propertyName

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 30, 2021, 8:24pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/13 "2021-07-30T20:24:37Z")

</div>

Thank you!

So I do see the barcode is being printed on top of that statement but I do not see the cursor on the numeric field itself.

```auto
043100069096
667888327577
667888327577
print event.propertyName
if event.propertyName == "longValue":
     event.source.requestFocusInWindow()

```

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [July 30, 2021, 9:10pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/14 "2021-07-30T21:10:12Z")

</div>

this is syntax mistake, you only call the focus as a method. delete parenthesis at beginning a after _longValue_ and delete longValue

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [July 30, 2021, 10:10pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/15 "2021-07-30T22:10:54Z")

</div>

This should not be that complicate.  
Anyway, toggle to True the Defer Updates property, and to create a virtual mouseRelease event you can do it with:

```auto
if event.propertyName == "longValue":
	event.source.requestFocusInWindow()
	def click():
		from java.awt.event import MouseEvent
		from java.lang import System
		target = event.source
		id = MouseEvent.MOUSE_RELEASED
		when = System.currentTimeMillis()
		x = target.getX()
		y = target.getY()
		click = MouseEvent(target, id, when, 0, x, y, 1, False)
		event.source.dispatchEvent(click)
	system.util.invokeLater(click)

```

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [August 1, 2021, 3:16pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/16 "2021-08-01T15:16:09Z")

</div>

You can also try this:  
[https://www.sepasoft.com/products/barcode-scanner/](https://www.sepasoft.com/products/barcode-scanner/)

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 3, 2021, 12:03pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/17 "2021-08-03T12:03:42Z")

</div>

Just as a reference for others:

I used **InternalFrameActivated** event handlers on the scripting feature of my main window and I wrote the script below in that option:

`system.gui.getParentWindow(event).getComponentForPath('Root Container.BarcodeScanner').requestFocusInWindow()`

And it worked! Now whenever the window opens up, the numeric entry box has focus and the user can directly enter number into it.

---

<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:** [August 3, 2021, 12:04pm UTC](https://forum.inductiveautomation.com/t/gain-focus-on-a-numeric-text-field-when-barcode-enters-a-value/49403/18 "2021-08-03T12:04:32Z")

</div>

That’s what I use 🙂
