Global Key Hook ist nicht global

Andreas_0815

Mitglied
Hallo Community,

Zur Zeit schreibe ich in Java ein Film-Vorführungs-Programm, das über einen NumPad bedient wird. Dabei sollte ich auch auf den nächsten Film wechseln können, wenn gerade ein Film abgespielt wird. Da diese Filme im Win Media Player abgespielt werden, hat mein Programm aber leider keinen Fokus und kann somit nicht auf Tasteneingaben hören.
Da ich in Java keinen global key listener erstellen kann, will ich nun über JNI mit einer C++ DLL kommunizieren. Die Kommunikation über JNI funktioniert auch problemlos.
Aber ich schaffe es nicht einen globalen Key Hook zu installieren. Mein Hook funktioniert immer nur, wenn mein Programm den Fokus hat.
Zunächst möchte ich auch nur einfach den wParam-Wert in der Console ausgeben. Den Rest schaff ich dann sicherlich selbst.
(Nebenbei, ich will kein .Net verwenden!)

Hier hab ich meine Klassen:
Java-Code
Code:
package key;

public class KeyDllConnection {
	
	public static native void setGlobalKeyHook(String functionName);
	
	public static native void removeKeyHook();
	
	static{
		System.loadLibrary("GlobalKeyListener");
	}
}

C++ File: HookConnectionDLL
Code:
// hook.cpp : Defines the entry point for the DLL application.
//
#include "key_KeyDllConnection.h"
// hook.cpp : Defines the entry point for the DLL application.
//
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include "KeyHook.h"
#include <windows.h>


// Globale Variablen 
HHOOK keyHook SHARED = NULL; // Handle unseres Hooks (als "shared" Deklariert) 
HINSTANCE g_hInst SHARED = NULL;     // Handle der DLL selbst 


extern "C" BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved){
	cout<<"HALLO"<<endl;
	g_hInst = hInst; 
	return TRUE;
}

BOOL InstallHook() 
{ 
	if(keyHook != NULL) 
		return TRUE; 
	keyHook = SetWindowsHookEx(WH_KEYBOARD, KeyAction, g_hInst, 0); 
	if(keyHook == NULL) 
		return FALSE; 
	return TRUE; 
}

BOOL UninstallHook() 
{ 
	if(keyHook != NULL) 
	{ 
		UnhookWindowsHookEx(keyHook); 
		keyHook = NULL; 
	} 
	return TRUE; 
}

LRESULT CALLBACK KeyAction(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
	if (nCode < 0) 
		return CallNextHookEx(keyHook, nCode, wParam, lParam); 
	if(nCode == HC_ACTION) 
	{
		cout<<wParam<<endl;
		if ( (wParam == WM_LBUTTONDOWN)||(wParam == WM_NCLBUTTONDOWN) ) 
		{ 
			MOUSEHOOKSTRUCT *mhs = (MOUSEHOOKSTRUCT*)lParam; 
			HWND caller = FindWindow("Pipette", NULL); 
			if(caller != NULL) 
				PostMessage(caller, WM_USER+123, 0, MAKELPARAM(mhs->pt.x, mhs->pt.y)); 
		} 
	} 
	return CallNextHookEx(keyHook, nCode, wParam, lParam); 
}

//
// hook setup function
//
JNIEXPORT void JNICALL Java_key_KeyDllConnection_setGlobalKeyHook(JNIEnv *env, jclass obj, jstring methodName)
{
	if(InstallHook()){
		cout<<"Successfully installed hook"<<endl;
	}else{
		cout<<"Wasn't able to install hook"<<endl;
	}
	
}

//
// hook remove function
//
JNIEXPORT void JNICALL Java_key_KeyDllConnection_removeKeyHook(JNIEnv *env, jclass obj)
{
	if(UninstallHook()){
		cout<<"Successfully uninstalled hook"<<endl;
	}
}

C++ File: KeyHook.h
Code:
#include <windows.h> 

#define SHARED __attribute__((section(".shr"), shared)) 

BOOL InstallHook(); 
BOOL UninstallHook(); 
LRESULT CALLBACK KeyAction(int nCode, WPARAM wParam, LPARAM lParam);

C++ File: key_KeyDllConnection (von javah erstellte Datei)
Code:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class key_KeyDllConnection */

#ifndef _Included_key_KeyDllConnection
#define _Included_key_KeyDllConnection
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     key_KeyDllConnection
 * Method:    setGlobalKeyHook
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_key_KeyDllConnection_setGlobalKeyHook
  (JNIEnv *, jclass, jstring);

/*
 * Class:     key_KeyDllConnection
 * Method:    removeKeyHook
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_key_KeyDllConnection_removeKeyHook
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

Da ich in c++ nicht sehr viel Ahnung habe, ist der Code aus Beispielen zusammenkopiert... wobei ich nirgends ein (bei mir) funktionierendes Beispiel fand. Auch die bisherigen Beiträge im Forum haben mir nicht helfen können...
Ich nutze Eclipse mit MinGW um die C++ Files zu kompilern.

Schon mal im voraus vielen Dank!
Andreas
 
Zuletzt bearbeitet:
Hallo,

ich hab leider noch immer keine Lösung gefunden... kennt sich niemand damit aus? Oder habe ich die Frage so kompliziert formuliert, dass es keiner kapiert

für Antworten wäre ich sehr dankbar!
Gruß Andreas
 
Zurück