Skip to content

Commit

Permalink
Fix keybindings in entry editor (#2971)
Browse files Browse the repository at this point in the history
* Try to fix other keybindings in entry editor

Try to match awt key events with keycombinations

* Finally fix keybinding
Remove + signs to make swing work again
Add + sign only in binding checker

* Extract method for event converting in keybinding repo
Adapt test
Add + between keybindings
  • Loading branch information
Siedlerchr authored and tobiasdiez committed Jul 6, 2017
1 parent 6897b10 commit edd3f7c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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(() -> {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/jabref/gui/keyboard/KeyBindingRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,25 @@ public Optional<KeyBinding> mapToKeyBinding(KeyEvent keyEvent) {
return Optional.empty();
}

public Optional<KeyBinding> mapToKeyBinding(java.awt.event.KeyEvent keyEvent) {
Optional<KeyCode> 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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> 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);
Expand Down

0 comments on commit edd3f7c

Please sign in to comment.