0

I want to grab the all global key events on linux using JNA same as window hooks. following is the code:

package pack.tracker.services;

//import sun.awt.X11.XEvent;

//import com.jscape.inet.ssh.transport.KeyCreator;
import com.sun.jna.examples.unix.X11;
import com.sun.jna.examples.unix.X11.Display;
import com.sun.jna.examples.unix.X11.Window;
import com.sun.jna.examples.unix.X11.XEvent;

public class JNATest {
    X11 x = X11.INSTANCE;
    Window window;
    Display display = x.XOpenDisplay("0");
    XEvent ev = new XEvent();

    public JNATest() {
        System.out.println("hi");
        // TODO Auto-generated constructor stub
        String keyString = "F3" ;
        Display display = x.XOpenDisplay(null);
        XEvent ev = new XEvent();
        window = x.XDefaultRootWindow(display);


        for(;;){
            x.XNextEvent(display, ev);
            if(ev.type == X11.KeyPress){
                System.out.println("KeyPressed");

            }
            else{
                System.out.println("ok");
            }
        }
    }

    public static void main(String s[]){
        new JNATest();
    }
}

I donot know whats going wrong. I am unable to use XGrabKeyboard. Please help me out.

Thanks and regards, Vivek Birdi

4

1 回答 1

0

JNA 中尚未实现 XGrabKeys 方法。您将需要扩展 X11 库并定义方法,如下所示:

import com.sun.jna.*;
import com.sun.jna.platform.unix.X11;
import com.sun.jna.platform.unix.X11.*;

public interface Xlib extends X11 {

    int XGrabKey(Display display, int keycode, NativeLong modifiers, Window grab_window, boolean owner_events, int pointer_mode, int keyboard_mode);
      /*
      Display *display;
      int keycode;
      unsigned int modifiers;
      Window grab_window;
      Bool owner_events;
      int pointer_mode, keyboard_mode;
      */
}

查看此站点以了解 Xlib 结构和方法。如果您仍然无法在 JNA 中实现,请查看 (http://sourceforge.net/projects/jxgrabkey/) 上的 JXGrabkey。

于 2010-11-21T10:22:41.690 回答