# Run Button On Click Script When Key is Pressed

**URL:** <https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [May 18, 2023, 1:34pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897 "2023-05-18T13:34:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![msitson](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@msitson](https://forum.inductiveautomation.com/u/msitson)\
**Post date:** [May 18, 2023, 1:34pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/1 "2023-05-18T13:34:19Z")

</div>

Hello,

Is it possible to run a button's on click event when the 'Enter' key is pressed? The on click event is a navigation script with one url parameter. I noticed that within Session Events there is a key events section, but I am not sure as to how to specify the specific view to act on and how I would access the view params to pass the value. What would be the best way of identifying when the 'Enter' key is pressed and running the navigation script?  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/0/607df8cf5b3461d4fcf767c401efd0422e0fbf11.png)

---

<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 18, 2023, 1:39pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/2 "2023-05-18T13:39:36Z")

</div>

Put the logic of the button into a project script, and then you can call it on any event (button press, key press, etc) with ease. It's fine to need parameters for your function to work correctly if that is what is required.

If you need help refactoring, post your button's current script.

---

<div class="post-metadata">

**Author:** ![msitson](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@msitson](https://forum.inductiveautomation.com/u/msitson)\
**Post date:** [May 18, 2023, 1:47pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/3 "2023-05-18T13:47:07Z")

</div>

Sorry maybe I should've been more clear in my post, but I feel that my main problem is how I should be identifying if the 'Enter' button is pressed as the script is simple a navigate.

---

<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 18, 2023, 1:58pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/4 "2023-05-18T13:58:09Z")

</div>

The keyCode should be 10 I believe. You can do a `system.perpective.print(theKeyCodeVariableHere)` to confirm that.

---

<div class="post-metadata">

**Author:** ![msitson](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@msitson](https://forum.inductiveautomation.com/u/msitson)\
**Post date:** [May 18, 2023, 2:11pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/5 "2023-05-18T14:11:58Z")

</div>

I tried checking if the keyCode was 10 and checking for "VK\_ENTER", but both did not work. I tried printing out event.keyCode but it returned 0 no matter what button I pressed. It is also worth noting that keyCode is deprecated.

---

<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 18, 2023, 2:13pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/6 "2023-05-18T14:13:45Z")

</div>

What do you see for the `key (str)` value when you press enter?

---

<div class="post-metadata">

**Author:** ![msitson](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@msitson](https://forum.inductiveautomation.com/u/msitson)\
**Post date:** [May 18, 2023, 2:14pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/7 "2023-05-18T14:14:59Z")

</div>

It's blank but I'm assuming it is entering a '\n'.

---

<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:** [May 18, 2023, 2:22pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/8 "2023-05-18T14:22:59Z")

</div>

Get rid of the button, and move your navigation script to the `onActionPerformed` event on the numeric entry field. When you hit enter, it will trigger the action performed event.

---

<div class="post-metadata">

**Author:** ![msitson](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@msitson](https://forum.inductiveautomation.com/u/msitson)\
**Post date:** [May 18, 2023, 2:35pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/9 "2023-05-18T14:35:06Z")

</div>

The input is a text field. I also can't change it to a numeric entry field as some employee numbers contain alphanumeric characters.

---

<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:** [May 18, 2023, 2:40pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/10 "2023-05-18T14:40:34Z")

</div>

On the text field, use `onKeyPress` and add this script with your nav info filled in.

```auto
if event.charCode == 13:
	empNumb = self.props.text
	system.perspective.navigate(page, url, view, params, newTab)

```

---

<div class="post-metadata">

**Author:** ![msitson](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@msitson](https://forum.inductiveautomation.com/u/msitson)\
**Post date:** [May 18, 2023, 2:45pm UTC](https://forum.inductiveautomation.com/t/run-button-on-click-script-when-key-is-pressed/73897/11 "2023-05-18T14:45:23Z")

</div>

Thanks I got it working!
