Version: 8.0

LESSON

Vision Event Scripts Overview

Description

Learn about creating and configuring event scripts in Vision.

Video recorded using: Ignition 8.0

Transcript

(open in window)

[00:00] Scripting on vision components is typically done using component event handlers. Events are things that may happen to a component, such as a property changing or the component gaining focus, and an event handler allows you to write a script to handle how to respond to that event. To access the event handlers on a component, simply right click on the component, and go to scripting. Here in the scripting window, we can see a list of the event handlers for this component on the left. The event handlers that a component has varies depending on the type of component. The event handlers are split up into these different categories with each one responding to a different type of event. The event handlers are usually pretty self-explanatory as to when they fire, but if you select it, they also have an event description down here at the bottom of the window. You'll notice I've selected the mouseEntered event handler, which fires when the mouse enters the space over the source component. Once you have selected an event handler, you can then add a script that runs when that event fires. To make writing scripts a little bit easier, we have what's called script builders for some common actions that you can use to build the script for you. You can navigate between the different builders using these tabs up at the top, things like navigation actions, doing a SQL update, setting a property value, all available within these builders. The script editor tab over here on the right will instead let you write your own scripts. If I take a look at one of the builders here, say, set tag value, I can make it so that when the event handler, mouseEntered, fires, I want to set a particular tag to a particular value. One of the neat features of the script builders is that I can then navigate over to the script editor tab, and the script that the builder has created will be placed in there. You can then manipulate the script in any way you want, providing you with a great tool to get some code started or to learn more about scripting if you are new to it. For now, I'll place a simple print statement in here so we can easily see how this event handler works. I'm going to go ahead, hit OK, and then open up my console in the tools menu so that I can see the print commands from this particular component. Keep in mind that the scripts only run within the run time, so to have them execute here in the designer, I need to put my designer into preview mode for the script to actually fire. You can see that when the mouse enters the component's bounds, the script fires, and I print out the word test in the console down below. I mentioned earlier that different components have different event handlers, and there was one component in particular that I wanted to take a look at. I have here just a standard button, and I'm going to go ahead and right click, and go to scripting on this component. The button has a lot of the events that we saw earlier on the text field, but it also has an actionPerformed event. The actionPerformed event fires when the action of the component occurs. In this case, when the button is being pressed. Now, if you wanted to have a script fire when a user presses the button, you might initially place the script on an event like mouseClicked or mousePressed. While those would work in some instances, it is possible to press a button in ways other than using the mouse, for example, you can tab over to the button using the keyboard, and then press it using the space bar, or type in the mnemonic key for the button that has been set up. In those cases, the mouse events would not fire since the mouse was not involved in any way. The actionPerformed event covers all methods of the button being pressed, so it is advisable to use that when dealing with buttons. One last thing with these mouse events, I have a text field here that has some scripting already set up on the mouseClicked, mousePressed, and mouseReleased events that simply prints the name of the event that's firing it. If I were to test this out, and click on the text field, you'll notice I get all three events to fire, Mouse Pressed, Mouse Released, and Mouse Clicked, however, the mouseClicked event is special in that it requires a full click, meaning a press and release in the exact same spot, so if I click again, but this time, move my mouse in between pressing and releasing, you'll notice that the mouseClicked event doesn't fire, only the Mouse Pressed and Released got fired. Since slight mouse movement during a click happens often, we actually recommend using a mouse press or release script over a mouse click when the action performed is not available. It is important to keep this mind when deciding on an event to use as you want to ensure that the script that you have created fires at the appropriate time.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.

close

Share this video