Hi, I’m fairly new to ignition and its expression scripting and need some advice on this… pls and ty
I’ve created a container to have a field text and a password field text for the user to input values and be able to switch users. Currently I have this working and once the user has typed in the correct details, a button performs the action to switc users in the client.
However, instead of having only the button, I’d like the user to be able to press the ENTER KEY on the keyboard to execute the user switch. I’ve tried to put the following code on the password field text’s event handlers of ketTyped (i’ve also tried it on propertyChange, mutually exclusive ofcourse, not at the same time).
if event.keyCode == event.VK_ENTER:
#This script would go on a button in a popup window used to switch users without logging out of the client.
# Pull the username and password from the input components
uname = event.source.parent.getComponent("Username").text
pwd = event.source.parent.getComponent("Password").text
# Call switchUser. The event object is passed to this
# function so that if the username and password work,
# this window will be closed before the switch occurs.
success= system.security.switchUser(uname,pwd,event)
# If the login didn't work, give input focus back to the
# username component, so that the user can try again
if not success:
event.source.parent.getComponent("Username").requestFocusInWindow()
However, running in vision client, im now unable to type in this field. the first key i press (Regardless of
which key) i get the error:
File "<event:keyTyped>", line 11, in <module>
NameError: name 'uname' is not defined
Eventhough it executes perfectly find as is as a button (without the if event.keycode statement)
Thank you in advance
You’re indents are out of order. This looks like it may be a property change script, so it looks like this part
if event.keyCode == event.VK_ENTER:
#This script would go on a button in a popup window used to switch users without logging out of the client.
# Pull the username and password from the input components
uname = event.source.parent.getComponent("Username").text
will get skipped if something other than a enter triggered it,
so then here
success= system.security.switchUser(uname,pwd,event)
uname is then undefined.
I think you may just want to indent like this
if event.keyCode == event.VK_ENTER:
#This script would go on a button in a popup window used to switch users without logging out of the client.
# Pull the username and password from the input components
uname = event.source.parent.getComponent("Username").text
pwd = event.source.parent.getComponent("Password").text
# Call switchUser. The event object is passed to this
# function so that if the username and password work,
# this window will be closed before the switch occurs.
success= system.security.switchUser(uname,pwd,event)
# If the login didn't work, give input focus back to the
# username component, so that the user can try again
if not success:
event.source.parent.getComponent("Username").requestFocusInWindow()
Python uses white space to determine the order of the logic. Doing something like this
if someCondition:
x =5
print x
will always break if some condition isn’t met to define x, because the print is not indented and will therefore always run.
Oh is that so! thank you very much for that, was not unaware. It works perfectly fine now thank you!!
1 Like
However, could I ask…
This code works find on the event “keyPressed”
but when I pasted it into “keyTyped” i get the error from vision client:
File "<KeyBinding:KeystrokeAction:pressed ENTER>", line 1, in <module>
ValueError: Window MFD does not exist.
would i need to change the event synthax?
Post your whole key pressed script.
Key pressed script as follows:
if event.keyCode == event.VK_ENTER:
#This script would go on a button in a popup window used to switch users without logging out of the client.
# Pull the username and password from the input components
uname = event.source.parent.getComponent("Username").text
pwd = event.source.parent.getComponent("Password").text
# Call switchUser. The event object is passed to this
# function so that if the username and password work,
# this window will be closed before the switch occurs.
success= system.security.switchUser(uname,pwd,event)
# If the login didn't work, give input focus back to the
# username component, so that the user can try again
if not success:
event.source.parent.getComponent("Username").requestFocusInWindow()
I copy and past the same to keyReleased / keyTyped to differ the condition...but it comes up with the error
didnt think it was gonna make a difference hmm
You put it in keyPressed right? Remember you only have access event properties the extension function tells you do -
So if you copy and pasted that code to say a mouse extension function, it would definitely throw you an error.
If it is inside keyPressed for sure, I don’t know. Someone else will have to help you.
Oh yes, thank you for the reminded. learning so many things at once you tend to overlook simple parts.
but yeah i thought itd work the same for keytype/release as it would on pressed. But anyway, thank you for the advice.
1 Like