# Tying button to key stroke

**URL:** <https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963>\
**Category:** Ignition\
**Created:** [August 9, 2012, 12:03am UTC](https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963 "2012-08-09T00:03:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Micky.Franks](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Micky.Franks](https://forum.inductiveautomation.com/u/Micky.Franks)\
**Post date:** [August 9, 2012, 12:03am UTC](https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963/1 "2012-08-09T00:03:25Z")

</div>

How can I tie a button to a key? What I want is when I press the left keypad I want a bit to stay on while it’s pressed and when released set to back to 0. I tried doing it with keystroke client scripts but it’s not real reliable.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [August 9, 2012, 12:18am UTC](https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963/2 "2012-08-09T00:18:21Z")

</div>

This is going to be tricky. The trouble is that no matter where you put your script, it’ll only work if no other component consumes that key stroke first.

I think the best way would be to put the script on a component that takes input focus and write a keystroke script on that component. And then train the operators to focus that component before trying to use the arrow key mechanism.

From a technical standpoint, there is a way to register a much higher level listener in Swing called an AWT event listener. This would be able to see the arrow keystroke no matter what the input selection was. Doing this is a bit, shall we say, “custom”, and will be a bit tricky to get right. (It’s not really a feature of Ignition, but a feature of the underlying Java Swing system)

---

<div class="post-metadata">

**Author:** ![Micky.Franks](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Micky.Franks](https://forum.inductiveautomation.com/u/Micky.Franks)\
**Post date:** [August 9, 2012, 1:21am UTC](https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963/3 "2012-08-09T01:21:28Z")

</div>

It’s more for a toy I’m building. I’m trying to put together spare parts I have and build a remote operated vehicle with a camera on the front and have all the controls in Ignition. I can do it with a joystick but then that’s another interface I would have to build and carry. I was trying to make it just two pieces, my laptop and the bot. I have an Ethernet radio and an ADAM Ethernet I/O module I was gonna use for the control of the vehicle.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [August 9, 2012, 3:52pm UTC](https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963/4 "2012-08-09T15:52:05Z")

</div>

Alright, try this.

Also, don’t call into support asking questions about this script for your toy please or I’ll get in trouble 😉

As should be obvious, you’ll want to insert your own code where the print statements are.

[code]from java.awt import Toolkit  
from java.awt import AWTEvent  
from java.awt.event import AWTEventListener

class KeyListener(AWTEventListener):  
def eventDispatched(self, event):  
from java.awt.event import KeyEvent  
if event.getID() == KeyEvent.KEY\_PRESSED:  
self.keyDown(event.getKeyCode())  
elif event.getID() == KeyEvent.KEY\_RELEASED:  
self.keyUp(event.getKeyCode())

```
def keyDown(self, code):
	from java.awt.event import KeyEvent
	if code == KeyEvent.VK_LEFT:
		print 'LEFT DOWN'
	elif code == KeyEvent.VK_RIGHT:
		print 'RIGHT DOWN'
		
def keyUp(self, code):
	from java.awt.event import KeyEvent
	if code == KeyEvent.VK_LEFT:
		print 'LEFT UP'
	elif code == KeyEvent.VK_RIGHT:
		print 'RIGHT UP'

```

Toolkit.getDefaultToolkit().addAWTEventListener(KeyListener(), AWTEvent.KEY\_EVENT\_MASK)[/code]

---

<div class="post-metadata">

**Author:** ![Micky.Franks](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Micky.Franks](https://forum.inductiveautomation.com/u/Micky.Franks)\
**Post date:** [August 9, 2012, 8:14pm UTC](https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963/5 "2012-08-09T20:14:24Z")

</div>

[quote=“Carl.Gould”]Alright, try this.

Also, don’t call into support asking questions about this script for your toy please or I’ll get in trouble 😉  
[/quote]  
 ![:mrgreen:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/d23659b203d2d24d394be983a52adb2fa6fe23d4.gif "Mr. Green") no problem

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [August 9, 2012, 9:02pm UTC](https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963/6 "2012-08-09T21:02:01Z")

</div>

I forgot to mention: this script should be in your project’s client startup script.

---

<div class="post-metadata">

**Author:** ![Micky.Franks](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Micky.Franks](https://forum.inductiveautomation.com/u/Micky.Franks)\
**Post date:** [August 9, 2012, 11:51pm UTC](https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963/7 "2012-08-09T23:51:34Z")

</div>

Would this be a script that runs at startup or does it have to run all the time?

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [August 10, 2012, 2:04am UTC](https://forum.inductiveautomation.com/t/tying-button-to-key-stroke/4963/8 "2012-08-10T02:04:10Z")

</div>

Client startup script.
