# Add Key Listener on Power Table

**URL:** <https://forum.inductiveautomation.com/t/add-key-listener-on-power-table/93603>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [July 23, 2024, 2:15pm UTC](https://forum.inductiveautomation.com/t/add-key-listener-on-power-table/93603 "2024-07-23T14:15:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lcies](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@lcies](https://forum.inductiveautomation.com/u/lcies)\
**Post date:** [July 23, 2024, 2:15pm UTC](https://forum.inductiveautomation.com/t/add-key-listener-on-power-table/93603/1 "2024-07-23T14:15:42Z")

</div>

Hello,

i should add onKeyPress event on Power Table component. To do this, from what i understand, i need to add a Listener on the Key side.  
I tried to do it using some code inside **initialize** extension function but it the event doesn't fired out:

```python
from java.awt.event import KeyListener	#KeyAdapter
		
#class KA(KeyAdapter):
class KL(KeyListener):
	
	def keyPressed(ev):
	
		print ev.getKeyCode(), ev.getKeyChar()
	
c = KL()	#KA()
self.addKeyListener(c)

```

Neither the KeyListener nor the KeyAdapter work when a key is pressed.

- Does anyone know how to do this?

PS: I saw in the forum ([here](https://forum.inductiveautomation.com/t/keystroke-client-event-script-keycode-hex-values/24613/6)) that someone has already succeeded using different functions.. but i simply want to print the .getKeyChar() and the .getKeyCode() of the key just pressed.

Thanks

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 23, 2024, 3:07pm UTC](https://forum.inductiveautomation.com/t/add-key-listener-on-power-table/93603/2 "2024-07-23T15:07:20Z")

</div>

What are you actually trying to do with the keyboard and your table? The right approach is going to vary based on what your ultimate goal is.

---

<div class="post-metadata">

**Author:** ![lcies](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@lcies](https://forum.inductiveautomation.com/u/lcies)\
**Post date:** [July 24, 2024, 7:30am UTC](https://forum.inductiveautomation.com/t/add-key-listener-on-power-table/93603/3 "2024-07-24T07:30:15Z")

</div>

I simply want to print on Client's console the .getKeyChar() and the .getKeyCode() of the key tah i just pressed when i focus any table's cell..

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [July 24, 2024, 1:13pm UTC](https://forum.inductiveautomation.com/t/add-key-listener-on-power-table/93603/4 "2024-07-24T13:13:46Z")

</div>

I like to add keyboard shortcuts to power tables in some cases. I do this by adding the following to the 'initialize' extension function of the table.

In the example below deleteRow is a custom method that I added to the table. I always use a custom method so that I can trigger the same action with either a key press or a right click event.

The script will not work in the desginer though without a couple of extra steps. To verify it works in the designer you will need to start preview mode with the window closed. Open the window while still in preview mode. That will cause the initialize script to run and the keyboard shortcut will work as long as the table has focus.

- Edited to add a missing line to the script

```python
from javax.swing import AbstractAction, JTable, KeyStroke	
from java.awt.event import KeyEvent

class DeleteRow(AbstractAction):
	def __init__ (self, tc):
		self.tc = tc
	def actionPerformed(self, event):
		self.tc.deleteRow()	

table = self.getTable()

ctrl_D = KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_DOWN_MASK)
ctrl_D_Pressed = "ControlDPressed"

table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(ctrl_D, ctrl_D_Pressed)
table.getActionMap().put(ctrl_D_Pressed, DeleteRow(self))	

```

---

<div class="post-metadata">

**Author:** ![lcies](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@lcies](https://forum.inductiveautomation.com/u/lcies)\
**Post date:** [July 25, 2024, 7:50am UTC](https://forum.inductiveautomation.com/t/add-key-listener-on-power-table/93603/5 "2024-07-25T07:50:13Z")

</div>

Hello,

I've already done something similar to you:

```python
from javax.swing import AbstractAction, JComponent, KeyStroke
		
class MyAction(AbstractAction):
	
	def actionPerformed(self, ev):
		#print "onKeyPressed!"
		
		cmp = ref.parent.getComponent('Del')
		if cmp.componentEnabled:
			cmp.delTags()				
	
a = MyAction()
k = KeyStroke.getKeyStroke("released DELETE")
	
self.registerKeyboardAction(a, k, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)

```

What i wanted to do now was to also add additional listener instance that would print to the Ignition console the keys i press on the keyboard when i have one or more power table's cells focused.

Thanks

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [July 26, 2024, 2:35am UTC](https://forum.inductiveautomation.com/t/add-key-listener-on-power-table/93603/6 "2024-07-26T02:35:13Z")

</div>

> [@lcies](#):
>
> when i have one or more power table's cells focused.

Why would we need text arbitrarily printed to the console? Is there a bigger picture problem that we are trying to solve? Real time text validation perhaps?
