I would like to modify a power table so that when the user presses a letter or number on the keyboard, it selects the row of the item that starts with that character. If there are multiple rows, just select the first one. I suppose if they keep pressing the same letter, it would be good to cycle through the rows that begin with that letter. But I digress.
I read this which looks similar, but it only works for “ESCAPE” and when I tried to modify it for other characters, it did not seem to work. Nothing seemed to happen in the client when I pressed the key I thought it would work for. I also added a print statement inside the actionPerformed() function of the original code, but it did not print when I hit Escape, so I’m not sure if this code even works on my version of Ignition.
Could you point me in the right direction for capturing all alphanumeric keystrokes on a power table so that I can accomplish this? I’m using Ignition 7.8.4. Thanks!
Here is the simplest modification I tried, just adding the print statement. This is inside the “initialize” extension function of the power table. This does not print escape when Esc is pressed on the table.
from javax.swing import AbstractAction, JComponent, KeyStroke
class MyAction(AbstractAction):
def actionPerformed(self, event):
event.source.selectedRow = -1
print 'escape'
a = MyAction()
k = KeyStroke.getKeyStroke("ESCAPE")
self.registerKeyboardAction(a, k, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
My first attempt at modification was this, just to test with the letter W. I tried this before realizing that the original code did not work for me.
from javax.swing import AbstractAction, JComponent, KeyStroke
class MyAction(AbstractAction):
def actionPerformed(self, event):
print 'W'
a = MyAction()
k = KeyStroke.getKeyStroke("W")
self.registerKeyboardAction(a, k, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
Just for kicks, try replacing your ‘print’ statements with system.util.getLogger(‘something’).info(“message”). If I recall correctly, extension functions don’t have access to the console directly.
Thanks for the suggestion, but there is still no statement in the console when I press Esc. Does it work for you if you put this code in a pwer table's initialize extension function? Here is the updated code.
from javax.swing import AbstractAction, JComponent, KeyStroke
class MyAction(AbstractAction):
def actionPerformed(self, event):
event.source.selectedRow = -1
system.util.getLogger('something').info("message")
a = MyAction()
k = KeyStroke.getKeyStroke("ESCAPE")
self.registerKeyboardAction(a, k, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
I just had a thought and figured something out. I thought that since Escape already has a function on the power table, maybe I should try something else like Space. So I tried that, and when I press the spacebar, it does print the console message. (The print statement also works, even inside the extension function.)
Then I tried it with “W” again, and it does work. I think my problem earlier is that I did not realize it was case sensitive, and also that you have to test this in the client, or by closing and re-opening the window in the Designer (because the initialize extension function is not called when you make a change to the script and save the window, only when the window is opened).
So the next question is, how do I capture all the letters and numbers in one script, without having to type a line for every single case?
[quote=“DougSN”]So the next question is, how do I capture all the letters and numbers in one script, without having to type a line for every single case?[/quote]I was wondering when this particular light bulb would go off… /-:
Strictly speaking, you need a different kind of listener, but that is not available due to the way the power table’s internal components are layered. I’m not entirely sure you can do it. The keyboard action listener is designed to make menu hot-keys work throughout an application, on a key by key basis, using keys combined with ctrl or alt in unambiguous ways.
You might want to check what happens if you make one of the power table’s cells editable and try to type a “W” into that cell. (-:
I wanted to capture when the spacebar was pressed over a PowerTable. I created a script in the Client Event Scripts.Keystroke scripts for the SPACE which captures the spacebar press and finds what component has the focus, and if it’s my “MyPowerTable”, then the script does some work:
from java.awt import KeyboardFocusManager
focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner().getName()
if “MyPowerTable” in focusedComponent:
A minor improvement to this discussion: here's a working script formatted correctly that can be copy/pasted into a power table's initialize function, ready-to-go. I think a broader usage is also more clearly demonstrated by using an example with the CTRL key:
# PowerTable initialize function
# Adds a hotkey action bound to the keystroke "CTRL+D"
from javax.swing import AbstractAction, JComponent, KeyStroke
class MyAction(AbstractAction):
def actionPerformed(self, event):
print 'Success'
a = MyAction()
k = KeyStroke.getKeyStroke("control D")
self.registerKeyboardAction(a, k, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
Edit: this is also relevant for people wanting to add hotkeys to a table with editable cells: