Trigger a button Click with text entry

Got thrown on an ignition project that has mostly been built and I do the PLC stuff and its been fine. I figured out most of the scripting stuff I’ve needed to in order to interact properly with the HMI however last minute they want me to have a “validate” button be triggered after a text box is filled. I’m new to ignition and really am not sure how to accomplish this. I’m using a barcode scanner that is acting as a USB keyboard so I was thinking of ‘property change’ or ‘key released’ as my script trigger and then use component.onClick() function call. Not sure if I’m goin in the right direction. The scanner blasts values into the text box and I would like it to trigger an adjacent button when it is done ‘typing’. This button also has scripting that was provided by another integrator. Some pointing in the correct direction would be appreciated.

What do your scanned strings look like?

There is a scanner input component you can use. That looks for certain patterns.

Otherwise. The direction you are going is not wrong. I tend to do that kind of stuff with events that call component methods.

If the bar code scanner can tab out of the text field. After a scan. You might consider using the lost focus event. Instead.

I usually make sure the string is matches some kind of format. First.

Post more info and we will help you.

They are just Char, Int, Int, Int, Int. This was a last minute thing they threw at me So they look like G2342 and normally user would hit the ‘validate button’ but now that I added a scanner they want the validate button to trigger on its own. I already have it checking format on the PLC side and throwing pop ups but not sure how to trigger an event in another object based off of key presses stopping or that field having a value present. I know some traditional programming but this is my first ignition project. Python is similar to java sort of and I know some java, I was thinking I need to assign the button component to a variable and then trigger its click fucntion but not sure what event from the text field would be a valid ‘green light’ to do that. I tried some stuff yesterday and was crashing it.

So i would set the scanner up to tab out of field after a read. Then I would use a onCompositionEnd event to call your code.

Ok, reading…

I had on my text box under “property change”
button = event.source.parent.getComponent(“button 2”)
button.doClick()

It sort of almost works it keeps calling the button click. Looking up your suggestion

That sounds like your propertyChange script isn't checking which property changed. You must have an if statement in propertyChange events that checks event.propertyName....

@pturmel beat me to it, but you need to narrow down the property change event you want to catch, IE

if event.propertyName == 'text':
  execute your script
1 Like

Is there something I should know about binding components to variables, forgive my noobness. It was calling the click before I put the if statement but now getting errors. My code:

button = event.source.parent.getComponent(“button 2”)
if event.propertyName == ‘text’:
button.onClick()

er doClick

Try this, Jython is a structured language so white space matters. Where many languages have brackets to determine scope, Python/Jython uses white space for this. Use two spaces for indention or a tab

if event.propertyName == 'text':
  button = event.source.parent.getComponent('button 2')
  button.doClick()
2 Likes

Attribute error: ‘NoneType’ object has no attribute ‘doClick’ leads me to believe I’m not binding the component to the variable correctly

Or calling an existing function on it, although other examples that does seem like a possible way to do it, usually can do function calls on variables in other languages confused

Yes, getComponent is returning None--no component. Did you replace the curly quotes in the forum post (“button 2”) with straight quotes ("button 2")? Otherwise, component name or path to it are wrong.

2 Likes

Good catch @witman. I altered my previous post to use the correct straight quotes.

1 Like

Facepalm, that worked. Havn’t had to do syntax heavy stuff like this for a few years. Also the white space I learned on java where I could indent or not. Thanks witman and hayes!

1 Like

For the record it actually took it with single quotes.
if event.propertyName == ‘text’:
button = event.source.parent.getComponent(‘Button 2’)
button.doClick()

I also saw on another post I could have done this

event.source.parent.getComponent(‘Button 2’).doClick()

Without doing a var assign, did not try it though.

You really need to surround your code on this forum within three ` characters, or use the preformatted function. Python, as people have mentioned, is a space-driven language. Without the preserved space formatting, it’s impossible to read

My bad I’m new to this forum and python, thanks for the tip.

Is there an equivalent to doClick() in perspective? This code won’t compile:

if event.key == "Enter":
		button = self.getSibling('LoadLotsButton')
		button.doClick()

Here is the error I’m seeing:

Error running action 'dom.onKeyPress' on Home@D/root/InputMaterialID: 
Traceback (most recent call last): 
File " ", line 6, in runAction 
AttributeError: 'com.inductiveautomation.perspective.gateway.script' object has no attribute 'doClick'

No. doClick in Vision is solving the problem in the wrong way in the first place. Put any significant business logic into a project script, and call it with whatever arguments are necessary, from whatever call sites.
If you don’t want to do a project script, then at least put it into a custom method on the component. Then the button’s actionPerformed can call its custom method, and wherever else you need to call the code can as well - just send a message, and have a message handler on the button that also calls the custom method.

4 Likes