Jump to content

Differentiation between multiple mouses


Recommended Posts

Hello,

Scenario: Two mouses plugged into the same computer. 

Would it be possible for AutoIt to disable and enable one mouse or the other?

Or somehow assign different handles to each of the mouses clicks? (E.G. $hwnd1 = left click for mouse1 and $hwnd2 = left click for mouse2)

 

Thank you for your time

Link to comment
Share on other sites

I think I have found a way.  Let me test it tomorrow, as I am too tired tonight to continue.  But if you want to invest time to understand your issue I would suggest you read about _WinAPI_EnumRawInputDevices and their related functions _WinAPI_GetRawInputDeviceInfo, _WinAPI_GetRawInputData.

Anyway tomorrow I shall post something interesting...

Link to comment
Share on other sites

Ok found some energy (curiosity ?), I tested on 2 different mouses and it works perfect... 

#include <Constants.au3>
#include <GUIConstants.au3>
#include <Array.au3>
#include <WinAPISys.au3>

Opt("MustDeclareVars", 1)

Global Const $HWND_MESSAGE = -3
Global Const $HID_USAGE_PAGE_GENERIC = 0x1
Global Const $HID_USAGE_GENERIC_MOUSE = 0x2

Global $aMouses = EnumRawMouses("\HID")

If Not IsArray($aMouses) Or Not $aMouses[0][0] Then Exit MsgBox($MB_SYSTEMMODAL, "", "Unable to find mouse device")

Global $hTarget = GUICreate("", 10, 10, Default, Default, Default, Default, $HWND_MESSAGE)
Register_RawInput($HID_USAGE_PAGE_GENERIC, $HID_USAGE_GENERIC_MOUSE, $RIDEV_INPUTSINK, $hTarget)

GUIRegisterMsg($WM_INPUT, 'WM_INPUT')
HotKeySet("{ESC}", _Exit)

While 1
  Sleep(100)
WEnd

Func _Exit()
  Exit
EndFunc   ;==>_Exit

Func Register_RawInput($iUsagePage, $iUsage, $iFlags, $hTargetHwnd)
  Local $tRID = DllStructCreate($tagRAWINPUTDEVICE)
  DllStructSetData($tRID, 'UsagePage', $iUsagePage)
  DllStructSetData($tRID, 'Usage', $iUsage)
  DllStructSetData($tRID, 'Flags', $iFlags)
  DllStructSetData($tRID, 'hTarget', $hTargetHwnd)
  _WinAPI_RegisterRawInputDevices($tRID)
EndFunc   ;==>Register_RawInput

Func WM_INPUT($hWnd, $iMsg, $wParam, $lParam) ;Callback from RawInput
  Local $tRIM = DllStructCreate($tagRAWINPUTMOUSE), $hDevice, $iMouse = 0
  If _WinAPI_GetRawInputData($lParam, $tRIM, DllStructGetSize($tRIM), $RID_INPUT) Then
    $hDevice = "0x" & Hex(DllStructGetData($tRIM, "hDevice"))
    For $i = 1 To $aMouses[0][0]
      If $hDevice = $aMouses[$i][0] Then $iMouse = $i
    Next
    Local $iFlags = DllStructGetData($tRIM, 'ButtonFlags')
    Local $sEvent = $iFlags & " / "
    If BitAND($iFlags, BitOR($RI_MOUSE_MIDDLE_BUTTON_DOWN, $RI_MOUSE_MIDDLE_BUTTON_UP, $RI_MOUSE_LEFT_BUTTON_DOWN, $RI_MOUSE_LEFT_BUTTON_UP, _
        $RI_MOUSE_RIGHT_BUTTON_DOWN, $RI_MOUSE_RIGHT_BUTTON_UP)) Then
      If BitAND($iFlags, $RI_MOUSE_MIDDLE_BUTTON_DOWN) Then $sEvent &= "RI_MOUSE_MIDDLE_BUTTON_DOWN / "
      If BitAND($iFlags, $RI_MOUSE_MIDDLE_BUTTON_UP) Then $sEvent &= "RI_MOUSE_MIDDLE_BUTTON_UP / "
      If BitAND($iFlags, $RI_MOUSE_LEFT_BUTTON_DOWN) Then $sEvent &= "RI_MOUSE_LEFT_BUTTON_DOWN / "
      If BitAND($iFlags, $RI_MOUSE_LEFT_BUTTON_UP) Then $sEvent &= "RI_MOUSE_LEFT_BUTTON_UP / "
      If BitAND($iFlags, $RI_MOUSE_RIGHT_BUTTON_DOWN) Then $sEvent &= "RI_MOUSE_RIGHT_BUTTON_DOWN / "
      If BitAND($iFlags, $RI_MOUSE_RIGHT_BUTTON_UP) Then $sEvent &= "RI_MOUSE_RIGHT_BUTTON_UP / "
    EndIf
    If Not $iFlags Then $sEvent &= "MOUSE_MOVE / "
    If BitAND($iFlags, $RI_MOUSE_WHEEL) Then
      Local $aData = _WinAPI_WordToShort(DllStructGetData($tRIM, 'ButtonData'))
      If $aData > 0 Then
        $sEvent &= 'Up / '
      Else
        $sEvent &= 'Down / '
      EndIf
    EndIf
    ConsoleWrite("Mouse " & $iMouse & " = " & $sEvent & DllStructGetData($tRIM, "LastX") & "/" & DllStructGetData($tRIM, "LastY") & @CRLF)
  EndIf
  Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_INPUT

Func EnumRawMouses($sDeviceNameMatch = "")
  Local Const $tagRID_INFO_MOUSE = "dword Size;dword Type;struct;dword Id;dword NumberOfButtons;dword SampleRate;int HasHorizontalWheel;endstruct;dword Unused[2];"
  Local $aData = _WinAPI_EnumRawInputDevices()
  If Not IsArray($aData) Then Exit MsgBox($MB_SYSTEMMODAL, "", "Enum input devices didn't work")
  ;_ArrayDisplay($aData)

  Local $aMouses[$aData[0][0]][7]
  Local $iCount = 0, $iSize
  Local $tInfo = DllStructCreate($tagRID_INFO_MOUSE), $tDeviceName, $sDeviceName

  For $i = 1 To $aData[0][0]
    If $aData[$i][1] = $RIM_TYPEMOUSE And _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $tInfo, DllStructGetSize($tInfo), $RIDI_DEVICEINFO) Then
      $iSize = _WinAPI_GetRawInputDeviceInfo($aData[$i][0], 0, 0, $RIDI_DEVICENAME)         ;Get bytes needed
      $tDeviceName = DllStructCreate('wchar[' & $iSize + 1 & ']')         ;Holds device name string
      If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $tDeviceName, DllStructGetSize($tDeviceName), $RIDI_DEVICENAME) Then
        $sDeviceName = DllStructGetData($tDeviceName, 1)
        If $sDeviceNameMatch <> "" And Not StringInStr($sDeviceName, $sDeviceNameMatch) Then ContinueLoop
        $iCount += 1
        $aMouses[$iCount][0] = $aData[$i][0]           ;Handle
        $aMouses[$iCount][1] = $aData[$i][1]           ;Type
        $aMouses[$iCount][2] = $sDeviceName
        $aMouses[$iCount][3] = DllStructGetData($tInfo, "Id")
        $aMouses[$iCount][4] = DllStructGetData($tInfo, "NumberOfButtons")
        $aMouses[$iCount][5] = DllStructGetData($tInfo, "SampleRate")
        $aMouses[$iCount][6] = DllStructGetData($tInfo, "HasHorizontalWheel")
      EndIf
    EndIf
  Next
  $aMouses[0][0] = $iCount
  ReDim $aMouses[$iCount + 1][7]
  ;_ArrayDisplay($aMouses)
  Return $aMouses
EndFunc   ;==>EnumRawMouses

 

Edited by Nine
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...