I have put together a JNA code for installing keyboard hook in Windows (using the JNA examples). The code compiles and everything, and I get the hook installed (I get handle to the hook successfully), also I can uninstall the hook successfully. However, the callback never get called when I press any key on the keyboard. Here is my code (most of it are type definitions got from the JNA examples, go to the "main" directly for my part)
import com.sun.jna.IntegerType;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.FromNativeContext;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Library;
import com.sun.jna.win32.W32APITypeMapper;
import com.sun.jna.win32.W32APIFunctionMapper;
import java.util.Map;
import java.util.HashMap;
public class HelloWorld {
static Map UNICODE_OPTIONS = new HashMap() {
{
put("type-mapper", W32APITypeMapper.UNICODE);
put("function-mapper", W32APIFunctionMapper.UNICODE);
}
};
public static class LONG_PTR extends IntegerType {
public LONG_PTR() { this(0); }
public LONG_PTR(long value) { super(Pointer.SIZE, value); }
}
public static class UINT_PTR extends IntegerType {
public UINT_PTR() { super(Pointer.SIZE); }
public UINT_PTR(long value) { super(Pointer.SIZE, value); }
public Pointer toPointer() { return Pointer.createConstant(longValue()); }
}
public static class ULONG_PTR extends IntegerType {
public ULONG_PTR() { this(0); }
public ULONG_PTR(long value) { super(Pointer.SIZE, value); }
}
public static class LRESULT extends LONG_PTR {
public LRESULT() { this(0); }
public LRESULT(long value) { super(value); }
}
public static class WPARAM extends UINT_PTR {
public WPARAM() { this(0); }
public WPARAM(long value) { super(value); }
}
public static class LPARAM extends LONG_PTR {
public LPARAM() { this(0); }
public LPARAM(long value) { super(value); }
}
public static class KBDLLHOOKSTRUCT extends Structure {
public int vkCode;
public int scanCode;
public int flags;
public int time;
public ULONG_PTR dwExtraInfo;
}
static HANDLE INVALID_HANDLE_VALUE = new HANDLE() {
{ super.setPointer(Pointer.createConstant(-1)); }
public void setPointer(Pointer p) {
throw new UnsupportedOperationException("Immutable reference");
}
};
public static class HANDLE extends PointerType {
public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o))
return INVALID_HANDLE_VALUE;
return o;
}
}
public static class HHOOK extends HANDLE { }
public static class HINSTANCE extends HANDLE { }
public static class HMODULE extends HINSTANCE { }
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32)Native.loadLibrary("user32", User32.class, UNICODE_OPTIONS);
static final int WH_KEYBOARD_LL = 13;
public static interface HOOKPROC extends StdCallCallback {
LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam);
}
HHOOK SetWindowsHookEx(int idHook, HOOKPROC lpfn, HMODULE hMod, int dwThreadId);
LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, LPARAM lParam);
LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, Pointer lParam);
boolean UnhookWindowsHookEx(HHOOK idHook);
}
public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class, UNICODE_OPTIONS);
HMODULE GetModuleHandle(String name);
}
public static HHOOK hHook;
public static User32.HOOKPROC lpfn;
public static volatile boolean quit = false;
public static void main(String[] args) throws Exception {
HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
System.out.println(hMod);
lpfn = new User32.HOOKPROC() {
public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam) {
System.out.println("here");
quit = true;
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod, 0);
System.out.println(hHook);
if(hHook != null)
System.out.println("Keyboard hooked, type anything to quit");
while(!quit) {
Thread.sleep(100);
}
if(User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
I have done keyboard/mouse hooks several times using both C++ and C# in the past. This my first attempt with Java, and I just don't know whether I imported and mapped the library correctly. Any ideas?
Thank you.
It appears you need to call GetMessage or PeekMessage, which is odd - it is not mentioned in the documentation for Hooks or LowLevelKeyboardProc. I don't know enough about this part of the API to guess at the reason.
I just used the example classes:
import com.sun.jna.examples.win32.*;
public class Callback {
public static User32.HHOOK hHook;
public static User32.LowLevelKeyboardProc lpfn;
public static volatile boolean quit = false;
public static void main(String[] args) throws Exception {
W32API.HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
lpfn = new User32.LowLevelKeyboardProc() {
public W32API.LRESULT callback(int nCode, W32API.WPARAM wParam,
User32.KBDLLHOOKSTRUCT lParam) {
System.out.println("here");
quit = true;
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam
.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod,
0);
if (hHook == null)
return;
User32.MSG msg = new User32.MSG();
while (!quit) {
User32.INSTANCE.PeekMessage(msg, null, 0, 0, 0);
Thread.sleep(100);
}
if (User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
A minimalist example:
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HINSTANCE;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser.HOOKPROC;
public class MainTestKeyHook {
public static void main(String[] args) throws Exception {
HOOKPROC hookProc = new HOOKPROC_bg();
HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null);
User32.HHOOK hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, hookProc, hInst, 0);
if (hHook == null)
return;
User32.MSG msg = new User32.MSG();
System.err.println("Please press any key ....");
while (true) {
User32.INSTANCE.GetMessage(msg, null, 0, 0);
}
}
}
class HOOKPROC_bg implements HOOKPROC {
public HOOKPROC_bg() {
}
public LRESULT callback(int nCode, WPARAM wParam, LPARAM lParam) {
System.err.println("callback bbbnhkilhjkibh nCode: " + nCode);
return new LRESULT(0);
}
}
Related
I'm trying to create a class (MoMoTest.class) which extends a generic abstract class (MappingObject.class). Everything looks good, except that JCodeModel doesn't import the narrowed class (MoTest.class), although I created a JClass of it with codeModel.ref:
MappingObject.class:
package test;
public abstract class MappingObject<T> {
protected T dataObject;
public MappingObject( T dataObject ) {
this.dataObject = dataObject;
}
public abstract T getDataObject();
public abstract String getStandardFormat();
}
MoTest.class:
package test;
public class MoTest {
}
MappingObjectCreator.class:
package test;
import com.sun.codemodel.*;
import java.io.File;
import java.io.IOException;
public class MappingObjectCreator {
public JDefinedClass getMappingObject(JCodeModel codeModel, JPackage jPackage, Class<?> clazz) throws JClassAlreadyExistsException {
JClass ref = codeModel.ref(clazz); // Not imported in MoMoTest.class
JDefinedClass definedClass = jPackage._class("Mo" + ref.name());
JClass superClass = codeModel.ref(MappingObject.class).narrow(ref);
definedClass._extends(superClass);
JFieldRef dataObject = JExpr.ref("dataObject");
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
JVar param = constructor.param(ref, ref.name());
constructor.body().invoke("super").arg(param);
JMethod getDataObject = definedClass.method(JMod.PUBLIC, ref, "getDataObject");
getDataObject.annotate(codeModel.ref(Override.class));
getDataObject.body()._return(dataObject);
JMethod getStandardFormat = definedClass.method(JMod.PUBLIC, String.class, "getStandardFormat");
getStandardFormat.annotate(codeModel.ref(Override.class));
getStandardFormat.body()._return(dataObject.invoke("toString"));
return definedClass;
}
public void getMappingObject(Class clazz, String path) throws JClassAlreadyExistsException, IOException {
JCodeModel codeModel = new JCodeModel();
JPackage jPackage = codeModel._package(clazz.getPackage().getName());
getMappingObject(codeModel, jPackage, clazz);
codeModel.build(new File(path));
}
public static void main(String[] args) throws IOException, JClassAlreadyExistsException {
new MappingObjectCreator().getMappingObject(MoTest.class, "src/main/java");
}
}
Result (MoMoTest.class):
package test;
public class MoMoTest
extends MappingObject<test.MoTest>
{
public MoMoTest(test.MoTest MoTest) {
super(MoTest);
}
#Override
public test.MoTest getDataObject() {
return dataObject;
}
#Override
public String getStandardFormat() {
return dataObject.toString();
}
}
So why is Motest.class not imported in MomoTest.class ?
After modifying the parametername of the constructor of the generated class (MoMoTest.class) to first-letter-lower-case it somehow works:
MappingObjectCreator.class:
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
char[] refName = ref.name().toCharArray();
refName[0] = Character.toLowerCase(refName[0]);
JVar param = constructor.param(ref, new String(refName));
constructor.body().invoke("super").arg(param);
Result:
package test;
public class MoMoTest
extends MappingObject<MoTest>
{
public MoMoTest(MoTest moTest) {
super(moTest);
}
#Override
public MoTest getDataObject() {
return dataObject;
}
#Override
public String getStandardFormat() {
return dataObject.toString();
}
}
// original c code
struct callback {
void (*recv_msg_)(const char * msg, int type, unsigned int len);
void (*connected_)(void *cs);
void (*disconnected_)(void *cs);
};
void recv_msg(const char *msg, int type, unsigned int len)
{
// some code
//......
}
void connected(void *s)
{
// some code
//......
}
void disconnected(void *s)
{
// some code
//......
}
struct callback cb;
cb.recv_msg_ = recv_msg;
cb.connected_ = connected;
cb.disconnected = disconnected;
init("127.0.0.1", 5672, &cb);
// C code end
////////////////////////////////////////////////////////////////////////
// THE JNA code
interface RecvMsg extends Callback {
void invoke(String msg, int type, int con_len);
}
interface CbConnected extends Callback {
void invoke(Pointer ctx);
}
interface CbDisconnected extends Callback {
void invoke(Pointer ctx);
}
class RecvMsgImpl implements RecvMsg {
#Override
public void invoke(String msg, int type, int len) {
System.out.println("recv msg: " + msg);
}
}
class CbConnectedImpl implements CbConnected {
#Override
public void invoke(Pointer ctx) {
System.out.println("connected.");
}
}
class CbDisconnectedImpl implements CbDisconnected {
#Override
public void invoke(Pointer ctx) {
System.out.println("diconnected.");
}
}
public class MyCallBack extends Structure {
public RecvMsg recv_msg_;
public CbConnected cb_connected_;
public CbDisconnected cb_disconnected_;
#Override
protected List getFieldOrder() {
return Arrays.asList(new String[] {"recv_msg_", "cb_connected_", "cb_disconnected_"});
}
}
MyCallBack cb = new MyCallBack();
cb.recv_msg_ = new RecvMsgImpl();
cb.cb_connected_ = new CbConnectedImpl();
cb.cb_disconnected_ = new CbDisconnectedImpl();
Xxx xxx = (Xxx) Native.loadLibrary("***", Xxx.class);
xxx.init("127.0.0.1", 5672, cb);
// Java code end
////////////////////////////////////////////////////////////////////
I run the java code, got exception like this:
Exception in thread "main" java.lang.IllegalArgumentException: Structure field "cb_connected_" was declared as interface DLLTest.CbConnected, which is not supported within a Structure
at com.sun.jna.Structure.writeField(Structure.java:808)
at com.sun.jna.Structure.write(Structure.java:718)
at com.sun.jna.Structure.autoWrite(Structure.java:1923)
at com.sun.jna.Function.convertArgument(Function.java:505)
at com.sun.jna.Function.invoke(Function.java:297)
at com.sun.jna.Library$Handler.invoke(Library.java:212)
at com.sun.proxy.$Proxy1.init(Unknown Source)
at DLLTest.Tester.main(Tester.java:55)
Please tell me how to translate it to java with JNA.
This is my test for this with jna library.
I think you have to find some difference with mine.
I made my own code because you didn't show me the full source code.
My environment of development are a mingw64 with msys2 and a eclipse(oxygen) with the java(1.8)
Here are some more details.
Gcc version
Using built-in specs.
COLLECT_GCC=C:\DEV\COMP\msys32\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=C:/DEV/COMP/msys32/mingw64/bin/../lib/gcc/x86_64-w64-
mingw32/6.3.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: .....skip
Thread model: posix
gcc version 6.3.0 (Rev1, Built by MSYS2 project)
Java information
java.class.version::52.0
sun.management.compiler::HotSpot 64-Bit Tiered Compilers
sun.arch.data.model::64
sun.desktop::windows
sun.cpu.isalist::amd64
First, A complete code of a JnaTest class from yours.
package stackoverflow;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Callback;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
public class JnaTest {
public interface JnaInf extends Library {
public void init(String val, int port, MyCallBack cb);
}
public static void main(String[] args) {
MyCallBack cb = new MyCallBack();
cb.recv_msg_ = new RecvMsgImpl();
cb.cb_connected_ = new CbConnectedImpl();
cb.cb_disconnected_ = new CbDisconnectedImpl();
System.load("C:\\DEV\\COMP\\msys32\\home\\stackoverflow\\jna_inf.dll");
JnaInf xxx = (JnaInf) Native.loadLibrary("jna_inf", JnaInf.class);
xxx.init("127.0.0.1", 5672, cb);
}
interface RecvMsg extends Callback {
void invoke(String msg, int type, int con_len);
}
interface CbConnected extends Callback {
void invoke(Pointer ctx);
}
interface CbDisconnected extends Callback {
void invoke(Pointer ctx);
}
static class RecvMsgImpl implements RecvMsg {
#Override
public void invoke(String msg, int type, int len) {
System.out.println("recv msg: " + msg);
System.out.println("type: " + type);
System.out.println("len: " + len);
}
}
static class CbConnectedImpl implements CbConnected {
#Override
public void invoke(Pointer ctx) {
System.out.println("connected.");
}
}
static class CbDisconnectedImpl implements CbDisconnected {
#Override
public void invoke(Pointer ctx) {
System.out.println("diconnected.");
}
}
public static class MyCallBack extends Structure {
public RecvMsg recv_msg_;
public CbConnected cb_connected_;
public CbDisconnected cb_disconnected_;
#Override
protected List getFieldOrder() {
return Arrays.asList(new String[] { "recv_msg_", "cb_connected_", "cb_disconnected_" });
}
}
}
Then, the c source code name, jna_info.c is as follows,
struct callback {
void (*recv_msg_)(const char * msg, int type, unsigned int len);
void (*connected_)(void *cs);
void (*disconnected_)(void *cs);
};
void recv_msg(const char *msg, int type, unsigned int len)
{
// some code
//......
}
void connected(void *s)
{
// some code
//......
return;
}
void disconnected(void *s)
{
// some code
//......
return;
}
void init(const char *msg, int type, struct callback *mycallback)
{
mycallback->recv_msg_("123", 22, 3);
mycallback->connected_("123");
return;
}
Compile the c source code
gcc -c jna_inf.c
gcc -shared -o jna_inf.dll jna_inf.o
The output looks like what i am expecting..
I sincerely hope this helps you.
public class JVideoOCR extends Observable
{
public static final int STATUS_DOCUMENT_REMOVED = 1;
public native boolean init();
public native boolean terminate();
public native boolean startRead();
public native boolean stopRead();
static {
System.loadLibrary("JVideoOCR"); }
IOCRDataHandler dataHandler = null;
public void setOCRDataHandler(IOCRDataHandler handler)
{
dataHandler = handler;
}
public void MRZDataCallback(MRZData data)
{
if (dataHandler != null)
dataHandler.handleOCRData(data);
}
private void returnStatus(int status)
{
switch (status)
{
case STATUS_DOCUMENT_REMOVED:
setChanged() ;
notifyObservers(new Integer(2)) ;
break;
}
}
}
This code is working fine whent plced that java file to default package but if i am shifting this java file to package it's not working.
I have a Java class called Term holding polynomials like below
public Term(int c, int e) throws NegativeExponent {
if (e < 0) throw new NegativeExponent();
coef = c;
expo = (coef == 0) ? 1 : e;
}
I also have an equals method in the same class like below
#Override
public boolean equals(Object obj) {
}
I am stuck with how to code how to compare these 2 Term objects
Within my JUnit test file I am using the test below to try and test the equals method
import static org.junit.Assert.*;
import org.junit.Test;
public class ConEqTest
{
private int min = Integer.MIN_VALUE;
private int max = Integer.MAX_VALUE;
#Test
public void eq01() throws TError { assertTrue(new Term(-10,0).equals(new Term(-10,0))); }
#Test
public void eq02() throws TError { assertTrue(new Term(0,0).equals(new Term(0,2))); }
What's wrong with
#Override
public boolean equals(Object obj) {
if (! (obj instanceof Term))
return false;
Term t = (Term)obj;
return coef == t.coef && expo == t.expo;
}
import static org.junit.Assert.*;
import org.junit.*;
#SuppressWarnings("serial") class NegativeExponentException extends Exception {}
class Term {
#Override public int hashCode() {
final int prime=31;
int result=1;
result=prime*result+coefficient;
result=prime*result+exponent;
return result;
}
#Override public boolean equals(Object obj) {
if(this==obj)
return true;
if(obj==null)
return false;
if(getClass()!=obj.getClass())
return false;
Term other=(Term)obj;
if(coefficient!=other.coefficient)
return false;
if(exponent!=other.exponent)
return false;
return true;
}
public Term(int c,int e) throws NegativeExponentException {
if(e<0)
throw new NegativeExponentException();
coefficient=c;
exponent=(coefficient==0)?1:e;
}
int coefficient,exponent;
}
public class So13408797TestCase {
#Test public void eq01() throws Exception {
assertTrue(new Term(-10,0).equals(new Term(-10,0)));
}
#Test public void eq02() throws Exception {
assertTrue(new Term(0,0).equals(new Term(0,2)));
}
private int min=Integer.MIN_VALUE;
private int max=Integer.MAX_VALUE;
}
I have put together a JNA code for installing keyboard hook in Windows (using the JNA examples). The code compiles and everything, and I get the hook installed (I get handle to the hook successfully), also I can uninstall the hook successfully. However, the callback never get called when I press any key on the keyboard. Here is my code (most of it are type definitions got from the JNA examples, go to the "main" directly for my part)
import com.sun.jna.IntegerType;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.FromNativeContext;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Library;
import com.sun.jna.win32.W32APITypeMapper;
import com.sun.jna.win32.W32APIFunctionMapper;
import java.util.Map;
import java.util.HashMap;
public class HelloWorld {
static Map UNICODE_OPTIONS = new HashMap() {
{
put("type-mapper", W32APITypeMapper.UNICODE);
put("function-mapper", W32APIFunctionMapper.UNICODE);
}
};
public static class LONG_PTR extends IntegerType {
public LONG_PTR() { this(0); }
public LONG_PTR(long value) { super(Pointer.SIZE, value); }
}
public static class UINT_PTR extends IntegerType {
public UINT_PTR() { super(Pointer.SIZE); }
public UINT_PTR(long value) { super(Pointer.SIZE, value); }
public Pointer toPointer() { return Pointer.createConstant(longValue()); }
}
public static class ULONG_PTR extends IntegerType {
public ULONG_PTR() { this(0); }
public ULONG_PTR(long value) { super(Pointer.SIZE, value); }
}
public static class LRESULT extends LONG_PTR {
public LRESULT() { this(0); }
public LRESULT(long value) { super(value); }
}
public static class WPARAM extends UINT_PTR {
public WPARAM() { this(0); }
public WPARAM(long value) { super(value); }
}
public static class LPARAM extends LONG_PTR {
public LPARAM() { this(0); }
public LPARAM(long value) { super(value); }
}
public static class KBDLLHOOKSTRUCT extends Structure {
public int vkCode;
public int scanCode;
public int flags;
public int time;
public ULONG_PTR dwExtraInfo;
}
static HANDLE INVALID_HANDLE_VALUE = new HANDLE() {
{ super.setPointer(Pointer.createConstant(-1)); }
public void setPointer(Pointer p) {
throw new UnsupportedOperationException("Immutable reference");
}
};
public static class HANDLE extends PointerType {
public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o))
return INVALID_HANDLE_VALUE;
return o;
}
}
public static class HHOOK extends HANDLE { }
public static class HINSTANCE extends HANDLE { }
public static class HMODULE extends HINSTANCE { }
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32)Native.loadLibrary("user32", User32.class, UNICODE_OPTIONS);
static final int WH_KEYBOARD_LL = 13;
public static interface HOOKPROC extends StdCallCallback {
LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam);
}
HHOOK SetWindowsHookEx(int idHook, HOOKPROC lpfn, HMODULE hMod, int dwThreadId);
LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, LPARAM lParam);
LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, Pointer lParam);
boolean UnhookWindowsHookEx(HHOOK idHook);
}
public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class, UNICODE_OPTIONS);
HMODULE GetModuleHandle(String name);
}
public static HHOOK hHook;
public static User32.HOOKPROC lpfn;
public static volatile boolean quit = false;
public static void main(String[] args) throws Exception {
HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
System.out.println(hMod);
lpfn = new User32.HOOKPROC() {
public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam) {
System.out.println("here");
quit = true;
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod, 0);
System.out.println(hHook);
if(hHook != null)
System.out.println("Keyboard hooked, type anything to quit");
while(!quit) {
Thread.sleep(100);
}
if(User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
I have done keyboard/mouse hooks several times using both C++ and C# in the past. This my first attempt with Java, and I just don't know whether I imported and mapped the library correctly. Any ideas?
Thank you.
It appears you need to call GetMessage or PeekMessage, which is odd - it is not mentioned in the documentation for Hooks or LowLevelKeyboardProc. I don't know enough about this part of the API to guess at the reason.
I just used the example classes:
import com.sun.jna.examples.win32.*;
public class Callback {
public static User32.HHOOK hHook;
public static User32.LowLevelKeyboardProc lpfn;
public static volatile boolean quit = false;
public static void main(String[] args) throws Exception {
W32API.HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
lpfn = new User32.LowLevelKeyboardProc() {
public W32API.LRESULT callback(int nCode, W32API.WPARAM wParam,
User32.KBDLLHOOKSTRUCT lParam) {
System.out.println("here");
quit = true;
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam
.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod,
0);
if (hHook == null)
return;
User32.MSG msg = new User32.MSG();
while (!quit) {
User32.INSTANCE.PeekMessage(msg, null, 0, 0, 0);
Thread.sleep(100);
}
if (User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
A minimalist example:
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HINSTANCE;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser.HOOKPROC;
public class MainTestKeyHook {
public static void main(String[] args) throws Exception {
HOOKPROC hookProc = new HOOKPROC_bg();
HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null);
User32.HHOOK hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, hookProc, hInst, 0);
if (hHook == null)
return;
User32.MSG msg = new User32.MSG();
System.err.println("Please press any key ....");
while (true) {
User32.INSTANCE.GetMessage(msg, null, 0, 0);
}
}
}
class HOOKPROC_bg implements HOOKPROC {
public HOOKPROC_bg() {
}
public LRESULT callback(int nCode, WPARAM wParam, LPARAM lParam) {
System.err.println("callback bbbnhkilhjkibh nCode: " + nCode);
return new LRESULT(0);
}
}