Link to home
Start Free TrialLog in
Avatar of funnyveryfunny
funnyveryfunny

asked on

global mousehook

I want to write a little java program that listens to all Windows mouse events. What I want is someone to provide me a little code on MFC side to show how this is done.

Particularly, I'm looking for is: what parameters does mouseHook returns that I can pass to my java program through JNI methods. If possible, please provide me with a single mouseEvent method,

e.g. int mouseListener(){} where 0=mouseMoved, 1= mouseDragged etc...

this method will then be part of my C source file.

Thank you.
SOLUTION
Avatar of nonubik
nonubik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of funnyveryfunny
funnyveryfunny

ASKER

thanx nonubik, I'll have look at these links
This what I understand about MouseProc from msd example:

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{

    // This section setup variables equivalent to String in Java to display event message in a Windows
    CHAR szBuf[128];
    CHAR szMsg[16];
    HDC hdc;
    static int c = 0;
    size_t cch;
      size_t * pcch;
      HRESULT hResult;
 
    // Main feature of MouseProc, this if() handles the event that has no interest to mouse hook
    if (nCode < 0)  // do not process the message
        return CallNextHookEx(myhookdata[MOUSE].hhook, nCode,
            wParam, lParam);
 
    // Call an application-defined function that converts a message
    // constant to a string and copies it to a buffer.
   
    // this I think is the interesting part, as IParam indicates which events, what sort of values does IParam return?
    // or nCode is the indicator of events
    LookUpTheMessage((PMSG) lParam, szMsg);

   //.. I think this where I implement my code according to the fired events, i.e this is where I call my java functions to do
   // something
   
    // This section has no interest to me as its purpose is to display a message
    hdc = GetDC(hwndMain);
      hResult = StringCchPrintf(szBuf, 128/sizeof(TCHAR),
        "MOUSE - nCode: %d, msg: %s, x: %d, y: %d, %d times   ",
        nCode, szMsg, LOWORD(lParam), HIWORD(lParam), c++);
      if (FAILED(hResult))
      {
      // TODO: write error handler
      }
      hResult = StringCchLength(szBuf, 128/sizeof(TCHAR), pcch);
      if (FAILED(hResult))
      {
      // TODO: write error handler
      }
    TextOut(hdc, 2, 95, szBuf, *pcch);
    ReleaseDC(hwndMain, hdc);
    return CallNextHookEx(myhookdata[MOUSE].hhook, nCode, wParam,
        lParam);
}

next setWindowsHookEx, I call this function in my own method created by JNI (Java Native Interface)

so code is something like this:

static MouseProc mouseProc;
static HINSTANCE hinstDLL; // This is my DLL, how do I create one?

void myNativeJavaMethod(){
  setWindowsHookEx(WH_MOUSE,mousemouseProc,hinstDLL,0);
}

is this correct what I am doing?

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry guys....my bad.

Thanx for all the helps.