Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:10:59+00:00 2026-05-28T04:10:59+00:00

I know how to modify cursor position from examples on MSDN. My question is

  • 0

I know how to modify cursor position from examples on MSDN.

My question is how to check position of mouse while mouse moves and then print X and Y position to representing labels?

EDIT: Lets say I want to track my mouse position from my entire screen.

EDIT 2: My app will be in the back ground/minimized.

I’m already using Mouse Hooks:

namespace Program
{
    public partial class MouseHook
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        public void DoMouseClick()
        {
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;

            IntPtr newP = new IntPtr(Convert.ToInt64("0", 16));
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, newP);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:10:59+00:00Added an answer on May 28, 2026 at 4:10 am

    Here is the msdn documentation on system level mouse event handling calls (low level mouse proc).

    Here is an example of using low level mouse procs to change the scroll event.

    In the answer in the second link use WM_MOUSEMOVE from here instead of WM_MOUSEWHEEL.

    One thing to note: for this program to continue to capture mouse events when the mouse is over a program with elevated privileges, this program will have to be launch with elevated privileges.

    Code (not tested):

    using System;
    
    using System.Diagnostics;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace CatchMouseMove
    {
        class InterceptMouse
        {
            const int INPUT_MOUSE = 0;
            const int MOUSEEVENTF_WHEEL = 0x0800;
            const int WH_MOUSE_LL = 14; 
    
    
            private static LowLevelMouseProc _proc = HookCallback;
            private static IntPtr _hookID = IntPtr.Zero;
    
            public static void Main()
            {
                _hookID = SetHook(_proc);
    
                if (_hookID == null)
                {
                    MessageBox.Show("SetWindowsHookEx Failed");
                    return;
                }
                Application.Run();
                UnhookWindowsHookEx(_hookID);
            }
    
            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);
    
            private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                int xPos = 0;
                int yPos = 0;
    
                if (nCode >= 0 && MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam)
                {    
                    xPos = GET_X_LPARAM(lParam); 
                    yPos = GET_Y_LPARAM(lParam);
    
                    //do stuff with xPos and yPos
                }
                return CallNextHookEx(_hookID, nCode, wParam, lParam);
            }
    
    
            private enum MouseMessages
            {
                WM_MOUSEMOVE = 0x0200
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct POINT
            {
                public int x;
                public int y;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct MSLLHOOKSTRUCT
            {
                public POINT pt;
                public int mouseData;
                public int flags;
                public int time;
                public IntPtr dwExtraInfo;
            }
    
            public struct INPUT
            {
                public int type;
                public MOUSEINPUT mi;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct MOUSEINPUT
            {
                public int dx;
                public int dy;
                public int mouseData;
                public uint dwFlags;
                public int time;
                public int 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);
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to know how to move the cursor from current textbox to previous
I know getting / setting cursor position in a contentEditable is damn near impossible.
Does anyone know how to modify an existing import specification in Microsoft Access 2007
Does anyone know how to modify the content of the Excel ribbon at runtime
Does anyone know how to modify the Wordpress canonical links to add a custom
Does anyone know how to modify the thread scheduling (specifically affinity) when using TBB?
I would like to know how to modify the below code to strip =20
I know that whenever you add/remove/modify any file in the App_Code, App_GlobalResources, and bin
Does someone know if it is possible to modify the JVM settings at runtime
I want to know I can dynamically modify an existing Crystal Report (using C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.