|
| 1 | +From 3d6df4c8194fae352369e7ce5a715c92f2c085de Mon Sep 17 00:00:00 2001 |
| 2 | +From: Luca Magrone <luca@magrone.cc> |
| 3 | +Date: Sun, 13 Oct 2024 17:10:41 +0200 |
| 4 | +Subject: [PATCH] CIEID: Allow the user to exit with ctrl+q and ctrl+w |
| 5 | + |
| 6 | +Attach a AWTEventListener to the Toolkit that checks if the user presses |
| 7 | +the combinatrions CTRL+Q or CTRL+W. On press close the current window. |
| 8 | + |
| 9 | +Signed-off-by: Luca Magrone <luca@magrone.cc> |
| 10 | +--- |
| 11 | + CIEID/src/it/ipzs/cieid/MainApplication.java | 29 ++++++++++++++++++++ |
| 12 | + 1 file changed, 29 insertions(+) |
| 13 | + |
| 14 | +diff --git a/CIEID/src/it/ipzs/cieid/MainApplication.java b/CIEID/src/it/ipzs/cieid/MainApplication.java |
| 15 | +index 79904d5..18ad302 100644 |
| 16 | +--- a/CIEID/src/it/ipzs/cieid/MainApplication.java |
| 17 | ++++ b/CIEID/src/it/ipzs/cieid/MainApplication.java |
| 18 | +@@ -1,11 +1,17 @@ |
| 19 | + package it.ipzs.cieid; |
| 20 | + |
| 21 | + import java.awt.EventQueue; |
| 22 | ++import java.awt.AWTEvent; |
| 23 | ++import java.awt.Toolkit; |
| 24 | ++import java.awt.Window; |
| 25 | ++import java.awt.event.AWTEventListener; |
| 26 | ++import java.awt.event.KeyEvent; |
| 27 | + import java.nio.file.Files; |
| 28 | + import java.nio.file.Path; |
| 29 | + import java.nio.file.Paths; |
| 30 | + |
| 31 | + import javax.swing.JFrame; |
| 32 | ++import javax.swing.FocusManager; |
| 33 | + |
| 34 | + import ch.swingfx.twinkle.NotificationBuilder; |
| 35 | + import ch.swingfx.twinkle.event.NotificationEvent; |
| 36 | +@@ -26,6 +32,7 @@ public class MainApplication { |
| 37 | + public static void main(final String[] args) { |
| 38 | + EventQueue.invokeLater(new Runnable() { |
| 39 | + public void run() { |
| 40 | ++ installKeyboardMonitor(); |
| 41 | + |
| 42 | + if(args.length > 0 && args[0].equals("pinwrong")) |
| 43 | + { |
| 44 | +@@ -51,6 +58,28 @@ public class MainApplication { |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | ++ /** |
| 49 | ++ * Close the application on ctrl+q |
| 50 | ++ */ |
| 51 | ++ public static void installKeyboardMonitor() { |
| 52 | ++ Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { |
| 53 | ++ @Override |
| 54 | ++ public void eventDispatched(AWTEvent event) { |
| 55 | ++ KeyEvent ke = (KeyEvent) event; |
| 56 | ++ if (ke.getID() == KeyEvent.KEY_PRESSED) { |
| 57 | ++ if (ke.getKeyCode() == KeyEvent.VK_Q || ke.getKeyCode() == KeyEvent.VK_W) { |
| 58 | ++ if (ke.isControlDown()) { |
| 59 | ++ Window window = FocusManager.getCurrentManager().getActiveWindow(); |
| 60 | ++ if (window != null) { |
| 61 | ++ window.dispose(); |
| 62 | ++ } |
| 63 | ++ } |
| 64 | ++ } |
| 65 | ++ } |
| 66 | ++ } |
| 67 | ++ }, AWTEvent.KEY_EVENT_MASK); |
| 68 | ++ } |
| 69 | ++ |
| 70 | + public static void showUI(String[] args) |
| 71 | + { |
| 72 | + MainApplication window = new MainApplication(args); |
| 73 | +-- |
| 74 | +2.43.5 |
| 75 | + |
0 commit comments