private static String getDownModifierMask(KeyCombination kc) { StringBuilder contextMenuKeyModifiers = new StringBuilder(); if (kc.getControl() == ModifierValue.DOWN) { contextMenuKeyModifiers.append("Ctrl+"); } if (kc.getAlt() == ModifierValue.DOWN) { contextMenuKeyModifiers.append("Alt+"); } if (kc.getMeta() == ModifierValue.DOWN) { contextMenuKeyModifiers.append("Meta+"); } if (kc.getShift() == ModifierValue.DOWN) { contextMenuKeyModifiers.append("Shift+"); } return contextMenuKeyModifiers.toString(); }
private String convert(KeyEvent e) { return TaskUtil.firstSuccess( () -> new KeyCodeCombination(e.getCode(), e.isShiftDown() ? ModifierValue.DOWN : ModifierValue.UP, e.isControlDown() ? ModifierValue.DOWN : ModifierValue.UP, e.isAltDown() ? ModifierValue.DOWN : ModifierValue.UP, e.isMetaDown() ? ModifierValue.DOWN : ModifierValue.UP, ModifierValue.UP).toString(), () -> { KeyCodeCombination key = new KeyCodeCombination(KeyCode.A, e.isShiftDown() ? ModifierValue.DOWN : ModifierValue.UP, e.isControlDown() ? ModifierValue.DOWN : ModifierValue.UP, e.isAltDown() ? ModifierValue.DOWN : ModifierValue.UP, e.isMetaDown() ? ModifierValue.DOWN : ModifierValue.UP, ModifierValue.UP); String name = key.getName(); return name.substring(0, name.length() - key.getCode().getName().length()); }); }
/** * Presses the provided {@link KeyCodeCombination}'s keys. * * @param kb the KeyCodeCombination * @return true, if successful */ @SuppressWarnings("deprecation") protected boolean keysDown(KeyCodeCombination kb) { try { if (kb.getShortcut() == ModifierValue.DOWN) { robot.keyPress(shortcutKeyEvent); Thread.sleep(modifiersPressSleepTime); } if (kb.getAlt() == ModifierValue.DOWN) { robot.keyPress(KeyEvent.VK_ALT); Thread.sleep(modifiersPressSleepTime); } if (kb.getShift() == ModifierValue.DOWN) { robot.keyPress(KeyEvent.VK_SHIFT); Thread.sleep(modifiersPressSleepTime); } // I know this is a bad practice. Do you have any idea how to avoid this, stranger? robot.keyPress(kb.getCode().impl_getCode()); Thread.sleep(modifiersPressSleepTime); } catch (InterruptedException e) { logger.debug("Haha, not this time, InterruptedException!"); return false; } return true; }
/** * Releases the provided {@link KeyCodeCombination}'s keys. * * @param kb the KeyCodeCombination * @return true, if successful */ @SuppressWarnings("deprecation") protected boolean keysUp(KeyCodeCombination kb) { try { // I know this is a bad practice. Do you have any idea how to avoid this, stranger? robot.keyRelease(kb.getCode().impl_getCode()); Thread.sleep(modifiersPressSleepTime); if (kb.getShift() == ModifierValue.DOWN) { robot.keyRelease(KeyEvent.VK_SHIFT); Thread.sleep(modifiersPressSleepTime); } if (kb.getAlt() == ModifierValue.DOWN) { robot.keyRelease(KeyEvent.VK_ALT); Thread.sleep(modifiersPressSleepTime); } if (kb.getShortcut() == ModifierValue.DOWN) { robot.keyRelease(shortcutKeyEvent); } } catch (InterruptedException e) { logger.debug("Haha, not this time, InterruptedException!"); return false; } return true; }
@Override public RController type(KeyCodeCombination... combinations) { for( KeyCodeCombination combination : combinations ) { if( combination.getAlt() == ModifierValue.DOWN ) { glassRobot.keyPress(JavaFXCompatUtil.getCode(KeyCode.ALT)); } if( combination.getShift() == ModifierValue.DOWN ) { glassRobot.keyPress(JavaFXCompatUtil.getCode(KeyCode.SHIFT)); } if( combination.getControl() == ModifierValue.DOWN ) { glassRobot.keyPress(JavaFXCompatUtil.getCode(KeyCode.CONTROL)); } glassRobot.keyPress(JavaFXCompatUtil.getCode(combination.getCode())); glassRobot.keyRelease(JavaFXCompatUtil.getCode(combination.getCode())); if( combination.getAlt() == ModifierValue.DOWN ) { glassRobot.keyRelease(JavaFXCompatUtil.getCode(KeyCode.ALT)); } if( combination.getShift() == ModifierValue.DOWN ) { glassRobot.keyRelease(JavaFXCompatUtil.getCode(KeyCode.SHIFT)); } if( combination.getControl() == ModifierValue.DOWN ) { glassRobot.keyRelease(JavaFXCompatUtil.getCode(KeyCode.CONTROL)); } } return this; }
/** * Converts the given {@link Key} to a {@link KeyCombination}. * * @param pKey the {@link Key} to convert. * @return the {@link KeyCombination}. */ public static KeyCombination keyToKeyCombination(Key pKey) { if (pKey == null) { return null; } KeyCode keyCode = keyToKeyCode(pKey); ModifierValue altModifier = ModifierValue.UP; ModifierValue ctrlModifier = ModifierValue.UP; ModifierValue shiftModifier = ModifierValue.UP; if ((pKey.getModifiers() & UIKeyEvent.ALT_MASK) == UIKeyEvent.ALT_MASK) { altModifier = ModifierValue.DOWN; } if ((pKey.getModifiers() & UIKeyEvent.CTRL_MASK) == UIKeyEvent.CTRL_MASK) { ctrlModifier = ModifierValue.DOWN; } if ((pKey.getModifiers() & UIKeyEvent.SHIFT_MASK) == UIKeyEvent.SHIFT_MASK) { shiftModifier = ModifierValue.DOWN; } return new KeyCodeCombination(keyCode, ctrlModifier, shiftModifier, altModifier, ModifierValue.UP, ModifierValue.UP); }
private String generateStubKeyCombinationString() { ArrayList<ModifierValue> modifiers = Util.generateDefaultModifiersForKeyCombination(); // No way to create KeyCodeCombination with null keycode, using "Clear" that is not located // on keyboard; KeyCodeCombination kb = new KeyCodeCombination(emptyCode, modifiers.get(0), modifiers.get(1), modifiers.get(2), modifiers.get(3), modifiers.get(4)); return kb.getName(); }
private String modifiersTextFromKeyCombination(KeyCodeCombination kcc) { if (kcc == null) { return ""; } ModifierValue downMod = ModifierValue.DOWN; // If any modifier exist if (kcc.getAlt() == downMod || kcc.getShortcut() == downMod || kcc.getShift() == downMod) { String FullHotkey = kcc.getDisplayText(); int lastPlusSignIndex = FullHotkey.lastIndexOf("+"); return FullHotkey.substring(0, lastPlusSignIndex); } return ""; }
private ArrayList<ModifierValue> getCurrentModifiers() { ArrayList<ModifierValue> modifiers = Util.generateDefaultModifiersForKeyCombination(); if (keyCombinationProperty.get() != null) { modifiers.set(0, keyCombinationProperty.get().getShift()); modifiers.set(2, keyCombinationProperty.get().getAlt()); // control on Windows and meta (command key) on Mac modifiers.set(4, keyCombinationProperty.get().getShortcut()); } return modifiers; }
private void setNewModifiersFromInput(ArrayList<ModifierValue> modifiers) { if (keyCombinationProperty.get() == null) { throw new RuntimeException( "Modifiers field should be inactive when Key field is empty"); } keyCombinationProperty.set( new KeyCodeCombination(keyCombinationProperty.get().getCode(), modifiers.get(0), modifiers.get(1), modifiers.get(2), modifiers.get(3), modifiers.get(4))); }
private String modifiersTextFromKeyCombination(KeyCodeCombination kcc) { if (kcc == null) { return ""; } ModifierValue downMod = ModifierValue.DOWN; // If any modifier exist if (kcc.getAlt() == downMod || kcc.getShortcut() == downMod || kcc.getShift() == downMod) { String FullHotkeyText = kcc.getDisplayText(); int lastPlusSignIndex = FullHotkeyText.lastIndexOf("+"); return FullHotkeyText.substring(0, lastPlusSignIndex); } return ""; }
private ArrayList<ModifierValue> getCurrentModifiers() { ArrayList<ModifierValue> modifiers = Util.generateDefaultModifiersForKeyCombination(); modifiers.set(0, keyCombinationProperty.get().getShift()); modifiers.set(2, keyCombinationProperty.get().getAlt()); // control on Windows and meta (command key) on Mac modifiers.set(4, keyCombinationProperty.get().getShortcut()); return modifiers; }
private void setNewModifiersFromInput(ArrayList<ModifierValue> modifiers) { keyCombinationProperty.set( new KeyCodeCombination(keyCombinationProperty.get().getCode(), modifiers.get(0), modifiers.get(1), modifiers.get(2), modifiers.get(3), modifiers.get(4))); String newModifiers = modifiersTextFromKeyCombination(keyCombinationProperty.get()); KeyCodeCombination originalKB = parseKeyCodeCombinationFromString(origHkAction.getHotkey()); buttonStateManager.reportNewValueOfControl(modifiersTextFromKeyCombination(originalKB), newModifiers, modifiersTextField, null); }
/** * Creates a new instance of {@link JavaFXFrame}. * * @param pStage the stage. */ @SuppressWarnings("deprecation") protected JavaFXFrame(Stage pStage) { super(null); stage = pStage; stage.hide(); setResource((Region)toolBarPanel.getResource()); // Add a style class to the new resource, so that the background // (and other stuff) can be set on it. resource.getStyleClass().add("root-node"); FXPositioningPane overlayPane = new FXPositioningPane(); overlayPane.setAutoSizeOnlyOnPositioning(true); root = new FXOverlayRegion((Pane)resource, overlayPane); FXSceneLocker.makeLockRoot(root); customWidth = getDouble(SYSPROP_SCENE_WIDTH); customHeight = getDouble(SYSPROP_SCENE_HEIGHT); FXZoomableHelper.setDefaultZoomEnabled(System.getProperty(SYSPROP_ZOOM) == null || Boolean.getBoolean(SYSPROP_ZOOM)); boolean isStageHidden = System.getProperty(SYSPROP_STAGE_HIDDEN) != null && Boolean.getBoolean(SYSPROP_STAGE_HIDDEN); boolean isSceneStyled = System.getProperty(SYSPROP_SCENE_STYLED) == null || Boolean.getBoolean(SYSPROP_SCENE_STYLED); if (!isStageHidden && isSceneStyled) { scene = new StyledScene(pStage, root, customWidth, customHeight); } else { scene = new Scene(root, customWidth, customHeight); pStage.setScene(scene); if (isStageHidden) { stage.initStyle(StageStyle.TRANSPARENT); scene.setFill(Color.TRANSPARENT); } } scene.setCamera(new PerspectiveCamera()); scene.getStylesheets().add("/com/sibvisions/rad/ui/javafx/impl/css/default.css"); stage.setOnCloseRequest(this::onStageClose); // TODO HACK JavaFX doesn't have an official API for setting a focus policy. RT-19379/RT-21209/RT-25538 resource.setImpl_traversalEngine(new ParentTraversalEngine(resource, new JavaFXFocusTraversalAlgorithm())); ScenicViewLoader.attach(scene, new KeyCodeCombination( KeyCode.S, // S ModifierValue.DOWN, // Shift ModifierValue.DOWN, // Control ModifierValue.UP, // Alt ModifierValue.UP, // Meta ModifierValue.UP // Shortcut )); }
@SuppressWarnings("deprecation") @Override public void nativeKeyPressed(NativeKeyEvent e) { if (hotkeyIsPressed) { return; // Do nothing if Hotkey is already pressed. } int modifiers = e.getModifiers(); if (modifiers == 0) return; // Do nothing when no modifiers are pressed, because it's not a hotkey. boolean isAltPressed = (modifiers & NativeKeyEvent.ALT_MASK) != 0; boolean isShiftPressed = (modifiers & NativeKeyEvent.SHIFT_MASK) != 0; // Shortcut modifier is Control modifier in Windows/Linux system and Meta (Command) in // Mac systems. boolean isShortcutPressed = (modifiers & NativeKeyEvent.CTRL_MASK) + (modifiers & NativeKeyEvent.META_MASK) != 0; for (Entry<Hotkey, ObjectProperty<KeyCodeCombination>> entry : registeredHotkeys .entrySet()) { KeyCodeCombination kcc = entry.getValue().get(); if (kcc == null) continue; // Do nothing if KeyCodeCombination is not set for this Hotkey. // FIXME: provide compatibility with Java 9 (pretty easy). if (e.getRawCode() != kcc.getCode().impl_getCode()) continue; // Key code does not match Hotkey. if (isAltPressed != (kcc.getAlt() == ModifierValue.DOWN)) continue; // Alt modifier doesn't match Hotkey. if (isShiftPressed != (kcc.getShift() == ModifierValue.DOWN)) continue; // Shift modifier doesn't match Hotkey. if (isShortcutPressed != (kcc.getShortcut() == ModifierValue.DOWN)) continue; // Shortcut modifier doesn't match Hotkey. if (hotkeyIsRunning) { logger.error("Previously pressed Hotkey is still running."); return; // Do nothing if Hotkey is still running. } // At this point the pressed key and modifiers match one of the registered Hotkeys. // Let's run the associated runnable with this Hotkey. Hotkey hotkey = entry.getKey(); logger.info("Catched the Hotkey: \"" + hotkey.name() + "\". Running the associated action."); hotkeyIsPressed = true; hotkeyIsRunning = true; Runnable task = () -> { hotkey.run(); logger.info("Finished running the action associated with the Hotkey: \"" + hotkey.name() + "\"."); hotkeyIsRunning = false; }; new Thread(task).start(); break; } }
/** * Generate list for {@link KeyCombination} with {@link ModifierValue#ANY} values. * * @return The list with {@link ModifierValue#ANY} values. */ public static ArrayList<ModifierValue> generateDefaultModifiersForKeyCombination() { ArrayList<ModifierValue> modifiers = new ArrayList<ModifierValue>( Collections.nCopies(5, ModifierValue.ANY)); return modifiers; }
private void initializeInputHandlers() { keyTextField.setOnKeyPressed(e -> { // if (keycode.isLetterKey() || keycode.isDigitKey() || keycode.isFunctionKey()) { if (!e.getCode().isModifierKey()) { if (e.getCode() == KeyCode.BACK_SPACE || e.getCode() == KeyCode.DELETE) { // Clean up key and modifiers keyCombinationProperty.set(null); } else { setNewKeyCodeFromInput(e.getCode()); } } e.consume(); }); modifiersTextField.addEventFilter(KeyEvent.ANY, e -> { if (e.getEventType() != KeyEvent.KEY_PRESSED) { e.consume(); return; } // This is a list with default modifiers - ModifierValue.ANY. ArrayList<ModifierValue> modifiers = Util.generateDefaultModifiersForKeyCombination(); if (e.getCode() == KeyCode.BACK_SPACE || e.getCode() == KeyCode.DELETE) { // Clean up modifiers. setNewModifiersFromInput(modifiers); } if (e.getCode().isModifierKey()) { boolean anyModifiersPressed = false; if (e.isShiftDown()) { modifiers.set(0, ModifierValue.DOWN); anyModifiersPressed = true; } if (e.isAltDown()) { modifiers.set(2, ModifierValue.DOWN); anyModifiersPressed = true; } // control on Windows and meta (command key) on Mac if (e.isShortcutDown()) { modifiers.set(4, ModifierValue.DOWN); anyModifiersPressed = true; } if (anyModifiersPressed) { setNewModifiersFromInput(modifiers); } } e.consume(); }); restoreDefaultButton.setOnAction((e) -> { restoreDefaultValueOfKeyCodeCombination(); }); }
private void setNewKeyCodeFromInput(KeyCode keycode) { ArrayList<ModifierValue> modifiers = getCurrentModifiers(); keyCombinationProperty.set(new KeyCodeCombination(keycode, modifiers.get(0), modifiers.get(1), modifiers.get(2), modifiers.get(3), modifiers.get(4))); }
private void initializeHandlers() { keyTextField.setOnKeyPressed(e -> { // if (keycode.isLetterKey() || keycode.isDigitKey() || keycode.isFunctionKey()) { if (!e.getCode().isModifierKey()) { setNewKeyCodeFromInput(e.getCode()); } e.consume(); }); modifiersTextField.addEventFilter(KeyEvent.ANY, e -> { if (e.getEventType() != KeyEvent.KEY_PRESSED) { e.consume(); return; } if (e.getCode() == KeyCode.ESCAPE) { // When root's parent is focused, second hit to Escape key will close panel root.getParent().requestFocus(); } // This is a list with default modifiers - ModifierValue.ANY. ArrayList<ModifierValue> modifiers = Util.generateDefaultModifiersForKeyCombination(); if (e.getCode().isModifierKey()) { boolean anyModifiersPressed = false; if (e.isShiftDown()) { modifiers.set(0, ModifierValue.DOWN); anyModifiersPressed = true; } if (e.isAltDown()) { modifiers.set(2, ModifierValue.DOWN); anyModifiersPressed = true; } // control on Windows and meta (command key) on Mac if (e.isShortcutDown()) { modifiers.set(4, ModifierValue.DOWN); anyModifiersPressed = true; } if (anyModifiersPressed) { setNewModifiersFromInput(modifiers); } } else { if (e.getCode() == KeyCode.BACK_SPACE || e.getCode() == KeyCode.DELETE) { setNewModifiersFromInput(modifiers); } } e.consume(); }); }