Solved

Mouse and keyboard

Posted on 1998-12-27
3
277 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 62
deburging in oracle form 12 87
difference between String.subString() and String.subSequence() 6 166
only14 challenge 19 68
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

932 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

12 Experts available now in Live!

Get 1:1 Help Now