Basically I would like to create a C# application (I'm using a standard WPF project opened in visual studio) that can detect when the user has scrolled the mouse wheel once either up or down, and to fire a mouse click at the current mouse screen position.
The Pseudo Code for the program I would give for what I want is
While the program is running
If the program detects a mouse scroll wheel up or scroll wheel down from the user
Perform a Single Left Click at the current mouse screen position
End If
End While
I do not know how to detect the mouse scroll wheel. I am using C# in a WPF Application, I have successfully been able to move the mouse cursor and perform a left click with the below code but I am having trouble figuring out how I can listen for mouse scroll wheel input and perform a function that will send the left mouse click when it receives the input. It will need to work even when the application does not have focus since the mouse clicks are sent to another application. Can anyone point me in the right direction to where I need to go to get this working.
Thank you. Current code is below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Clicky_Clicky
{
public partial class MainWindow : Window
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public const int WM_MOUSEWHEEL = 0x020A;
public void MouseClick(int x, int y)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
public MainWindow()
{
int x = 1400;//Set Mouse Pos X
int y = 340;//Set Mouse Pos Y
InitializeComponent();
SetCursorPos(x, y); //Move Mouse to position on screen designated by X and Y
MouseClick(x, y); //Perform a mouse click (mouse event down, mouse event up)
}
}
}
EDIT: Witha little more research it looks like the thing I want is a global hook for the mouse but I still have not been able to find a simple way to get what I want.
I was able to get a very simple answer for what I wanted to do using
http://blogs.msdn.com/b/toub/archive/2006/05/03/589468.aspx
Source code: