-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix delete entry keybindings bug #1815
Changes from all commits
b646a53
0de49e2
1299685
54e709a
0e1a1f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
|
||
import java.awt.event.KeyAdapter; | ||
import java.awt.event.KeyEvent; | ||
import java.util.ArrayList; | ||
import java.util.Locale; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* respond to grabKey and display the key binding | ||
|
@@ -16,51 +19,93 @@ public KeyBindingsListener(KeyBindingTable table) { | |
|
||
@Override | ||
public void keyPressed(KeyEvent evt) { | ||
// first check if anything is selected if not the return | ||
|
||
boolean isFunctionKey = false; | ||
boolean isEscapeKey = false; | ||
boolean isDeleteKey = false; | ||
|
||
// first check if anything is selected if not then return | ||
final int selRow = table.getSelectedRow(); | ||
boolean isAnyRowSelected = selRow >= 0; | ||
if (!isAnyRowSelected) { | ||
return; | ||
} | ||
|
||
final String modifier = KeyEvent.getKeyModifiersText(evt.getModifiers()); | ||
final String modifier = getModifierText(evt); | ||
|
||
// VALIDATE code and modifier | ||
// all key bindings must have a modifier: ctrl alt etc | ||
if ("".equals(modifier)) { | ||
int kc = evt.getKeyCode(); | ||
boolean isFunctionKey = (kc >= KeyEvent.VK_F1) && (kc <= KeyEvent.VK_F12); | ||
boolean isEscapeKey = kc == KeyEvent.VK_ESCAPE; | ||
boolean isDeleteKey = kc == KeyEvent.VK_DELETE; | ||
isFunctionKey = (kc >= KeyEvent.VK_F1) && (kc <= KeyEvent.VK_F12); | ||
isEscapeKey = kc == KeyEvent.VK_ESCAPE; | ||
isDeleteKey = kc == KeyEvent.VK_DELETE; | ||
if (!(isFunctionKey || isEscapeKey || isDeleteKey)) { | ||
return; // need a modifier except for function, escape and delete keys | ||
// need a modifier except for function, escape and delete keys | ||
return; | ||
} | ||
} | ||
|
||
final String code = KeyEvent.getKeyText(evt.getKeyCode()); | ||
// second key cannot be a modifiers | ||
if ("Tab".equals(code) | ||
|| "Backspace".equals(code) | ||
|| "Enter".equals(code) | ||
|| "Space".equals(code) | ||
|| "Ctrl".equals(code) | ||
|| "Shift".equals(code) | ||
|| "Alt".equals(code)) { | ||
int code = evt.getKeyCode(); | ||
String newKey; | ||
//skip the event triggered only by a modifier, tab, backspace or enter because these normally have preset | ||
// functionality if they alone are pressed | ||
if (code == KeyEvent.VK_ALT || | ||
code == KeyEvent.VK_TAB || | ||
code == KeyEvent.VK_BACK_SPACE || | ||
code == KeyEvent.VK_ENTER || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you comment why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BUMP |
||
code == KeyEvent.VK_SPACE || | ||
code == KeyEvent.VK_CONTROL || | ||
code == KeyEvent.VK_SHIFT || | ||
code == KeyEvent.VK_META) { | ||
return; | ||
} | ||
|
||
// COMPUTE new key binding | ||
String newKey; | ||
if ("".equals(modifier)) { | ||
newKey = code; | ||
if (isFunctionKey) { | ||
newKey = KeyEvent.getKeyText(code); | ||
} else if (isEscapeKey) { | ||
newKey = "ESCAPE"; | ||
} else if (isDeleteKey) { | ||
newKey = "DELETE"; | ||
} else { | ||
return; | ||
} | ||
} else { | ||
newKey = modifier.toLowerCase().replace("+", " ") + " " + code; | ||
newKey = modifier.toLowerCase(Locale.ENGLISH) + " " + KeyEvent.getKeyText(code); | ||
} | ||
|
||
// SHOW new key binding | ||
//SHOW new key binding | ||
//find which key is selected and set its value | ||
table.setValueAt(newKey, selRow, 1); | ||
table.revalidate(); | ||
table.repaint(); | ||
} | ||
|
||
/** | ||
* Collects th English translations of all modifiers and returns them separated by a space | ||
* | ||
* @param evt the KeyEvent that was triggered to set the KeyBindings | ||
* @return a String containing the modifier keys text | ||
*/ | ||
private String getModifierText(KeyEvent evt) { | ||
ArrayList<String> modifiersList = new ArrayList<>(); | ||
|
||
if (evt.isControlDown()) { | ||
modifiersList.add("ctrl"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm missing the meta key, which is especially for mac users an important modifier key. The default shortcuts of JabRef also include this modifier, so please add it. |
||
if (evt.isAltDown()) { | ||
modifiersList.add("alt"); | ||
} | ||
if (evt.isShiftDown()) { | ||
modifiersList.add("shift"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be replaced by one line by using |
||
if (evt.isAltGraphDown()) { | ||
modifiersList.add("alt gr"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that true? altgr without space? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On linux 'alt gr' works just fine. On my windows machine at home, 'ctrl alt' is set instead of 'alt gr' but no matter if it's written with or without space in between. I tested it on the master and 'alt gr' is always parsed as 'ctrl alt' so it works as intended I guess. |
||
} | ||
if (evt.isMetaDown()) { | ||
modifiersList.add("meta"); | ||
} | ||
//Now build the String with all the modifier texts | ||
String modifiersAsString = modifiersList.stream().collect(Collectors.joining(" ")); | ||
return modifiersAsString; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I encountered an unnecessary space so i deleted it