SetWindowsHookEx not notifing me on key pressed, using JNI and C++ dll
I'm trying to develop a simple program that intercepts braille key
strokes and translates them into normal letters. The goal is to create
a "braille keyboard emulator", is one can call it that.
I'm using java and a c++ dll making them comunicate with JNI.
The java program loads the dll in a static block in the Main class with
the 'System.loadLibrary' method the dll is loaded and the 'DllMain'
funtion is called.
What I don't know is if the 'SetWindowsHookEx' is called or not. I can
make the java program send messages to the C++ dll library and I can
make the dll send back messages to the java program but I'm never
notified about keys being pressed. I have no idea about what is going
wrong and have had no success searching for similar problems on the
Here is the code for the C++ dll:
Library that allows the hooking of the keyboard so that
the Java componet of the system is able to process the key codes
and send the right information to the user desktop aplication.
It's responsible for the 'grabing' of the keyboard and is the bridge
the java part and the aplication waiting for the key.
LRESULT CALLBACK LowLevelKeyboardProc(int, WPARAM, LPARAM);
HHOOK hhkLowLevelKybd = NULL;
HINSTANCE hInst - Library instance handle.
DWORD reason - Reason this function is being called.
LPVOID reserved - Not used.
extern "C" BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID
printf("Metodo dllmain invocado\n");
hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL,
LowLevelKeyboardProc, hInst, 0);
if(hhkLowLevelKybd == NULL)
printf("hook at process attach failed!");
printf("hook feito no process attach\n");
printf("unhook feito no process detach\n");
/*Are process and thread called allways or is only one of them
/* Returns TRUE on success, FALSE on failure */
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM
printf("callback de teclado invocada!\n");
KBDLLHOOKSTRUCT *p = (KBDLLHOOKSTRUCT *) lParam;
printf("Pressionada a tecla\n");
jclass cls = (java)->GetObjectClass(parser);
jmethodID mid = (java)->GetMethodID(cls, "processKey", "(I)V");
(java)->CallVoidMethod(parser, mid, p->vkCode);
//return the control to the sytem!
return CallNextHookEx(NULL, nCode, wParam, lParam);
Function called by the Java part in order to return the processed key.
JNIEXPORT void JNICALL Java_bkey_KeyParser_parsedKey (JNIEnv *env,
jobject object, jint key)
if(key == VK_L)
printf("Java enviou a tecla L\n");
printf("Codigo da tecla enviada por Java diferente da tecla L\n");
Function called by the Java part to register itself as the owner and
caller of this library.
This is used to make the library able to call java methods and send the
intercepted keys to be processed.
JNIEXPORT void JNICALL Java_bkey_KeyParser_registerOwner (JNIEnv *env,
printf("C a registar o dono\n");
parser = object;
java = env;