Search code examples
c#mouseevent

How to get mouse wheel state ?


In Microsoft.NET Framework there is a Form class in the namespace called System.Windows.Forms, and in there defined many events. The event KeyPressed occurs at the moment a key was pressed on the keyboard while the Form itself has input focus. But if I want that something will happen when a key is pressed at anytime no matter if the Form has input focus or not, I know that I can run a timer, and dllimport the GetKeyState, or GetAsyncKeyState functions from user32.dll, and know when a key was pressed at anytime.

The same thing I can do with the mouse with Form's MouseDown event and without, with the same user32.dll functions and a timer, but when I scroll up or down my mouse wheel, I can program that something will happen with the Form's MouseDown event.

Delta is the value for mouse wheel event at MouseEventArgs, but it occurs not only if the mouse wheel was scrolled up or down, but I need input focus on that Form with the MouseEvent.

I don't want to be dependent on any Form. I don't know which integer to give to the GetKeyState or GetAsyncKeyState functions to get the state of the mouse wheel, so I can run a piece of code in an if of a function that a timer calls frequently.

What should I do? Which integer is it? Is there an other function from user32 or other dll that can return the "Delta"? I will be happy if I will know, so please answer! :D


Solution

  • You can by the Add Reference tool to add the following files to your project: Microsoft.DirectX.dll and Microsoft.DirectX.DirectInput.dll, and then add two namespaces to your code: "using Microsoft.DirectX;" and "using Microsoft.DirectX.DirectInput;".

    These files can be found in your computer:

    • They are located in drive C.
    • The path is: Windows => Microsoft.NET => DirectX for Managed Code => 1.0.2902.0
    • You can hit the letters on your keyboard to find each folder or directory faster.
    • You can use the Search tool in My Computer to find where they are exactly, or verify that they are exist in your computer. If not, then you can download them from the internet, and locate them anywhere you want.

    Once all these steps above were done, you are able to create new global instance of type Device anywhere you want in the code by choosing the overloading constructor that you give him SystemGuid.Mouse. Then in the Main function you have to invoke the Acquire() function of the global Device instance that you created before.

    After that there is no problem to get the "Delta" of the mouse globally. Just resort to the global instance of Device, get from him the CurrentMouseState, and then ask for the Z property value that is an integer struct (int or System.Int32). This is exactly the mouse "Delta". Z > 0 = mouse wheel scrolled up, Z < 0 = mouse wheel scrolled down, Z == 0 = mouse wheel was not scrolled right now.

    Note: Using Microsoft.DirectX in your project requires you to target .NET framework under version 4.0 in Project Properties, because it doesn't support it. You'll face problems if you forget to make this change!