gpt4 book ai didi

c# - 使用 Mono 和 C# 监听系统范围内的点击?

转载 作者:可可西里 更新时间:2023-11-01 09:27:50 24 4
gpt4 key购买 nike

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

抱歉,这是一个真正愚蠢的问题,但来自 JS 背景,它只是 AddEventListener,这有点令人困惑,或者记录不当。谢谢

最佳答案

您正在寻找的是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);

为此,您需要在每次要检查 key 是否按下时检查 key 。您可以使用 virtual key code或使用 Keys 类。


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

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

不久前我做了类似的事情,但是我连接的是键盘而不是鼠标。这个过程是相似的,但是 Hook 到一个特定的程序要容易得多。下面的代码说明了我是如何解决我的问题的。

在下面的代码中,我创建了一个事件,只要按下一个键就会触发该事件,并将键码作为事件参数发送。

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

关于c# - 使用 Mono 和 C# 监听系统范围内的点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43803698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com