# Capture Key Press and Do something if it is a enter key

**URL:** <https://forum.inductiveautomation.com/t/capture-key-press-and-do-something-if-it-is-a-enter-key/89384>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [April 24, 2024, 5:03pm UTC](https://forum.inductiveautomation.com/t/capture-key-press-and-do-something-if-it-is-a-enter-key/89384 "2024-04-24T17:03:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt\_Kerg](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@matt\_Kerg](https://forum.inductiveautomation.com/u/matt_Kerg)\
**Post date:** [April 24, 2024, 5:03pm UTC](https://forum.inductiveautomation.com/t/capture-key-press-and-do-something-if-it-is-a-enter-key/89384/1 "2024-04-24T17:03:45Z")

</div>

I apologize if the answer is already on here but I did a pretty good search.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/7/67cca18ab5bb7798739cbf0707c71390e2e06e3c.png)  
if I change the number and then hit enter I want to popup and ask Yes, No then proceed or exit.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/a/9a7ba6e8c19ddbde4904a99de4af823202cbd8dc.png)

```python
def runAction(self, event):
	system.perspective.print(message='Key ' + event.keyCode + event.key, destination="all")
	self.session.custom.newSequenceNumber = event.keyCode
	if event.key == '\n':
		system.perspective.openPopup("messageBox",'ChangeSequence', title='Key ' + event.keyCode + event.key, showCloseIcon = True)
	else:
		system.perspective.openPopup("messageBox",'ChangeSequence', title='Key ' + event.keyCode + event.key, showCloseIcon = True)
		
		#system.perspective.openPopup("messageBox",'Popups/MessageBox', params = {'message':self.getSibling("TextField").props.text})

```

So I have this Table that I want to run this script on but want to ask yes or no to first.  
I am have problems opening popup or even printing to console's

```python
|||
|---|---|
||def get_sequence_values(data):|
|| return [data.getValueAt(row, 'Sequence') for row in range(data.getRowCount())]|
|| |
||def get_sorted_indices(sequence_values):|
|| return sorted(range(len(sequence_values)), key=lambda k: sequence_values[k])|
|| |
||def get_row_values(data, row_index):|
|| return [data.getValueAt(row_index, col) for col in range(data.getColumnCount())]|
|||
||def swap_rows(data, row_index1, row_index2):|
|| if 0 <= row_index1 < data.getRowCount() and 0 <= row_index2 < data.getRowCount():|
|| if row_index1 != row_index2:|
|| row1_values = get_row_values(data, row_index1)|
|| row2_values = get_row_values(data, row_index2)|
|| # Delete the row with the higher index first|
|| if row_index1 < row_index2:|
|| data = system.dataset.deleteRow(data, row_index2)|
|| data = system.dataset.deleteRow(data, row_index1)|
|| data = system.dataset.insertRow(data, row_index1, row2_values)|
|| data = system.dataset.insertRow(data, row_index2, row1_values)|
|| else:|
|| data = system.dataset.deleteRow(data, row_index1)|
|| data = system.dataset.deleteRow(data, row_index2)|
|| data = system.dataset.insertRow(data, row_index2, row1_values)|
|| data = system.dataset.insertRow(data, row_index1, row2_values)|
|| return data|
||def adjust_sequence_values(data):|
|| for row in range(data.getRowCount()):|
|| data = system.dataset.setValue(data, row, 'Sequence', row + 1)|
|| return data|
||newValue = event.value|
|||
||columnName = event.column|
||data = self.props.data|
||maxSequence = max(get_sequence_values(data))|
||if columnName == 'Sequence' and newValue is not None:|
|||
|| if newValue <= maxSequence:|
|| originalSequenceValues = get_sequence_values(data)|
|| newData = system.dataset.setValue(data, event.row, columnName, newValue)|
|| newRowIndex = originalSequenceValues.index(newValue)|
|| if newRowIndex != event.row:|
|| newData = swap_rows(newData, event.row, newRowIndex)|
|| newData = adjust_sequence_values(newData)|
|| self.props.data = newData|

```

---

<div class="post-metadata">

**Author:** ![danieltalbot](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/danieltalbot/32/11201_2.png) [@danieltalbot](https://forum.inductiveautomation.com/u/danieltalbot)\
**Post date:** [April 24, 2024, 6:51pm UTC](https://forum.inductiveautomation.com/t/capture-key-press-and-do-something-if-it-is-a-enter-key/89384/2 "2024-04-24T18:51:05Z")

</div>

I think this sort of thing is a lot easier to do when you make your editable columns into embedded views. If you're unfamiliar with that process, I have a video on it [https://www.youtube.com/watch?v=xQiiJkSnrvg](https://www.youtube.com/watch?v=xQiiJkSnrvg)

Ultimately, with an embedded view you can forget about keyboard events altogether and either use an onActionPerformed event or a change script on the component to open a confirmation popup after the field is edited.
