diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index 4eb738305c2..282d3099d01 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -178,9 +178,21 @@ public void keyReleased(java.awt.event.KeyEvent e) { @Override public void keyPressed(java.awt.event.KeyEvent e) { //We need to consume this event here to prevent the propgation of keybinding events back to the JFrame - - e.consume(); - + Optional keyBinding = Globals.getKeyPrefs().mapToKeyBinding(e); + if (keyBinding.isPresent()) { + switch (keyBinding.get()) { + case CUT: + case COPY: + case PASTE: + case CLOSE_ENTRY_EDITOR: + case DELETE_ENTRY: + case SELECT_ALL: + e.consume(); + break; + default: + //do nothing + } + } } }); DefaultTaskExecutor.runInJavaFXThread(() -> { diff --git a/src/main/java/org/jabref/gui/keyboard/KeyBindingRepository.java b/src/main/java/org/jabref/gui/keyboard/KeyBindingRepository.java index 491f45b7586..1cd94352d1f 100644 --- a/src/main/java/org/jabref/gui/keyboard/KeyBindingRepository.java +++ b/src/main/java/org/jabref/gui/keyboard/KeyBindingRepository.java @@ -126,12 +126,25 @@ public Optional mapToKeyBinding(KeyEvent keyEvent) { return Optional.empty(); } + public Optional mapToKeyBinding(java.awt.event.KeyEvent keyEvent) { + Optional keyCode = Arrays.stream(KeyCode.values()).filter(k -> k.getName().equals(keyEvent.getKeyText(keyEvent.getKeyCode()))).findFirst(); + if (keyCode.isPresent()) { + KeyEvent event = new KeyEvent(keyEvent.getSource(), null, KeyEvent.KEY_PRESSED, "", "", keyCode.get(), keyEvent.isShiftDown(), keyEvent.isControlDown(), keyEvent.isAltDown(), keyEvent.isMetaDown()); + return mapToKeyBinding(event); + + } + + return Optional.empty(); + + } + /** * Returns the KeyStroke for this binding, as defined by the defaults, or in the Preferences. */ public KeyStroke getKey(KeyBinding bindName) { String s = get(bindName.getConstant()); + s = s.replace("+", " "); //swing needs the keys without pluses but whitespace between the modifiers if (OS.OS_X) { return getKeyForMac(KeyStroke.getKeyStroke(s)); diff --git a/src/test/java/org/jabref/gui/keyboard/KeyBindingsDialogViewModelTest.java b/src/test/java/org/jabref/gui/keyboard/KeyBindingsDialogViewModelTest.java index d9e6720790a..92523d3b9a4 100644 --- a/src/test/java/org/jabref/gui/keyboard/KeyBindingsDialogViewModelTest.java +++ b/src/test/java/org/jabref/gui/keyboard/KeyBindingsDialogViewModelTest.java @@ -1,5 +1,10 @@ package org.jabref.gui.keyboard; +import java.awt.event.InputEvent; +import java.util.Optional; + +import javax.swing.JFrame; + import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCombination; import javafx.scene.input.KeyEvent; @@ -168,6 +173,17 @@ public void testSetSingleKeyBindingToDefault() { assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent)); } + @Test + public void testConversionAwtKeyEventJavafxKeyEvent() { + + + java.awt.event.KeyEvent evt = new java.awt.event.KeyEvent(mock(JFrame.class), 0, 0, InputEvent.CTRL_MASK, java.awt.event.KeyEvent.VK_S, java.awt.event.KeyEvent.CHAR_UNDEFINED); + + Optional keyBinding = keyBindingRepository.mapToKeyBinding(evt); + assertEquals(Optional.of(KeyBinding.SAVE_DATABASE), keyBinding); + + } + private KeyBindingViewModel setKeyBindingViewModel(KeyBinding binding) { KeyBindingViewModel bindViewModel = new KeyBindingViewModel(keyBindingRepository, binding, binding.getDefaultKeyBinding()); model.selectedKeyBindingProperty().set(bindViewModel);