C# 定义热键
阅读原文时间:2023年07月09日阅读:1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace AutoSendMessages
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    #region AIP

    //如果函数执行成功,返回值不为0。  
    //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。  
    \[DllImport("user32.dll", SetLastError = true)\]  
    public static extern bool RegisterHotKey(  
        IntPtr hWnd,                //要定义热键的窗口的句柄  
        int id,                     //定义热键ID(不能与其它ID重复)  
        KeyModifiers fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效  
        Keys vk                     //定义热键的内容  
        );

    \[DllImport("user32.dll", SetLastError = true)\]  
    public static extern bool UnregisterHotKey(  
        IntPtr hWnd,                //要取消热键的窗口的句柄  
        int id                      //要取消热键的ID  
        );

    //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)  
    \[Flags()\]  
    public enum KeyModifiers  
    {  
        None = ,  
        Alt = ,  
        Ctrl = ,  
        Shift = ,  
        WindowsKey =  
    }

    //获取鼠标当前位置  
    \[DllImport("user32.dll")\]  
    private static extern bool GetCursorPos(out Point p);

    //设置鼠标位置  
    \[DllImport("User32")\]  
    public extern static void SetCursorPos(int x, int y);

    //模拟鼠标  
    \[DllImport("User32")\]  
    public extern static void mouse\_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);

    public enum MouseEventFlags  
    {  
        Move = 0x0001, //移动鼠标  
        LeftDown = 0x0002,//模拟鼠标左键按下  
        LeftUp = 0x0004,//模拟鼠标左键抬起  
        RightDown = 0x0008,//鼠标右键按下  
        RightUp = 0x0010,//鼠标右键抬起  
        MiddleDown = 0x0020,//鼠标中键按下  
        MiddleUp = 0x0040,//中键抬起  
        Wheel = 0x0800,  
        Absolute = 0x8000//标示是否采用绝对坐标  
    }

    #endregion

    private void Form1\_Load(object sender, EventArgs e)  
    {  
        RegisterHotKey(Handle, , KeyModifiers.None, Keys.F7);//F7 锁定消息框位置  
        RegisterHotKey(Handle, , KeyModifiers.None, Keys.F8);//F8 开始 / 暂停  
        RegisterHotKey(Handle, , KeyModifiers.None, Keys.F9);//F9 隐藏 / 显示

    }

    private void Form1\_FormClosing(object sender, FormClosingEventArgs e)  
    {  
        e.Cancel = true;  
        if (MessageBox.Show("你是否要退出?","退出提示",MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)  
        {  
            e.Cancel = false;  
        }  
    }

    private void Form1\_FormClosed(object sender, FormClosedEventArgs e)  
    {  
        UnregisterHotKey(Handle, );  
        UnregisterHotKey(Handle, );  
        UnregisterHotKey(Handle, );  
    }

    protected override void WndProc(ref Message m)  
    {  
        const int WM\_HOTKEY = 0x0312;  
        //按快捷键  
        switch (m.Msg)  
        {  
            case WM\_HOTKEY:  
                switch (m.WParam.ToInt32())  
                {  
                    case :    // F7 锁定位置  
                        FindPoint();  
                        break;  
                    case :    //F8 开始 / 暂停  
                        MakeStart();  
                        break;  
                    case :    //F9 隐藏 / 显示  
                        MakeShow();  
                        break;  
                }  
                break;  
        }  
        base.WndProc(ref m);  
    }

    Point CP = new Point();

    //寻找位置  
    private void FindPoint()  
    {  
        GetCursorPos(out CP);  
        label5.Text = "(" + CP.X + " , "+ CP.Y + ")";  
    }  
    //开始/暂停  
    private void MakeStart()  
    {  
        int t\_interval = ;  
        int.TryParse(textBox2.Text.ToString(), out t\_interval);  
        if (t\_interval < )  
        {  
            t\_interval = ;  
        }  
        timer1.Interval = t\_interval;  
        timer1.Enabled = !timer1.Enabled;  
    }  
    //隐藏/显示  
    private void MakeShow()  
    {  
        this.Visible = !this.Visible;  
    }

    int indexs = ;  
    private void timer1\_Tick(object sender, EventArgs e)  
    {  
        indexs++;  
        SendMessage();  
    }  
    //发送消息  
    \[DllImport("user32.dll", EntryPoint = "keybd\_event", SetLastError = true)\]  
    public static extern void keybd\_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);  
    public const int WM\_KEYDOWN = ;  
    public const int WM\_KEYUP = ;

    private void SendMessage()  
    {  
        if (textBox1.Text.ToString() != "")  
        {  
            MoveMouse();  
            DoubleMouse();  
            //Thread.Sleep(100);

            keybd\_event(Keys.ControlKey, , , );  
            keybd\_event(Keys.A, , , );  
            keybd\_event(Keys.A, , 0x2, );  
            keybd\_event(Keys.ControlKey, , 0x2, );

            Thread.Sleep();

            SendKeys.SendWait(textBox1.Text.ToString());  
            SendKeys.SendWait("{ENTER}");  
        }  
    }  
    //移动鼠标  
    private void MoveMouse()  
    {  
        SetCursorPos(CP.X, CP.Y);  
    }  
    //双击鼠标  
    private void DoubleMouse()  
    {  
        mouse\_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), , , , IntPtr.Zero);  
        mouse\_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), , , , IntPtr.Zero);

        //mouse\_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);  
        //mouse\_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);  
    }  
}  

}