DJL
DJL

Reputation: 166

Can I distinguish touchpad and mouse messages within a low level hook proc? (Win32)

I am working on adding touchpad-specific functionality to a windows desktop application (written in C/C++). Here are my requirements:

  1. The user shall be able to use both a standard windows mouse and touchpad simultaneously.
  2. All touchpad HID events shall be custom-handled by the application as long as it is running. It shall not be able to control the mouse or interact with other windows.
  3. Normal mouse operation shall not be affected when a touchpad is in use.

Through researching the Win32 API, I learned that a low level mouse hook could be used to block touchpad/mouse input from the rest of the system. I got a message queue and simple hook proc working.

static LRESULT CALLBACK lowLevelHIDHookProc
    (
    int                 nCode,
    WPARAM              wParam,
    LPARAM              lParam
    )
{
HHOOK ignored( NULL );

// have to pass message along to next hook
if( nCode < 0 )
    {
    return( CallNextHookEx( ignored, nCode, wParam, lParam ) );
    }
// THIS IS WHAT I CAN'T FIGURE OUT
else if( eventComingFromNormalMouse ) // allow normal mouse events
    {
    return( CallNextHookEx( ignored, nCode, wParam, lParam ) );
    }
// block touchpad stuff here
else
    {
    printf( "********\nTOUCH INTERCEPTED!!!\n********\n" );
    return( -1 );
    } 

What I'm stuck on right now is distinguishing mouse messages coming from touchpads and normal mice. I found a couple open source projects and examples that use HID Raw Input in order to detect and read generic touchpad data, but they are fine with the mouse cursor being disabled while the touchpad is in use. Instead, I need to figure out in this hook proc whether the message is being generated by a touchpad device, and only block in that case, so the mouse can work normally.

I'm looking through the lParam/MSLLHOOKSTRUCT documentation, but am not seeing anything that looks helpful for making this distinction. I'm open to any ideas, including not using low-level hooks at all since they have a performance impact. Maybe there's a way to open a serial connection to a USB touchpad without windows detecting it as an HID device at all?

Upvotes: 2

Views: 1283

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596928

SetWindowsHookEx() hooks, like WH_MOUSE_LL, do not provide any identifying information about the devices that are generating input events. Raw Input hooks can identify devices, but they can't block input events.

So, you will have to use both kinds of hooks and coordinate them - use a Raw Input hook to identify the touchpad events, and use a WH_MOUSE (not WH_MOUSE_LL) hook to block the events.

However, this is not easy to do reliably. See Combining Raw Input and keyboard Hook to selectively block input from multiple keyboards 1 for more info, and gotchas.

1: that article was written for keyboard input, but should be applicable to mouse/touchpad input, too.

Upvotes: 2

Related Questions