# Closing a vision popup by pressing Esc

**URL:** <https://forum.inductiveautomation.com/t/closing-a-vision-popup-by-pressing-esc/26573>\
**Category:** Ignition\
**Tags:** ignition80, vision\
**Created:** [July 16, 2019, 9:36am UTC](https://forum.inductiveautomation.com/t/closing-a-vision-popup-by-pressing-esc/26573 "2019-07-16T09:36:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joel.golinucci](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/joel.golinucci/32/9927_2.png) [@joel.golinucci](https://forum.inductiveautomation.com/u/joel.golinucci)\
**Post date:** [July 16, 2019, 9:36am UTC](https://forum.inductiveautomation.com/t/closing-a-vision-popup-by-pressing-esc/26573/1 "2019-07-16T09:36:23Z")

</div>

Hi,

How can I close a custom popup window by pressing Esc ?

On Buttons there is a property “Default Button” making it respond to an Enter pressed on the window, but how can I do a similar behavior with a cancel or close button when pressing Esc ?

Thanks for the help

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [July 16, 2019, 10:46am UTC](https://forum.inductiveautomation.com/t/closing-a-vision-popup-by-pressing-esc/26573/2 "2019-07-16T10:46:23Z")

</div>

We use the code below (as a client event script, triggering on the ESC key)

```auto
windows = system.gui.getOpenedWindows()

for window in windows:
	try:
		isPopUp = ((not window.startMaximized) and (window.closable))
		if (isPopUp and window.isSelected()):
			system.nav.closeWindow(window)
			break
	except:
		pass

```

---

<div class="post-metadata">

**Author:** ![joel.golinucci](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/joel.golinucci/32/9927_2.png) [@joel.golinucci](https://forum.inductiveautomation.com/u/joel.golinucci)\
**Post date:** [July 17, 2019, 5:16am UTC](https://forum.inductiveautomation.com/t/closing-a-vision-popup-by-pressing-esc/26573/3 "2019-07-17T05:16:25Z")

</div>

> [@Sanderd17](#):
>
> as a client event script, triggering on the ESC key

How do you do such a script ?  
When you say a "client event script" is this a script you put in the Scripting/Project Library folder ?

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [July 17, 2019, 5:51am UTC](https://forum.inductiveautomation.com/t/closing-a-vision-popup-by-pressing-esc/26573/4 "2019-07-17T05:51:01Z")

</div>

No, here:

 ![ClientKeystrokeScripts](https://us1.discourse-cdn.com/inductiveautomation/original/2X/5/5c2e31df6c7d302b2587887d003a4e2b20904563.png)

---

<div class="post-metadata">

**Author:** ![joel.golinucci](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/joel.golinucci/32/9927_2.png) [@joel.golinucci](https://forum.inductiveautomation.com/u/joel.golinucci)\
**Post date:** [July 17, 2019, 9:35am UTC](https://forum.inductiveautomation.com/t/closing-a-vision-popup-by-pressing-esc/26573/5 "2019-07-17T09:35:13Z")

</div>

In V8 it was in a slightly different place but it is still quite similar to your printscreen:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/b/b4f3a63d91eb4cd35f87b5d32bfbc9c88195a5de.png)

It’s now working well. Thank you very much for your help !

---

<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:** [September 8, 2022, 3:30am UTC](https://forum.inductiveautomation.com/t/closing-a-vision-popup-by-pressing-esc/26573/6 "2022-09-08T03:30:25Z")

</div>

I was looking at this code earlier today because I wanted to add this functionality to my project, but my project uses multiple desktop instances, and I discovered that this solution only works with the primary desktop. I developed the code a little bit further, so that it will work no matter how many desktop instances are open. I’m still running it off of the ESCAPE(Released) Client Event Script.

Here is the code in case anybody could use it:

```auto
def closeTheWindow(testWindow,selectedDesktop):
	import system
	if testWindow.isFocused():
		windows = system.gui.desktop(selectedDesktop).getOpenedWindows()
		for window in windows:
			try:
				isPopUp = ((not window.startMaximized) and (window.closable))
				if (isPopUp and window.isSelected()):
					system.nav.desktop(selectedDesktop).closeWindow(window)
					break
			except:
				pass
from javax.swing import JFrame,SwingUtilities
header = ["DesktopHeaders"]
desktopList = system.gui.getDesktopHandles();
desktopColumn = [[desktop] for desktop in desktopList]
desktopSet = system.dataset.toDataSet(header, desktopColumn)
desktopCount = desktopSet.getRowCount()
closeTheWindow(SwingUtilities.getAncestorOfClass(JFrame,system.gui.desktop().getWindow(system.nav.desktop().getCurrentWindow())),system.gui.getCurrentDesktop())
for desktop in range(desktopCount):
	selectedDesktop = desktopSet.getValueAt(desktop, 0)
	closeTheWindow(SwingUtilities.getAncestorOfClass(JFrame,system.gui.desktop(desktopSet.getValueAt(desktop, 0)).getWindow(system.nav.desktop(desktopSet.getValueAt(desktop, 0)).getCurrentWindow())),selectedDesktop)

```
