Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Read Mouse HID

Discussion in 'Input System' started by WhosTheBoss, Aug 6, 2021.

  1. WhosTheBoss

    WhosTheBoss

    Joined:
    Jan 8, 2013
    Posts:
    64
    Hellow,

    Does anyone know if it's possible to read mouse HID? I've seen documentation on how to read HID from PS4 controllers etc.
    So my common sense tells me that it's possible to read a Mouse HID. I want to read mouse HID because I have two mice connected to my computer.
    I'm making a two-player game, where each player will physically sit next to each other and play the game using the separate mouse to point and shoot enemies.
     
    Bastienre4 likes this.
  2. Bastienre4

    Bastienre4

    Joined:
    Jul 8, 2014
    Posts:
    191
    If you want to assign the mouse for a player, maybe you can use the device ID?

    upload_2021-8-11_14-41-49.png
     
  3. WhosTheBoss

    WhosTheBoss

    Joined:
    Jan 8, 2013
    Posts:
    64
    OneMouse.png
    That was a clever suggestion. Unfortunately, Unity's Input system ignores Multiple Mouse. I currently have Two mice plugged into my computer, one keyboard, and one USB Gamepad. However, from debugging the InputSystem, it only reads one mouse.
     
  4. Bastienre4

    Bastienre4

    Joined:
    Jul 8, 2014
    Posts:
    191
    WhosTheBoss likes this.
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
  6. WhosTheBoss

    WhosTheBoss

    Joined:
    Jan 8, 2013
    Posts:
    64
  7. WhosTheBoss

    WhosTheBoss

    Joined:
    Jan 8, 2013
    Posts:
    64
    I got the Multiple Asset from Unity Store. It works and does what it says it's supposed to do.
    However, I'm actually using a Lightgun as a Mouse in my game. I have two USB IR Lightguns, when plugged into my PC, Windows 10 recognizes them as a USB Mouse. I can move the Lightguns around just as a regular mouse for pointing and clicking.
    The Multiple Asset package I purchased from the Unity store seems to have a hard time reading the X-Y of the Lightguns.


    For the C++ .dll.
    Do you know how that's supposed to work. I read the thread and downloaded the C++ code and tried compiling it but I don't understand how it's supposed to be used. The owner doesn't have much documentation.
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    i think it should produce dll when compiled, then place dll to your project (in Plugins/ and or project root if dll not found), and c# script calls the dll.
     
  9. WhosTheBoss

    WhosTheBoss

    Joined:
    Jan 8, 2013
    Posts:
    64
    I spent much of the day and I finally figured it out using the Asset you recommended. I had to do a lot of hacking and debugging.

    I was able to get both Lightguns to show up and read independently.
     

    Attached Files:

  10. WhosTheBoss

    WhosTheBoss

    Joined:
    Jan 8, 2013
    Posts:
    64
    thumbnail.jpg
     
    mgear likes this.
  11. Bastienre4

    Bastienre4

    Joined:
    Jul 8, 2014
    Posts:
    191
    Well done!
    Can you send your code to help us understand how it works? :)
     
  12. WhosTheBoss

    WhosTheBoss

    Joined:
    Jan 8, 2013
    Posts:
    64
    Thanks for your recommendation.
     
  13. WhosTheBoss

    WhosTheBoss

    Joined:
    Jan 8, 2013
    Posts:
    64
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using RavingBots.MultiInput; // Purchase the MultiInput Asset from Unity Asset store
    6. using UnityEngine.Events;
    7. using System.Linq;
    8. using UnityEngine.UI;
    9.  
    10. public class DisplayMultipleLightGuns
    11. {
    12.     // InputeState class is available when you purchase the MultiInput asset from Unity Asset store.
    13.     // Create an empty gameobject in the editor and attach a MultiInput.cs to the empty gameobject
    14.     // Attache the empty gameobject here.
    15.     public InputeState InputManager;
    16.  
    17.     public IDevice Gun1, Gun2;
    18.     private IVirtualAxis Gun1InfraredXValue, Gun1InfraredYValue, Gun2InfraredXValue, Gun2InfraredYValue;
    19.  
    20.     void Start()
    21.     {
    22.         if(InputManager.devices.Count() > 0)
    23.         {
    24.             foreach(IDevice thisDevice in InputManager.Devices)
    25.             {
    26.                 if(thisDevice.ProductID == abc123) // To get the actual product ID, you should use Windows Device Manager.
    27.                 {
    28.                     Gun1 = thisDevice;
    29.                 }
    30.                 if(thisDevice.ProductID == xyz123)
    31.                 {
    32.                      Gun2 = thisDevice;
    33.                 }
    34.             }
    35.         }
    36.     }
    37.  
    38.     void Update()
    39.     {
    40.         if(Gun1 != null)
    41.         {
    42.             Gun1InfraredXValue = Gun1[InputCode.MouseX];
    43.             Gun1InfraredYValue = Gun1[InputCode.MouseY];
    44.         }
    45.  
    46.         if(Gun2 != null)
    47.         {
    48.             Gun2InfraredXValue = Gun2[InputCode.MouseX];
    49.             Gun2InfraredYValue = Gun2[InputCode.MouseY];
    50.         }
    51.  
    52.         // From here on you can do whatever you want with Gun1InfraredX/YValue and Gun2InfraredX/YValue
    53.         // such as move cursors, laser, gameobjects etc.
    54.  
    55.     }
    56.  
    57. }
     
    Last edited: Aug 15, 2021
    Bastienre4 likes this.
  14. Sovogal

    Sovogal

    Joined:
    Oct 15, 2016
    Posts:
    100
    How do you get the positional values in sync with the actual pointer device? The input value of x and y are relative, so you can add those to a previous position to get your current position. However, they start at 0 no matter where the actual mouse is on screen, so the position is always off.