globally detecting mouse pressed

Piotr Rybak 21 Reputation points
2021-04-07T16:45:39.263+00:00

I do

 for (; ; ) {
                if (System.Windows.Input.Mouse.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
                {
                    MessageBox.Show("hgg");
                }
            }

It is not uesponding to mouse button press.

HOW can I find globally if mouse button is being pressed?

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Castorix31 91,506 Reputation points
    2021-04-07T18:05:47.187+00:00

    If you mean in the whole system, you can use a WH_MOUSE_LL hook

    A test with a Beep for Left Button =>
    (add using System.Runtime.InteropServices; at beginning)

    public partial class Form1 : Form
    {
        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
    
        [DllImport("User32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
    
        [DllImport("User32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
    
        [DllImport("User32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
    
        public const int WH_MIN = (-1);
        public const int WH_MSGFILTER = (-1);
        public const int WH_JOURNALRECORD = 0;
        public const int WH_JOURNALPLAYBACK = 1;
        public const int WH_KEYBOARD = 2;
        public const int WH_GETMESSAGE = 3;
        public const int WH_CALLWNDPROC = 4;
        public const int WH_CBT = 5;
        public const int WH_SYSMSGFILTER = 6;
        public const int WH_MOUSE = 7;
        public const int WH_HARDWARE = 8;
        public const int WH_DEBUG = 9;
        public const int WH_SHELL = 10;
        public const int WH_FOREGROUNDIDLE = 11;
        public const int WH_CALLWNDPROCRET = 12;
        public const int WH_KEYBOARD_LL = 13;
        public const int WH_MOUSE_LL = 14;
        public const int WH_MAX = 14;
        public const int WH_MINHOOK = WH_MIN;
        public const int WH_MAXHOOK = WH_MAX;
    
        [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct MSLLHOOKSTRUCT
        {
            public System.Drawing.Point pt;
            public int mouseData;
            public int flags;
            public int time;
            public uint dwExtraInfo;
        }
    
        public const int WM_MOUSEMOVE = 0x0200;
        public const int WM_LBUTTONDOWN = 0x0201;
        public const int WM_LBUTTONUP = 0x0202;
        public const int WM_RBUTTONDOWN = 0x0204;
        public const int WM_RBUTTONUP = 0x0205;
    
    
        public Form1()
        {
            //InitializeComponent();
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            this.Load += new System.EventHandler(this.Form1_Load);
        }
    
        private static Label label1;
    
        private static int hHook = 0;
        private HookProc MouseLLProcedure;  
    
        private void Form1_Load(object sender, EventArgs e)
        {
            label1 = new Label();
            label1.Size = new Size(100, 32);
            label1.Location = new Point(90, 100);
            label1.BorderStyle = BorderStyle.Fixed3D;
            label1.BackColor = Color.Black;
            label1.ForeColor = Color.Yellow;
            label1.Font = new Font("Arial", 8F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
            label1.TextAlign = ContentAlignment.MiddleCenter;
            this.Controls.Add(label1);         
    
            MouseLLProcedure = new HookProc(MouseLLProc);
            hHook = SetWindowsHookEx(WH_MOUSE_LL, MouseLLProcedure, (IntPtr)0, 0);
    
            this.Text = "Test LL Mouse Hook";
            this.ClientSize = new System.Drawing.Size(300, 260);
            CenterToScreen();
        }
    
         public static int MouseLLProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if ((nCode >= 0))
            {
                MSLLHOOKSTRUCT pMSLLHOOKSTRUCT = new MSLLHOOKSTRUCT();
                pMSLLHOOKSTRUCT = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, pMSLLHOOKSTRUCT.GetType());
                label1.Text = pMSLLHOOKSTRUCT.pt.ToString();
    
                if (wParam == (IntPtr)WM_LBUTTONDOWN)
                {                   
                    Console.Beep(5000, 10);
                }               
            }
            return nCode < 0 ? CallNextHookEx(hHook, nCode, wParam, lParam) : 0;
        }
    
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (hHook != 0)
                UnhookWindowsHookEx(hHook);
        }
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.