简体   繁体   English

在滚轮输入上执行鼠标左键单击

[英]Perform Mouse Left Clicks on Scroll Wheel Input

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. 基本上,我想创建一个C#应用程序(我正在使用在Visual Studio中打开的标准WPF项目),该应用程序可以检测用户何时向上或向下滚动鼠标滚轮,并在当前鼠标上触发鼠标点击屏幕位置。

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. 我在WPF应用程序中使用C#,我已经成功地能够移动鼠标光标并使用以下代码执行左键单击,但是我在弄清楚如何侦听鼠标滚轮输入并执行将要执行的功能时遇到了麻烦收到输入后发送鼠标左键。 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 http://blogs.msdn.com/b/toub/archive/2006/05/03/589468.aspx

Source code: 源代码:

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
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
    {
        private static LowLevelMouseProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;


        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        public MainWindow()
        {
            int x = 1400;//Set Mouse Pos X
            int y = 340;//Set Mouse Pos  Y
            InitializeComponent();
            _hookID = SetHook(_proc);
        }

        public const int MOUSEEVENTF_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF_LEFTUP = 0x04;

        private static IntPtr SetHook(LowLevelMouseProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_MOUSE_LL, proc,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

        public static void MouseClick(int x, int y)
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
            System.Threading.Thread.Sleep(33);
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
        }

        private static IntPtr HookCallback(
            int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 &&
                MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam)
            {
                MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
                Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y);
                MouseClick(hookStruct.pt.x, hookStruct.pt.y);
            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        private const int WH_MOUSE_LL = 14;

        private enum MouseMessages
        {
            WM_LBUTTONDOWN = 0x0201,
            WM_LBUTTONUP = 0x0202,
            WM_MOUSEMOVE = 0x0200,
            WM_MOUSEWHEEL = 0x020A,
            WM_RBUTTONDOWN = 0x0204,
            WM_RBUTTONUP = 0x0205
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct POINT
        {
            public int x;
            public int y;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct MSLLHOOKSTRUCT
        {
            public POINT pt;
            public uint mouseData;
            public uint flags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM