# Query on Pasword field text Enter key to execute

**URL:** <https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [May 29, 2020, 2:00pm UTC](https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159 "2020-05-29T14:00:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![trishadx](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@trishadx](https://forum.inductiveautomation.com/u/trishadx)\
**Post date:** [May 29, 2020, 2:00pm UTC](https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159/1 "2020-05-29T14:00:45Z")

</div>

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).

```auto
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:

```auto
  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

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 29, 2020, 2:09pm UTC](https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159/2 "2020-05-29T14:09:37Z")

</div>

You’re indents are out of order. This looks like it may be a property change script, so it looks like this part

```auto
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

```auto
success= system.security.switchUser(uname,pwd,event)

```

uname is then undefined.

I think you may just want to indent like this

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![trishadx](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@trishadx](https://forum.inductiveautomation.com/u/trishadx)\
**Post date:** [May 29, 2020, 2:15pm UTC](https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159/3 "2020-05-29T14:15:41Z")

</div>

Oh is that so! thank you very much for that, was not unaware. It works perfectly fine now thank you!!

---

<div class="post-metadata">

**Author:** ![trishadx](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@trishadx](https://forum.inductiveautomation.com/u/trishadx)\
**Post date:** [May 29, 2020, 2:17pm UTC](https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159/4 "2020-05-29T14:17:25Z")

</div>

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:

```auto
  File "<KeyBinding:KeystrokeAction:pressed ENTER>", line 1, in <module>
ValueError: Window MFD does not exist.

```

would i need to change the event synthax?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 29, 2020, 2:18pm UTC](https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159/5 "2020-05-29T14:18:44Z")

</div>

Post your whole key pressed script.

---

<div class="post-metadata">

**Author:** ![trishadx](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@trishadx](https://forum.inductiveautomation.com/u/trishadx)\
**Post date:** [May 29, 2020, 2:20pm UTC](https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159/6 "2020-05-29T14:20:09Z")

</div>

Key pressed script as follows:

```auto
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

> [@trishadx](#):
>
> File "\<KeyBinding:KeystrokeAction:pressed ENTER\>", line 1, in ValueError: Window MFD does not exist.

didnt think it was gonna make a difference hmm

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 29, 2020, 2:23pm UTC](https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159/7 "2020-05-29T14:23:51Z")

</div>

You put it in keyPressed right? Remember you only have access event properties the extension function tells you do -

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/2/2c0669d07e5610bb2b4710f8f4afe500a79a1606.png)

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.

---

<div class="post-metadata">

**Author:** ![trishadx](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@trishadx](https://forum.inductiveautomation.com/u/trishadx)\
**Post date:** [May 29, 2020, 3:20pm UTC](https://forum.inductiveautomation.com/t/query-on-pasword-field-text-enter-key-to-execute/35159/8 "2020-05-29T15:20:15Z")

</div>

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.
