当前位置:   article > 正文

C#版本的按键精灵,全局键盘鼠标事件_c# 按键精灵

c# 按键精灵

代码参考:

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


    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

钩子类:

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/272438
推荐阅读
相关标签
  

闽ICP备14008679号