Listen to system click with Mono and C #?

I want to use mono to write a simple CL tool that logs every click throughout the system. I understand that I can access this from Windows Forms? What's like a wrapper around Windows internal API?

Sorry this is a real stupid question, but coming from a JS background where its just AddEventListener is a bit confusing or poorly documented. thank

+3


source to share


1 answer


What you are looking for is in user32.dll


Here are some links about this:

http://pinvoke.net/default.aspx/user32.GetAsyncKeyState

https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx

looking that the user is pressing a key or not?


The first link contains examples of using dlls.

You can do several things with this DLL. For example, what you need is

[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Int32 vKey);

      



To do this, you will need to check the key every time you want to check if the key is turned off. You can use virtual key code or use class Keys

.


If you also want to simulate mouse events, for example send a left click to the system, the following code is what you need. (more details here )

[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);

      


I recently did a similar thing, however, I connected to the keyboard and not the mouse. The process is similar, but it is much easier to connect to a specific program. Below is the code on how I solved my problem.

In the following code, I created an event that fires whenever a key was pressed and sent the Key Code as the event argument.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KeyHook {
    public class KeyHook {
        const int WH_KEYBOARD_LL = 13;

        const int WM_KEYDOWN = 0x0100;
        const int WM_KEYUP = 0x0101;

        delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        LowLevelKeyboardProc _proc { get; set; }

        IntPtr _hookID { get; set; }

        public delegate void KeyHandler(Keys k);
        public event KeyHandler OnKeyDown;
        public event KeyHandler OnKeyUp;

        public KeyHook() {
            Initialize();
            _hookID = SetHook(_proc);
        }

        void Initialize() {
            this._proc = HookCallback;
            this._hookID = IntPtr.Zero;
            Application.ApplicationExit += Application_ApplicationExit;
        }

        void Application_ApplicationExit(object sender, EventArgs e) {
            UnhookWindowsHookEx(_hookID);
        }

        IntPtr SetHook(LowLevelKeyboardProc proc) {
            using (Process curProcess = Process.GetCurrentProcess()) {
                using (ProcessModule curModule = curProcess.MainModule) {
                    return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle

(curModule.ModuleName), 0);
                }
            }
        }

        IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN) {
                if (this.OnKeyDown != null) {
                    this.OnKeyDown((Keys)Marshal.ReadInt32(lParam));
                }
            } else if (nCode >= 0 && wParam == (IntPtr)WM_KEYUP) {
                if (this.OnKeyUp != null) {
                    this.OnKeyUp((Keys)Marshal.ReadInt32(lParam));
                }
            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        #region dll Imports
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, 

uint dwThreadId);


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


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


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

      


+2


source







All Articles