Tying button to key stroke

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.

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)

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.

Alright, try this.

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

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]

[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 :wink:
[/quote]
:mrgreen: no problem

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

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

Client startup script.