Bom após muita busca cheguei ao seguinte resultado parcial:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Hook{
public native void InitJavaInterface (String dirpath, String name, String kname, String mname);
public native void StopJavaInterface ();
public native int SetKeyboardHook (int value);
public native int SetMouseHook (int value);
static{
System.loadLibrary("myhooks");
}
public static void main(String [] args){
Hook hk = new Hook();
hk.InitJavaInterface("-Djava.class.path=c:\\temp\\testhook\\","Test","kbd","mouse");
/**int t = hk.SetMouseHook(1);
System.out.println(t +"\n");**/
int t = hk.SetKeyboardHook (1);
System.out.println(t +"\n");
t = hk.SetMouseHook(1);
System.out.println(t +"\n");
}
}
myhooks.cpp
// myhooks.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "myhooks.h"
#include <stdlib.h>
#include <stdio.h>
#include <jni.h>
#pragma data_seg(".SharedData")
#pragma data_seg( )
HHOOK hLLKeyboardHook = NULL;
HHOOK hLLMouseHook = NULL;
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *g_env; /* pointer to native method interface */
jclass cls ;
jmethodID midKBD = 0;
jmethodID midMOUSE = 0;
DWORD hookThreadId = 0;
#ifdef _MANAGED
#pragma managed(push, off)
#endif
kbcallback kbfun=0;
mscallback msfun=0;
LRESULT CALLBACK LowLevelKeyboardFilterProc(int nCode, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK LowLevelMouseFilterProc(int nCode, WPARAM wParam, LPARAM lParam);
HKEY hModuleKey = NULL; // Key used to save settings
HINSTANCE hInstance = NULL; // This instance of the DLL
void MsgLoop() {
MSG message;
while (GetMessage(&message, NULL, 0, 0)) {
TranslateMessage(&message);
DispatchMessage(&message);
}
}
DllExport BOOL SetMouseFilterHook(BOOL activate)
{
if (activate)
{
if (hLLMouseHook == NULL)
{
// Start up the hook...
hLLMouseHook = SetWindowsHookEx(
WH_MOUSE_LL, // Hook in before msg reaches app
(HOOKPROC) LowLevelMouseFilterProc, // Hook procedure
hInstance, // This DLL instance
0L // Hook in to all apps
);
if (hLLMouseHook == NULL)
return FALSE;
}
return TRUE;
} else {
if (hLLMouseHook != NULL)
{
// Stop the hook...
if (!UnhookWindowsHookEx(hLLMouseHook))
return FALSE;
hLLMouseHook = NULL;
}
return TRUE;
}
}
DllExport BOOL SetKbdFilterHook(BOOL activate)
{
printf("callled kbd hook set\n");
if (activate)
{
printf("setting kbd hook\n");
if (hLLKeyboardHook == NULL)
{
// Start up the hook...
printf("kbd hook is null now setting\n ");
hLLKeyboardHook = SetWindowsHookEx(
WH_KEYBOARD_LL, // Hook in before msg reaches app
(HOOKPROC) LowLevelKeyboardFilterProc, // Hook procedure
hInstance, // This DLL instance
0L // Hook in to all apps
);
printf("setting kbd hook now hook:%x\n", hLLKeyboardHook);
if (hLLKeyboardHook == NULL){
printf("could not set kbd hook now hook:%x\n", hLLKeyboardHook);
return FALSE;
}
}
return TRUE;
} else {
if (hLLKeyboardHook != NULL)
{
// Stop the hook...
printf("stopping kbd hook now hook:%x\n", hLLKeyboardHook);
if (!UnhookWindowsHookEx(hLLKeyboardHook)){
return FALSE;
}
hLLKeyboardHook = NULL;
}
return TRUE;
}
}
DllExport BOOL SetKbCallBack(kbcallback bk){
if (kbfun==NULL)
kbfun = bk;
else{
kbfun = bk;
return FALSE;
}
return TRUE;
}
DllExport BOOL SetMsCallBack(mscallback bk){
if (msfun==NULL)
msfun = bk;
else{
msfun = bk;
return FALSE;
}
return TRUE;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hInstance = (HINSTANCE)hModule;
return TRUE;
case DLL_PROCESS_DETACH:
return TRUE;
default:
return TRUE;
}
}
LRESULT CALLBACK LowLevelKeyboardFilterProc(int nCode, WPARAM wParam, LPARAM lParam)
{
int ret =0;
printf("got a call now printing\n");
// Are we expected to handle this callback?
if (nCode == HC_ACTION)
{
// Is this keyboard event "real" or "injected"
// i.e. hardware or software-produced?werwerwer
KBDLLHOOKSTRUCT *hookStruct = (KBDLLHOOKSTRUCT*)lParam;
if (!(hookStruct->flags & LLKHF_INJECTED)) {
if(midKBD!=0){
printf("got a call now calling for printing\n");
ret = g_env->CallStaticIntMethod(cls, midKBD,hookStruct->scanCode, hookStruct->vkCode,hookStruct->flags,hookStruct->time);
}
if(kbfun)
ret = kbfun(hookStruct);
if(ret>0)
return ret;
}
}
// Otherwise, pass on the message
return CallNextHookEx(hLLKeyboardHook, nCode, wParam, lParam);
}
// Hook procedure for LowLevel Mouse filtering
LRESULT CALLBACK LowLevelMouseFilterProc(int nCode, WPARAM wParam, LPARAM lParam)
{
int ret = 0 ;
// Are we expected to handle this callback?
if (nCode == HC_ACTION)
{
// Is this mouse event "real" or "injected"
// i.e. hardware or software-produced?
MSLLHOOKSTRUCT *hookStruct = (MSLLHOOKSTRUCT*)lParam;
if (!(hookStruct->flags & LLMHF_INJECTED)) {
if(midMOUSE!=0){
ret = g_env->CallStaticIntMethod(cls, midMOUSE,hookStruct->flags,hookStruct->mouseData,hookStruct->pt.x,hookStruct->pt.y,hookStruct->time);
}
if(msfun)
ret = msfun(hookStruct);
}
if(ret>0)
return ret;
}
// Otherwise, pass on the message
return CallNextHookEx(hLLMouseHook, nCode, wParam, lParam);
}
/*
"-Djava.class.path=C:\\Documents and Settings\\HP_Administrator\\workspace\\ii\\classes\\";
*/
DllExport void InitJavaInterface(const char *classPathWithOption,const char* className,const char *kbdMethod, const char* mouseMethod){
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
char buffer[256]={0};
strcpy(buffer,classPathWithOption);
options[0].optionString = buffer;
vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
JNI_CreateJavaVM(&jvm,(void**) &g_env, &vm_args);
delete options;
printf("jvm created: %x \n",jvm);
printf("env created: %x \n",g_env);
/* invoke the Main.test method using the JNI */
printf("Found the class :%x \n",cls);
cls = g_env->FindClass(className);
printf("Found the class :%x \n",cls);
midKBD = g_env->GetStaticMethodID(cls, kbdMethod, "(IIII)I");
printf("Found the kbd method:%d \n",midKBD);
midMOUSE = g_env->GetStaticMethodID(cls, mouseMethod,"(IIIII)I");
printf("Found the mouse method: %d \n",midMOUSE);
printf("found class and method %d, %d \n",midKBD, midMOUSE);
}
DllExport void StopJavaInterface(){
midKBD = 0;
midMOUSE = 0;
if(jvm)
jvm->DestroyJavaVM();
jvm = 0;
cls = 0;
}
JNIEXPORT void JNICALL Java_Hook_InitJavaInterface (JNIEnv * env2, jobject obj,jstring dirpath, jstring className,jstring kbdMethod, jstring mouseMethod){
const char * strClassName = g_env->GetStringUTFChars(className, 0);
const char * strKbdMethod = g_env->GetStringUTFChars(kbdMethod, 0);
const char * strMouseMethod = g_env->GetStringUTFChars(mouseMethod, 0);
printf("Found the class :%x \n",cls);
cls = g_env->FindClass(strClassName);
printf("Found the class :%x \n",cls);
midKBD = g_env->GetStaticMethodID(cls, strKbdMethod, "(IIII)I");
printf("Found the kbd method:%d \n",midKBD);
midMOUSE = g_env->GetStaticMethodID(cls, strMouseMethod,"(IIIII)I");
printf("Found the mouse method: %d \n",midMOUSE);
printf("found class and method %d, %d \n",midKBD, midMOUSE);
g_env->ReleaseStringUTFChars(className, strClassName);
g_env->ReleaseStringUTFChars(kbdMethod, strKbdMethod);
g_env->ReleaseStringUTFChars(mouseMethod, strMouseMethod);
hookThreadId = GetCurrentThreadId();
MsgLoop();
}
JNIEXPORT void JNICALL Java_Hook_StopJavaInterface (JNIEnv * env2, jobject obj){
PostThreadMessage(hookThreadId, WM_QUIT, 0, 0L);
midKBD = 0;
midMOUSE = 0;
JavaVM *jvm;
g_env->GetJavaVM(&jvm);
jvm->DetachCurrentThread();
cls = 0;
}
JNIEXPORT int JNICALL Java_Hook_SetKeyboardHook (JNIEnv * env2, jobject obj,jint setOrReset){
return SetKbdFilterHook(setOrReset);
}
JNIEXPORT int JNICALL Java_Hook_SetMouseHook (JNIEnv * env2, jobject obj,jint setOrReset){
return SetMouseFilterHook(setOrReset);
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
jint res;
/* cache JNIEnv2 interface pointer in global variable */
res = vm->GetEnv((void **)&g_env, JNI_VERSION_1_4);
printf("got the env : %x \n",&g_env);
if (res < 0) {
return res;
}
return JNI_VERSION_1_4; /* the required JNI version */
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
e
myhooks.h
#if !defined(_MYHOOKS_DLL_)
#define _MYHOOKS_DLL_
#include <windows.h>
#include <jni.h>
#define DllImport __declspec(dllimport)
#define DllExport __declspec(dllexport)
typedef int (*kbcallback)(KBDLLHOOKSTRUCT *hookStruct);
typedef int (*mscallback)(MSLLHOOKSTRUCT *hookStruct );
extern "C"
{
DllExport BOOL SetKbCallBack(kbcallback bk);
DllExport BOOL SetMsCallBack(mscallback bk);
DllExport void InitJavaInterface(const char *classPathWithOption,const char* className,const char *kbdMethod, const char* mouseMethod);
DllExport BOOL SetMouseFilterHook(BOOL activate);
DllExport BOOL SetKbdFilterHook(BOOL activate);
DllExport void StopJavaInterface();
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved);
JNIEXPORT void JNICALL Java_Hook_InitJavaInterface (JNIEnv * env, jobject obj,jstring dirpath, jstring className,jstring kbdMethod, jstring mousemethod);
JNIEXPORT void JNICALL Java_Hook_StopJavaInterface (JNIEnv * env, jobject obj);
JNIEXPORT int JNICALL Java_Hook_SetKeyboardHook (JNIEnv * env, jobject obj,jint setOrReset);
JNIEXPORT int JNICALL Java_Hook_SetMouseHook (JNIEnv * env, jobject obj,jint setOrReset);
}
#endif
Blz, supimpa, maravilha, mas não to entendendo uma coisa... o código funfa blz, só que na hora que mais preciso, que é o evento CLICK do mouse, ele não diferencia... ou seja, tanto faz, Mover, CLicar, ou qq outro evento, o flag de retorno sempre é zero...
Será que a partir disso, alguém consegue achar o erro?
Valeu galera!