代码参考:文章来源:https://www.toymoban.com/news/detail-626952.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using static WindowsFormsApp1.GlobalMousePosition;
namespace 测试Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.TopMost = true;
}
private async void button1_Click(object sender, EventArgs e)
{
int time = 300;
//模拟点击
SimulateMouseClick(666, 666);
await SetText("2023-06-20");
Thread.Sleep(time);
//发送Alt+Q
SendKeys.Send("%Q");
Thread.Sleep(time);
Send_CV();
Release_CV();
Thread.Sleep(time);
// 发送回车键
SendKeys.Send("{ENTER}");
Thread.Sleep(time);
await SetText("G20");
Thread.Sleep(time);
Send_CV();
Release_CV();
Thread.Sleep(time);
// 发送回车键
SendKeys.Send("{ENTER}");
Thread.Sleep(time);
await SetText("上海");
Thread.Sleep(time);
Send_CV();
Release_CV();
Thread.Sleep(time);
// 发送回车键
SendKeys.Send("{ENTER}");
Thread.Sleep(time);
await SetText("广州");
Thread.Sleep(time);
Send_CV();
Release_CV();
MessageBox.Show("完成");
}
const int KEYEVENTF_EXTENDEDKEY = 0x0001;
const int KEYEVENTF_KEYUP = 0x0002;
private void Send_CV()
{
// 模拟按下 Ctrl 键
keybd_event(0x11, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
// 模拟按下 V 键
keybd_event(0x56, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
// 等待一小段时间
Thread.Sleep(100);
}
private void Release_CV()
{
// 模拟松开 V 键
keybd_event(0x56, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);
// 模拟松开 Ctrl 键
keybd_event(0x11, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);
}
private async Task SetText(string text)
{
await Task.Run(() => {
//写入剪贴板需要单独开个任务或者线程去跑 不然UI层容易死机
this.Invoke((MethodInvoker)(() => Clipboard.SetText(text)));
});
Thread.Sleep(200);
}
}
}
钩子类:文章来源地址https://www.toymoban.com/news/detail-626952.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WindowsFormsApp1
{
public static class GlobalMousePosition
{
#region 获取鼠标当前位置
public const int WH_MOUSE_LL = 14;
public enum MouseMessages
{
WM_MOUSEMOVE = 0x0200
}
public delegate int LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
#region 点击事件
[DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);
// 模拟鼠标点击一次
public static void SimulateMouseClick(int x, int y)
{
SetCursorPos(x, y);
Thread.Sleep(100); // 等待 100 毫秒,确保鼠标移动到指定位置
// 模拟鼠标按下和释放操作(模拟点击)
mouse_event(0x0002, x, y, 0, 0); // 鼠标左键按下
mouse_event(0x0004, x, y, 0, 0); // 鼠标左键释放
}
#endregion
#region 模拟键盘操作
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
#endregion
}
}
到了这里,关于C#版本的按键精灵,全局键盘鼠标事件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!