0

I am attempting to create a keyboard hook to use in Java. After some research, I realized I will need some things from C++. I don't have much experience with C++, I have only messed with the syntax for a short while. How would I do this in C++? And how would I make the C++ code interact with the Java code? Any help is greatly appreciated.

Stripies
  • 1,267
  • 5
  • 19
  • 29
  • 1
    There's plenty of information already out there that answers this very question, much of it on this site. You're far better off reading up on it, and then coming back if you have a *specific* question. This doesn't require C++ but can be done with C and JNA, but you'll need to know how to code in C if you are to succeed. – Hovercraft Full Of Eels Jan 17 '12 at 03:14
  • For Windows, see [JNA Keyboard hook on Windows](http://stackoverflow.com/questions/3078646/jna-keyboard-hook-in-windows) or [KeyHook.java](https://github.com/twall/jna/blob/master/contrib/w32keyhook/src/KeyHook.java) from JNA contrib – prunge Jan 17 '12 at 04:44

1 Answers1

1

I'm not exactly sure what you want for your C++ code, but here's how you would make it work with Java:

Look into JNI, it'll explain how to execute native code through Java

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html

Essentially, you use a tool to create glue headers that you include in your C++ application. The Java code will automatically call the native library when you call a native function.

Also keep in mind that there's a speed penalty for going from native to java layer.

Prime
  • 4,081
  • 9
  • 47
  • 64
  • JNA is a lot easier to use than JNI for this sort of problem. – Hovercraft Full Of Eels Jan 17 '12 at 03:16
  • I don't have much experience with JNA, it'd be helpful for the asker if you'd post an answer then I think – Prime Jan 17 '12 at 03:18
  • 1
    JNA is a library that is built on JNI and allows Java programs to interact directly with C dll's without having to create header files. I'd post this as an answer if I thought his question were answerable, but it's not. All I can do is make general suggestions for this overly broad question. I think that the OP needs to first do his home-work and due diligence first before he can even consider approaching this topic. – Hovercraft Full Of Eels Jan 17 '12 at 03:21