Solved

global mousehook

Posted on 2004-08-27
7
273 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 66
viewing source code from eclipse 13 74
matchUp  challenge 9 72
mapBully challenge 6 89
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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