It is currently Thu Jul 24, 2014 9:16 pm




Post new topic Reply to topic  [ 8 posts ] 
 Tying button to key stroke 
Author Message
Private
Private

Joined: Fri Aug 12, 2011 7:09 am
Posts: 34
Post 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.


Wed Aug 08, 2012 4:03 pm
Profile
Moderator
Moderator

Joined: Sun Apr 02, 2006 2:46 pm
Posts: 4044
Location: Sacramento, CA
Post Re: Tying button to key stroke
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)

_________________
Carl Gould
Co-Director, Software Engineering
Inductive Automation


Wed Aug 08, 2012 4:18 pm
Profile WWW
Private
Private

Joined: Fri Aug 12, 2011 7:09 am
Posts: 34
Post Re: Tying button to key stroke
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.


Wed Aug 08, 2012 5:21 pm
Profile
Moderator
Moderator

Joined: Sun Apr 02, 2006 2:46 pm
Posts: 4044
Location: Sacramento, CA
Post Re: Tying button to key stroke
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)

_________________
Carl Gould
Co-Director, Software Engineering
Inductive Automation


Thu Aug 09, 2012 7:52 am
Profile WWW
Private
Private

Joined: Fri Aug 12, 2011 7:09 am
Posts: 34
Post Re: Tying button to key stroke
Carl.Gould wrote:
Alright, try this.

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

:mrgreen: no problem


Thu Aug 09, 2012 12:14 pm
Profile
Moderator
Moderator

Joined: Sun Apr 02, 2006 2:46 pm
Posts: 4044
Location: Sacramento, CA
Post Re: Tying button to key stroke
I forgot to mention: this script should be in your project's client startup script.

_________________
Carl Gould
Co-Director, Software Engineering
Inductive Automation


Thu Aug 09, 2012 1:02 pm
Profile WWW
Private
Private

Joined: Fri Aug 12, 2011 7:09 am
Posts: 34
Post Re: Tying button to key stroke
Would this be a script that runs at startup or does it have to run all the time?


Thu Aug 09, 2012 3:51 pm
Profile
Moderator
Moderator

Joined: Sun Apr 02, 2006 2:46 pm
Posts: 4044
Location: Sacramento, CA
Post Re: Tying button to key stroke
Client startup script.

_________________
Carl Gould
Co-Director, Software Engineering
Inductive Automation


Thu Aug 09, 2012 6:04 pm
Profile WWW
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: