Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

global mousehook

Posted on 2004-08-27
7
286 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 126
Expand macro to ask for filename column 8 37
abstract class with all non abstract mentods 6 79
unix example issues 18 98
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.

808 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