Closing previous dialog box through keyboard shortcut

Hi folks,

Is there a way to close a previous windows programmatically when I use a keyboard shortcut (In the attached image). I have tried with a conditional statement with the aim to detect the previous window and close it, but I think there is not a function that allows to do this (attached image). I have tried with eval() function and javascript function (I don’t know if I could use dispose() once I get the previous windows or all windows references). Also, I would like to use the keyboard shortcut when a dialog box or window is displayed (I have tried NonBlocking GenericDialog, but in order to use the keyboard shortcut I need to click on the Imagej toolbar previously). I checked: " Note that keyboard shortcuts will not work unless the macros are installed and the “ImageJ” window, or an image window, is the active (front) window and has keyboard focus". Could I focus the ImageJ tool bar at the same time than dialog box or windows is displayed to enable a keyboard shortcut?


//Display a first dialog box
macro "start[s]"{
	title = "First Exemplary Dialog ";
	x_1=0; y_1= 0;
	u="o";
	
	script = "var gdo = new GenericDialog('"+title+"');gdo.hideCancelButton();gdo.setLocation("+x_1+","+y_1+");gdo.showDialog();loc = gdo.getLocation();";
	res = eval("script", script);	
	
	//Display a second dialog box
	macro "open[o]"{
		
		title2 = "Exemplary Dialog";
		o=isOpen("Exemplary Dialog");
	

		if (o==true){
			list = getList("window.titles");
     		for (i=0; i<list.length; i++){
     			winame = list[i];
     			i = "Close";
     			script3 = "IJ.selectWindow('"+winame+"'); IJ.run('"+i+"');";
     			res_1 = eval("script", script3);
     			print(res_1);
			}
		}
		x=500; y= 600;
		script2 = "var gd = new NonBlockingGenericDialog('"+title2+"'); gd.hideCancelButton();gd.setLocation("+x+","+y+");gd.showDialog();WindowManager.getNonImageTitles();";
		res_1 = eval("script", script2);	
	}
}