Solved

global mousehook

Posted on 2004-08-27
7
278 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:funnyveryfunny
  • 4
7 Comments
 
LVL 16

Assisted Solution

by:nonubik
nonubik earned 50 total points
ID: 11920802
What you need to do is to install a system-wide mouse hook, by calling SetWindowsHookEx(..) with WH_MOUSE as first parameter. Then implemet the MouseProc procedure.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp
http://msdn.microsoft.com/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Hooks/HookReference/HookFunctions/MouseProc.asp

Fore more info on hooks, you can take a look here: http:Q_20989953.html or http:Q_21017098.html
0
 
LVL 4

Author Comment

by:funnyveryfunny
ID: 11921160
thanx nonubik, I'll have look at these links
0
 
LVL 4

Author Comment

by:funnyveryfunny
ID: 11921404
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);
}

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:funnyveryfunny
ID: 11921437
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?

0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 11922230
Not quite - it should me more like the way described in this sample: http://www.codeproject.com/dll/ParkMouse.asp ("Mousey! Roll Over and Park")

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    LRESULT lResult = CallNextHookEx(s_hHook, nCode, wParam, lParam);

    // Functionality goes here...

    return lResult;
}

extern "C" void InstallHook()
{
    if(s_hHook)
        return;

    s_hHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInst, 0);
    if(NULL == s_hHook)
        MessageBox(0, "Hook Failed", "", 0);

}

void myNativeJavaMethodToSetTheHook(){
 InstallHook();
}
0
 
LVL 4

Author Comment

by:funnyveryfunny
ID: 12213891
Sorry guys....my bad.

Thanx for all the helps.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
FizzBuzz challenge 9 76
matchUp  challenge 9 92
mapBully challenge 6 113
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now