Reputation:
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
Upvotes: 3
Views: 3391
Reputation:
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:
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!
Upvotes: 0
Reputation: 54532
There is a really nice CodePlex library that will allow you to easily get Global Mouse and KeyBoard events.
Description from above link:
This library allows you to tap keyboard and mouse and to detect and record their activity even when an application is inactive and runs in background.
This library attaches to windows global hooks, tracks keyboard and mouse clicks and movement and raises common .NET events with KeyEventArgs and MouseEventArgs, so you can easily retrieve any information you need: Mouse coordinates
Example (modified from the Demo program) it will only give you the Delta:
using System;
using System.Windows.Forms;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private readonly MouseHookListener m_MouseHookManager;
public Form1()
{
InitializeComponent();
m_MouseHookManager = new MouseHookListener(new GlobalHooker());
m_MouseHookManager.Enabled = true;
m_MouseHookManager.MouseWheel += HookManager_MouseWheel;
}
private void HookManager_MouseWheel(object sender, MouseEventArgs e)
{
labelWheel.Text = string.Format("Wheel={0:000}", e.Delta);
}
}
}
Upvotes: 3