void (@NotNull Content content) { Alarm alarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, content); AtomicReference<Data> dataRef = new AtomicReference<>(); KeyAdapter listener = new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER && TerminalArrangementManager.isAvailable()) { alarm.cancelAllRequests(); if (!alarm.isDisposed()) { alarm.addRequest(() -> updateWorkingDirectory(content, dataRef.get()), MERGE_WAIT_MILLIS); } } } }; Data data = new Data(listener, content.getDisplayName()); data.myWorkingDirectory = content.getUserData(INITIAL_CWD_KEY); content.putUserData(INITIAL_CWD_KEY, null); dataRef.set(data); JBTerminalWidget widget = TerminalToolWindowManager.getWidgetByContent(content); if (widget != null) { widget.getTerminalPanel().addCustomKeyListener(listener); Disposer.register(content, () -> widget.getTerminalPanel().removeCustomKeyListener(listener)); } myDataByContentMap.put(content, data); }