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. Dismiss Notice

Question How to totally disable Windows mouse input (through disabling device) ?

Discussion in 'Scripting' started by danielesuppo, Aug 4, 2023.

  1. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    Hello all!
    I have a very weird and specific question: I've developed a Windows Standalone application for a interactive kiosk, that is connected to a ATM (so the user should pay a few euros to proceed).
    When the ATM is activated by code (thanks to the provided C# SDK of the ATM producer), the Unity application "give to control to Windows" (please excuse this very non-techhnical explanation), that will wait for the transaction.
    So, to try to explain better, when the transaction process start, the Unity application "freeze", and come alive again just when receive some feedback from the ATM (transaction completed, transaction denied, etc).
    That's the way the provided SDK work.
    Well, everything work fine, BUT...
    In real life, I've noticed that many times, while the Unity application is "freezed", people that don't want to pay just touch the monitor (it's a touch screen) many times (maybe to see what happen... I don't know, people are crazy...)
    So, while the Unity application is hanged, if they touch many times the monitor it show a message that say "The application bla bla is not responding, ..... do you want to close it?"
    If they press "YES" the application is obviuosly closed.

    This is a problem!
    Unfortunately I can't simply disable mouse (touch) input from Unity, because when the payment process start, since the Unity app is hanged, the mouse (touch) is totally managed by Windows itself.

    So, I've discovered that there's a utility (pnputil) to disable/enable Windows devices by code with a batch file:
    for example, this line of code (in a .bat file) will disable my mouse:

    pnputil /disable-device "HID\VID_046D&PID_C050\6&2970368F&1&0000"

    Wonderful! BUT (again)...
    this Windows utility must be called as an administrator, so you first need to get the administrator prileges, so I've modified my batch file like this:

    >NUL 2>&1 REG.exe query "HKU\S-1-5-19" || (
    ECHO SET UAC = CreateObject^("Shell.Application"^) > "%TEMP%\Getadmin.vbs"
    ECHO UAC.ShellExecute "%~f0", "%1", "", "runas", 1 >> "%TEMP%\Getadmin.vbs"
    "%TEMP%\Getadmin.vbs"
    DEL /f /q "%TEMP%\Getadmin.vbs" 2>NUL
    Exit /b
    )
    pnputil /disable-device "HID\VID_046D&PID_C050\6&2970368F&1&0000"

    Fantastic! Now I can simply launch a batch file from Unity to totally disable the mouse (touch)!
    BUT... ... (again and again...)
    It perfectly work in Editor (I've created a button to launch this batch file, just for testing), but it doesn't work in the build o_O
    I mean, the batch file is called both in Editor and in the build, but it work only in Editor. In the build the device is not disabled.
    (But, if I directly double click on the batch file, while the build is running, it work and the mouse is disabled :confused:)

    So, to resume:
    1) I need to disable the mouse (touch) when the application is hanged
    2) I've created a batch file to disable/enable the mouse (or any device) from Windows
    3) I can call this batch file from Unity
    4) When I call it in Editor mode, or double clicking on the .bat file, it just work
    5) When I call it from the build it doesn't work

    Any help on this issue would be really a lot appreciated!
    Many thanks
     
  2. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    If your application "hangs" then that means something is blocking the Unity main thread. In order to do anything you will have to unblock it. I know, it's not the answer you seek but find out WHY it is blocking. Is it your code? Is it the SDK code? Maybe you can run the SDK in a separate thread so your Unity main thread won't freeze up.

    Also, running batch files as ADMIN on an ATM machine sounds like a recipe for desaster to me.
     
  3. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    Many thanks for your kind reply
    At the end the problem was that I was deploying for x86, and not for x86_64 (I'm on Unity 2020 on this project) so, for some reason that I don't know, in x86 mode some system application do not work (for sure pnputil).
    Just switching to x86_64 solved the problem.

    P.S. I'm not running on the ATM machine, it's just connected to my app
     
    _geo__ likes this.