Vyrobil jsem nějaký hack. JNI wrapper je už v knihovně ve formě XlibWrapper, ale to je non-public API.
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) throws Exception {
Program prog = new Program();
SwingUtilities.invokeAndWait(prog);
}
public static class Program implements Runnable {
public Program() {}
@Override
public void run() {
JFrame myFrame = new JFrame("Main frame");
XHack.SetDetectableAutoRepeat(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(750, 500);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new Dispatcher());
}
}
public static class Dispatcher implements KeyEventDispatcher {
public Dispatcher() {}
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_RELEASED)
System.out.println("akce");
return false;
}
}
}
import java.lang.reflect.Method;
import java.lang.Class;
public class XHack {
public static void SetDetectableAutoRepeat(boolean enabled) {
try {
Class<?> xToolkit = Class.forName("sun.awt.X11.XToolkit");
Class<?> xWrapper = Class.forName("sun.awt.X11.XlibWrapper");
Method getDisplay = xToolkit.getDeclaredMethod("getDisplay");
Method setRepeat = xWrapper.getDeclaredMethod("XkbSetDetectableAutoRepeat", Long.TYPE, Boolean.TYPE);
getDisplay.setAccessible(true);
setRepeat .setAccessible(true);
long display = (long) getDisplay.invoke(null);
setRepeat.invoke(null, display, enabled);
} catch (Exception e) {
e.printStackTrace();
}
}
}
U mě to ale nemá žádný efekt, protože se to chová správně i bez toho.