【问题标题】:Listen for system wide click using Mono and C#?使用 Mono 和 C# 监听系统范围的点击?
【发布时间】:2017-10-03 20:41:08
【问题描述】:

我想使用 mono 来编写一个简单的 CL 工具来记录系统周围的每次点击。我知道我可以从 Windows 窗体访问它?哪个类似于内部 Windows API 的包装器?

抱歉,这是一个非常愚蠢的问题,但来自 JS 背景,其中它只是 AddEventListener,这有点令人困惑,或者记录不充分。谢谢

【问题讨论】:

  • 在 .NET Framework 上,您可以使用 PInvoke 通过调用相关的 Win32 API 来实现这一点。 Mono 允许您做同样的事情。关于调用哪个 Win32 API,以及如何使用 PInvoke,请使用 Google。
  • 出于兴趣,既然你在 Windows 上,为什么你使用 Mono 而不是 .NET 框架?

标签: c# windows mono


【解决方案1】:

你要找的是user32.dll


这里有一些关于它的链接:

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

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

looking up that the user press a key or not?


第一个链接包含如何使用 dll 的示例。

您可以使用此 dll 执行多项操作。例如,你所追求的是

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

为此,您需要在每次要检查密钥是否已关闭时检查密钥。您可以使用virtual key codeKeys 类。


如果您还想模拟鼠标事件,例如向系统发送左键单击,则以下代码就是您所追求的。 (更多信息here

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

不久前我也做过类似的事情,但是我连接的是键盘而不是鼠标。这个过程是相似的,但是它更容易挂钩到一个特定的程序。下面是我如何解决问题的代码。

在以下代码中,我创建了一个事件,该事件在按下某个键时触发,并将键代码作为事件参数发送。

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

【讨论】:

    猜你喜欢
    • 2016-11-12
    • 2011-12-10
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多