/** * Construct a new hotkey manager * * @param snapper The screensnapper instance */ public HotkeyManager(ScreenSnapper snapper) { this.snapper = snapper; this.provider = HotkeyProvider.getCurrentProvider(false); // Register a shutdown hook just in case Runtime.getRuntime() .addShutdownHook( new Thread() { public void run() { cleanupInput(); } }); }
/** * Initialize the input using JKeymaster * * @throws JSONException */ public void initializeInput() { JSONObject keys = snapper.getConfiguration().getJSONObject("hotkeys"); // A little sanity check, just in case we don't have ANY keys set if (keys == null) { return; } if (keys.has("crop")) { provider.register( KeyStroke.getKeyStroke(keys.getString("crop")), new HotKeyListener() { @Override public void onHotKey(HotKey hotKey) { snapper.crop(); } }); } if (keys.has("full")) { provider.register( KeyStroke.getKeyStroke(keys.getString("full")), new HotKeyListener() { @Override public void onHotKey(HotKey hotKey) { snapper.full(); } }); } if (keys.has("clipboard")) { provider.register( KeyStroke.getKeyStroke(keys.getString("clipboard")), new HotKeyListener() { @Override public void onHotKey(HotKey hotKey) { snapper.clipboard(); } }); } if (keys.has("file")) { provider.register( KeyStroke.getKeyStroke(keys.getString("file")), new HotKeyListener() { @Override public void onHotKey(HotKey hotKey) { snapper.selectFile(); } }); } if (keys.has("options")) { provider.register( KeyStroke.getKeyStroke(keys.getString("options")), new HotKeyListener() { @Override public void onHotKey(HotKey hotKey) { if (!snapper.openSettings()) { snapper .getTrayIcon() .displayMessage( "Error", "Could not open settings, is there another window open?", TrayIcon.MessageType.ERROR); } } }); } // We support active windows only on windows/linux, but OSX SOON! if ((Platform.isWindows() || Platform.isLinux()) && keys.has("active")) { provider.register( KeyStroke.getKeyStroke(keys.getString("active")), new HotKeyListener() { @Override public void onHotKey(HotKey hotKey) { snapper.active(); } }); } initialized = true; }
/** Reset the bound keys and let other classes know that they are not set */ public void resetKeys() { provider.reset(); initialized = false; }
/** Clean up the input bindings */ public void cleanupInput() { resetKeys(); provider.stop(); }