# Capture keystroke/keycode on power table

**URL:** <https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707>\
**Category:** Ignition\
**Created:** [February 22, 2017, 11:01pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707 "2017-02-22T23:01:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![DougSN](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@DougSN](https://forum.inductiveautomation.com/u/DougSN)\
**Post date:** [February 22, 2017, 11:01pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/1 "2017-02-22T23:01:02Z")

</div>

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](https://inductiveautomation.com/forum/viewtopic.php?f=50&t=13950&p=50865) 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!

---

<div class="post-metadata">

**Author:** ![anna](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anna](https://forum.inductiveautomation.com/u/anna)\
**Post date:** [February 23, 2017, 5:27pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/2 "2017-02-23T17:27:07Z")

</div>

DougSN, Could you post your modified code?

---

<div class="post-metadata">

**Author:** ![DougSN](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@DougSN](https://forum.inductiveautomation.com/u/DougSN)\
**Post date:** [February 23, 2017, 6:16pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/3 "2017-02-23T18:16:26Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 23, 2017, 8:21pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/4 "2017-02-23T20:21:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DougSN](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@DougSN](https://forum.inductiveautomation.com/u/DougSN)\
**Post date:** [February 23, 2017, 9:13pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/5 "2017-02-23T21:13:48Z")

</div>

> [@pturmel](#):
>
> Just for kicks, try replacing your 'print' statements with system.util.getLogger('something').info("message").

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)`

---

<div class="post-metadata">

**Author:** ![DougSN](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@DougSN](https://forum.inductiveautomation.com/u/DougSN)\
**Post date:** [February 23, 2017, 9:32pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/6 "2017-02-23T21:32:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 23, 2017, 10:41pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/7 "2017-02-23T22:41:07Z")

</div>

[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. (-:

---

<div class="post-metadata">

**Author:** ![DougSN](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@DougSN](https://forum.inductiveautomation.com/u/DougSN)\
**Post date:** [February 24, 2017, 2:46pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/8 "2017-02-24T14:46:38Z")

</div>

> [@pturmel](#):
>
> 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.

Ah, shucks. Well, thank you for your help Phil. I think I'll come back to this one in a bit.

---

<div class="post-metadata">

**Author:** ![sqlsoul3535](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sqlsoul3535/32/8698_2.png) [@sqlsoul3535](https://forum.inductiveautomation.com/u/sqlsoul3535)\
**Post date:** [April 20, 2019, 6:02pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/9 "2019-04-20T18:02:24Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Ryan\_Shaw](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Ryan\_Shaw](https://forum.inductiveautomation.com/u/Ryan_Shaw)\
**Post date:** [March 23, 2023, 8:13pm UTC](https://forum.inductiveautomation.com/t/capture-keystroke-keycode-on-power-table/12707/10 "2023-03-23T20:13:48Z")

</div>

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:

```auto
# 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:

> [@Power Table Stray Typing](https://forum.inductiveautomation.com/t/power-table-stray-typing/18975):
>
> I could be completely missing something simple with the Power Tables. But is there any way to prevent stray typing from being accepted in a Power Table cell? I would like to make it so the user is forced to double click or press Enter on a cell to be able to actually edit it. However, even with the Edit Click Count property set to 2, it will allow stray typing in a cell that doesn’t have focus but is selected/highlighted This can be problematic for us with operators accidentally bumping keys…
