question

SaiTeja-0091 avatar image
0 Votes"
SaiTeja-0091 asked SaiTeja-0091 commented

find Button click event is from Mouse or touchoad in WPF

Hi,
I am working on app. which need to verify external mouse is working on not. i handled click events, these click events are working for both Touchpad and external mouse as-well.

 private void MouseLeftClickHandler(object sender, MouseButtonEventArgs e)
         {
             if( CONDITION ){

                           print("Mouse is clicked")

                   }
              else{

                         print("Touch pad on laptop is clicked")

              }
    
        }

I want to differential input event is touchpad on laptop or a mose connected externally.
What is the condition i need to keep.



windows-wpf
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe handle the TouchDown event separately?


0 Votes 0 ·

touch down event is for touchScreen it seems.

private void Button_PreviewTouchDown(object sender, TouchEventArgs e)
{
MessageBox.Show("Touchscreen was used.");
}


My Aim is to only check External mouse events and trackpad events saparately.

0 Votes 0 ·

Hi,@ SaiTeja-0091. You could try to register to receive input from the Touchpad, the Mouse, or both devices using the RegisterRawInputDevices function(Row Input). For more information, you can also refer to here.


0 Votes 0 ·

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered SaiTeja-0091 commented

I have no Touchpad to test, but this test with the mouse works for me with GetCurrentInputMessageSource :

 [DllImport("User32.dll", SetLastError = true)]
 private static extern bool GetCurrentInputMessageSource(out INPUT_MESSAGE_SOURCE inputMessageSource);
    
 [StructLayout(LayoutKind.Sequential)]
 public struct INPUT_MESSAGE_SOURCE
 {
     public INPUT_MESSAGE_DEVICE_TYPE deviceType;
     public INPUT_MESSAGE_ORIGIN_ID originId;
 }
    
 public enum INPUT_MESSAGE_DEVICE_TYPE
 {
     IMDT_UNAVAILABLE = 0x00000000,      // not specified
     IMDT_KEYBOARD = 0x00000001,      // from keyboard
     IMDT_MOUSE = 0x00000002,      // from mouse
     IMDT_TOUCH = 0x00000004,      // from touch
     IMDT_PEN = 0x00000008,      // from pen
     IMDT_TOUCHPAD    = 0x00000010,      // from touchpad
 }
    
 public enum INPUT_MESSAGE_ORIGIN_ID
 {
     IMO_UNAVAILABLE = 0x00000000,  // not specified
     IMO_HARDWARE = 0x00000001,  // from a hardware device or injected by a UIAccess app
     IMO_INJECTED = 0x00000002,  // injected via SendInput() by a non-UIAccess app
     IMO_SYSTEM = 0x00000004,  // injected by the system
 }
    
 public const int WM_MOUSEFIRST = 0x0200;
 public const int WM_MOUSELAST = 0x020E;
 public const int WM_KEYFIRST = 0x0100;
 public const int WM_KEYLAST = 0x0109;
 public const int WM_TOUCH = 0x0240;
 public const int WM_POINTERWHEEL = 0x024E;
    
    
 protected override void OnSourceInitialized(EventArgs e)
 {
     base.OnSourceInitialized(e);
     HwndSource hWndSource = PresentationSource.FromVisual(this) as HwndSource;
     hWndSource.AddHook(WndProc);
 }
    
 private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
 {
     if ((msg >= WM_MOUSEFIRST && msg <= WM_MOUSELAST) || (msg >= WM_KEYFIRST && msg <= WM_KEYLAST) || (msg >= WM_TOUCH && msg <= WM_POINTERWHEEL))
     {
         INPUT_MESSAGE_SOURCE ims = new INPUT_MESSAGE_SOURCE();
         GetCurrentInputMessageSource(out ims); 
         if (ims.deviceType == INPUT_MESSAGE_DEVICE_TYPE.IMDT_MOUSE)
         {
             Console.Beep(6000, 5);
         }
         // not tested
         else if (ims.deviceType == INPUT_MESSAGE_DEVICE_TYPE.IMDT_TOUCHPAD)
         {
             Console.Beep(2000, 5);
         }
     }
     return IntPtr.Zero;
 }



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Castorix31 ,

I have tried with your solution, device type still coming as IMDT_MOUSE for touchpad(track pad )events on laptop. I am using WINDOWS-10.
I want to differentiate touchpad Click and Mouse click events. is there any other way, we can get raw input is coming from touchPad or mouse?

0 Votes 0 ·