RSyntaxTextArea icon indicating copy to clipboard operation
RSyntaxTextArea copied to clipboard

Here is a FIX for num Pad cursor keys not working in Linux

Open stefan-reich opened this issue 7 years ago • 3 comments

Here is the fix for num pad cursor keys on Linux. Run the code before making any RSyntaxTextAreas.

static Map<Integer, Integer> keyStrokeMap = litmap(
  KeyEvent.VK_UP, KeyEvent.VK_KP_UP,
  KeyEvent.VK_DOWN, KeyEvent.VK_KP_DOWN,
  KeyEvent.VK_LEFT, KeyEvent.VK_KP_LEFT,
  KeyEvent.VK_RIGHT, KeyEvent.VK_KP_RIGHT
);

static void fixKeys() {
  RSyntaxTextAreaDefaultInputMap inputMap = new RSyntaxTextAreaDefaultInputMap();
  for (KeyStroke key : inputMap.keys()) {
    Integer code = keyStrokeMap.get(key.getKeyCode());
    if (code != null)
      inputMap.put(KeyStroke.getKeyStroke(code, key.getModifiers()),
        inputMap.get(key));
  }
  UIManager.put("RSyntaxTextAreaUI.inputMap", inputMap);
}

static Map litmap(Object... x) {
  HashMap map = new HashMap();
  for (int i = 0; i < x.length-1; i += 2)
    map.put(x[i], x[i+1]);
  return map;
}

stefan-reich avatar Dec 22 '17 15:12 stefan-reich

Nobody interested in working cursor keys? lol

stefan-reich avatar May 02 '18 07:05 stefan-reich

Here's an additional fix if you use AutoComplete (all of this could be done easier when patching the library itself):

static class NumPadFixingInputMap extends InputMap {
  public void put(KeyStroke keyStroke, Object actionMapKey) {
    super.put(keyStroke, actionMapKey);
    Integer code = keyStrokeMap.get(keyStroke.getKeyCode());
    if (code != null)
      put(KeyStroke.getKeyStroke(code, keyStroke.getModifiers()), actionMapKey);
  }
  
  void replaceOn(JComponent c) {
    InputMap im = c.getInputMap();
    setParent(im.getParent());
    for (KeyStroke key : im.keys())
      put(key, im.get(key));
    c.setInputMap(JComponent.WHEN_FOCUSED, this);
  }
}

new NumPadFixingInputMap().replaceOn(yourSyntaxTextArea);

Both fixes together made the cursors work for me.

stefan-reich avatar May 03 '18 10:05 stefan-reich

Not my project, but I bet a pull-request would be very welcome..!

stolsvik avatar Jan 29 '19 10:01 stolsvik