Hallo,
hab vor längerer zeit mal nach einer möglichkeit gesucht, aus einem inaktieven fenster heraus tastatureingaben abzufangen, um bei einer besimmten tastenkombi das fenster zu maximieren und den focus drauf zu setzen.
Nun hab ich zwar nix wirklich sinnvolles gefunden, aber da das prog nur auf meinen xp rechner laufen soll, hab ich mich dann entschlossen das beste zu nehmen, was ich gefunden habe:
Java – Global (low level) Keyboard / Mouse Hook – JNI kSquared.de – Blog
Nun lässt sich bei mir der c(++) code nicht kompilieren, obwohl ich eigentlich alles was wichtig war abgändert habe, gibt er komische meldungen aus, bei dennen ich den fehler einfach nicht finde:
abgeänderte header datei der java classe (KeyControll.h)
JKHook.dll (methodennamen angepasst)
da kriege ich dann eine ganze liste mit fehlern:
hab vor längerer zeit mal nach einer möglichkeit gesucht, aus einem inaktieven fenster heraus tastatureingaben abzufangen, um bei einer besimmten tastenkombi das fenster zu maximieren und den focus drauf zu setzen.
Nun hab ich zwar nix wirklich sinnvolles gefunden, aber da das prog nur auf meinen xp rechner laufen soll, hab ich mich dann entschlossen das beste zu nehmen, was ich gefunden habe:
Java – Global (low level) Keyboard / Mouse Hook – JNI kSquared.de – Blog
Nun lässt sich bei mir der c(++) code nicht kompilieren, obwohl ich eigentlich alles was wichtig war abgändert habe, gibt er komische meldungen aus, bei dennen ich den fehler einfach nicht finde:
abgeänderte header datei der java classe (KeyControll.h)
Code:
#include <jni.h>
/* Header for class KeyboardHook */
#ifndef _Included_natives_KeyboardHook
#define _Included_natives_KeyboardHook
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: KeyboardHook
* Method: registerHook
* Signature: (LGlobalEventListener;)V
*/
JNIEXPORT void JNICALL Java_natives_KeyboardHook_registerHook(JNIEnv *,jobject thisObj,jobject listenerObj);
/*
* Class: KeyboardHook
* Method: unregisterHook
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_natives_KeyboardHook_unregisterHook(JNIEnv *env,jobject thisObj);
#ifdef __cplusplus
}
#endif
#endif
JKHook.dll (methodennamen angepasst)
Code:
#include <windows.h>
#include <jni.h>
#include "KeyControll.h"
#ifdef DEBUG
#define DEBUG_PRINT(x) printf x
#else
#define DEBUG_PRINT(x) do {} while (0)
#endif
HINSTANCE hInst = NULL;
JavaVM * jvm = NULL;
DWORD hookThreadId = 0;
jobject keyboardHookObject = NULL;
jobject globalKeyListenerObject = NULL;
jmethodID processKeyMethod = NULL;
//extern "C"
BOOL APIENTRY DllMain(HINSTANCE _hInst,DWORD reason,LPVOID reserved) {
switch(reason) {
case DLL_PROCESS_ATTACH:
DEBUG_PRINT(("NATIVE: DllMain - DLL_PROCESS_ATTACH.n"));
hInst = _hInst;
break;
default:
break;
}
return TRUE;
}
LRESULT CALLBACK LowLevelKeyboardProc(int nCode,WPARAM wParam,LPARAM lParam) {
JNIEnv* env;
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT *)lParam;
if(jvm->AttachCurrentThread((void **)&env, NULL)>=0) {
jboolean transitionState = (jboolean)FALSE;
switch(wParam) {
case WM_KEYDOWN: case WM_SYSKEYDOWN:
transitionState = (jboolean)TRUE;
case WM_KEYUP: case WM_SYSKEYUP:
env->CallVoidMethod(keyboardHookObject,processKeyMethod,transitionState,p->vkCode,globalKeyListenerObject);
break;
default:
break;
}
} else DEBUG_PRINT(("NATIVE: LowLevelKeyboardProc - Error on the attach current thread.n"));
return CallNextHookEx(NULL,nCode,wParam,lParam);
}
JNIEXPORT void JNICALL Java_natives_KeyboardHook_registerHook(JNIEnv * env,jobject obj,jobject _globalKeyListenerObject) {
DEBUG_PRINT(("NATIVE: Java_de_ksquared_system_keyboard_KeyboardHook_registerHook - Hooking started!n"));
HHOOK hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,hInst,0);
globalKeyListenerObject = _globalKeyListenerObject;
if(hookHandle==NULL) {
DEBUG_PRINT(("NATIVE: Java_de_ksquared_system_keyboard_KeyboardHook_registerHook - Hook failed!n"));
return;
} else DEBUG_PRINT(("NATIVE: Java_de_ksquared_system_keyboard_KeyboardHook_registerHook - Hook successfuln"));
keyboardHookObject = env->NewGlobalRef(obj);
jclass cls = env->GetObjectClass(keyboardHookObject);
processKeyMethod = env->GetMethodID(cls,"processKey","(ZILnative/GlobalKeyListener;)V");
env->GetJavaVM(&jvm);
hookThreadId = GetCurrentThreadId();
MSG message;
while(GetMessage(&message,NULL,0,0)) {
TranslateMessage(&message);
DispatchMessage(&message);
}
DEBUG_PRINT(((!UnhookWindowsHookEx(hookHandle))?("NATIVE: Java_de_ksquared_system_keyboard_KeyboardHook_registerHook - Unhook failedn")
:"NATIVE: Java_de_ksquared_system_keyboard_KeyboardHook_registerHook - Unhook successfuln"));
}
JNIEXPORT void JNICALL Java_natives_KeyboardHook_unregisterHook(JNIEnv *env,jobject object) {
if(hookThreadId==0) return;
DEBUG_PRINT(("NATIVE: Java_de_ksquared_system_keyboard_KeyboardHook_unregisterHook - call PostThreadMessage.n"));
PostThreadMessage(hookThreadId,WM_QUIT,0,0L);
}
da kriege ich dann eine ganze liste mit fehlern:
Code:
D:\....\C\KHook\JKHook.c(36): error #2112: Left operand of '->' has incompatible type 'JavaVM *'.
D:\....\C\KHook\JKHook.c(36): error #2068: Expected a function but found 'JavaVM *'.
D:\....\C\KHook\JKHook.c(36): error #2168: Operands of '>=' have incompatible types 'void' and 'int'.
D:\....\C\KHook\JKHook.c(42): error #2112: Left operand of '->' has incompatible type 'JNIEnv *'.
D:\....\C\KHook\JKHook.c(42): error #2068: Expected a function but found 'JNIEnv *'.
D:\....\C\KHook\JKHook.c(62): error #2112: Left operand of '->' has incompatible type 'JNIEnv *'.
D:\....\C\KHook\JKHook.c(62): error #2068: Expected a function but found 'JNIEnv *'.
D:\....\C\KHook\JKHook.c(62): error #2168: Operands of '=' have incompatible types 'jcharArray' and 'void'.
D:\....\C\KHook\JKHook.c(63): error #2112: Left operand of '->' has incompatible type 'JNIEnv *'.
D:\....\C\KHook\JKHook.c(63): error #2068: Expected a function but found 'JNIEnv *'.
D:\....\C\KHook\JKHook.c(63): error #2168: Operands of '=' have incompatible types 'jcharArray' and 'void'.
D:\....\C\KHook\JKHook.c(64): error #2112: Left operand of '->' has incompatible type 'JNIEnv *'.
D:\....\C\KHook\JKHook.c(64): error #2068: Expected a function but found 'JNIEnv *'.
D:\....\C\KHook\JKHook.c(64): error #2168: Operands of '=' have incompatible types 'jmethodID' and 'void'.
D:\....\C\KHook\JKHook.c(66): error #2112: Left operand of '->' has incompatible type 'JNIEnv *'.
D:\....\C\KHook\JKHook.c(66): error #2068: Expected a function but found 'JNIEnv *'.
*** Error code: 1 ***
Zuletzt bearbeitet: