Solved

Mouse and keyboard

Posted on 1998-12-27
3
280 Views
Last Modified: 2013-11-20
I want to write an application which receives all mouse- and keyboard-messages and decides whether let them through to the destination window or throw it. Any sample-code would be very helpfull.
0
Comment
Question by:joker1
3 Comments
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1326747
Looks like you need to use

SetWindowsHookEx

The SetWindowsHookEx function installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the system.

HHOOK SetWindowsHookEx(
  int idHook,        // type of hook to install
  HOOKPROC lpfn,     // address of hook procedure
  HINSTANCE hMod,    // handle to application instance
  DWORD dwThreadId   // identity of thread to install hook for
);

Consider looking it up in the help file for additional information.  If you have questions, please ask.
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1326748
tresher_shark is absolutely right concerning 'SetWindowsHookEx()'. This is an example on how to implement it:
This is a system wide message hook (see 'SetWindowsHookEx()' and 'WH_GETMESSAGE', the hook has to reside in a DLL)
LRESULT CALLBACK HookProc   (   int     nCode,  // hook
code
                                WPARAM  wParam, // removal flag
                                LPARAM  lParam  // address of structure with message
                            )
{
    PMSG    pmsg    =   ( PMSG) lParam;
    MSG     msg;

    if  (   0   >   nCode   ||  PM_NOREMOVE ==  wParam)
        {
            return  (   CallNextHookEx  (   g_hhk,
                                            nCode,
                                            wParam,
                                            lParam
                                        )
                    );
        }

    if  (   /* ... check whether it's a mouse message */
            PeekMessage (   &msg,
                            pmsg->hwnd,
                            WM_MOUSEFIRST,
                            WM_MOUSELAST,
                            PM_NOREMOVE
                        )
        )
        {
            /* appropriate action, e.g. suppress msg by removing it /*
            PeekMessage (   &msg,
                            pmsg->hwnd,
                            WM_MOUSEFIRST,
                            WM_MOUSELAST,
                            PM_REMOVE
                        );
        }

 
}
0
 

Author Comment

by:joker1
ID: 1326749
I tried this out. First I used WH_KEYBOARD Hook and installed a hook which received only keyboardevents sent to the current application (the hook-procedure isn't located in a dll). This worked fine, but after another application received keyboard-input, the hook didn't work (even if the "hook-application" got the focus back). - Any ideas why?

I also tried WH_KEYBOARD_LL. With this hook I am able to receive keyboardevents of ALL applications (the hook-procedure isn't located in a dll too). This works fine, but only with WinNT.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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
either24  challenge 19 101
NotAlone Challenge 20 84
how to split multiple lines delimiter : 8 109
Making an alias 7 101
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. …
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.

789 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