This discussion is archived
0 Replies Latest reply: Nov 28, 2011 10:02 AM by hwaite RSS

XGrabKeyboard freezes Swing application

hwaite Newbie
Currently Being Moderated
I'm trying to bypass the window manager and reroute all keyboard input directly to a JFC application. On Linux, this is accomplished via JNI calls to Xlib's 'XGrabKeyboard' or XCB's 'xcb_grab_keyboard'. Unfortunately, capturing keyboard input appears to hang up the application completely. The native code is invoked in a dedicated thread so I can't imagine where a deadlock is occurring. Not only is the UI unresponsive, but the JFrame becomes immobile as well (which is not the case if AWT event queue thread is blocked). Can anyone give me a hint as to why this is happening and how to prevent it?

Java:
(
  new Thread(
    new Runnable() {
      public void run() {
        Test.grabKeyboard();
      }
    }
  )
).run();
C:
JNIEXPORT void JNICALL Java_test_Test_grabKeyboard (JNIEnv* env, jclass cls) {
  Display* dpy = XOpenDisplay(0);
  XGrabKeyboard(
    dpy, DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync, CurrentTime
  );
  XEvent ev;
  for (;;) XNextEvent(dpy, &ev);
  XUngrabKeyboard(dpy, CurrentTime);
  XCloseDisplay(dpy);
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points