Add Key Listener on Power Table

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:

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

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.

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..

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

2 Likes

Hello,

I've already done something similar to you:

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

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?

1 Like