Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am writing a console application. I am able to get mouse position wrt screen. Can anybody help me getting mouse events enabled as it works in forms.
Thank you for your time.
Posted
Updated 20-Oct-18 17:44pm
Comments
Sergey Alexandrovich Kryukov 29-Jun-11 3:41am    
Wow! In console?! and what do you want to do with it, print mouse coordinates?
--SA
Morl99 29-Jun-11 3:43am    
I can't imagine that you will Be able to capture clicks in the console, but prove me wrong!
J imran 29-Jun-11 3:43am    
I have done with mouse coordinates. I want events like mouse click mouse hover and so on..
J imran 29-Jun-11 3:46am    
I can do it in assembly but c# doesn't invoke assembly code I guess.I once made a program with DOS interrupts in C++. In c# I don't find any method to invoke DOS interrupts :(

 
Share this answer
 
Comments
J imran 29-Jun-11 3:58am    
thank you. You are always helping. I have completed that project in c#. thank God the client agreed not to convert it in c++.
happy coding
Tarun.K.S 29-Jun-11 4:19am    
Please do vote and accept his answer.
Sergey Alexandrovich Kryukov 29-Jun-11 4:51am    
Thank you Tarun. Did you decide to vote not by your own but indirectly? :-)
Thank you very much for proposing to accept it formally. OP could use a green button.
--SA
Manfred Rudolf Bihy 29-Jun-11 5:00am    
I'll get you started with my 5! ;)
Sergey Alexandrovich Kryukov 29-Jun-11 11:55am    
Thank you, Manfred.
--SA
Look at my short example -

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ComputerToggle
{
    class Program
    {
        [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);

        private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
        private const int WH_MOUSE_LL = 14;

        private static LowLevelMouseProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;

        static void Main(string[] args)
        {
            _hookID = SetHook(_proc);

            System.Windows.Forms.Application.Run();

            UnhookWindowsHookEx(_hookID);

            Console.Read();
        }

        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 static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam)
            {
                MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
                Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y);
            }

            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        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;
        }

    }
}


Enjoy!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900