Solved

Mouse and keyboard

Posted on 1998-12-27
3
275 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

6 Experts available now in Live!

Get 1:1 Help Now