Solved

global mousehook

Posted on 2004-08-27
7
281 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

13 Experts available now in Live!

Get 1:1 Help Now