C#实现三种方式的模拟按键

模拟按键在.Net中有三种方式实现。

第一种方式:System.Windows.Forms.SendKeys 

                     组合键:Ctrl = ^ 、Shift = + 、Alt = %

模拟按键:A 

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Focus();
    SendKeys.Send("{A}");
}

 

模拟组合键:CTRL + A 

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Focus();
    SendKeys.Send("^{A}");
}

 

SendKeys.Send // 异步模拟按键(不阻塞UI) 

SendKeys.SendWait // 同步模拟按键(会阻塞UI直到对方处理完消息后返回)

 

第二种方式:keybd_event

模拟按键:A

[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
 
private void button1_Click(object sender, EventArgs e)
{
    textBox1.Focus();
    keybd_event(Keys.A, 0, 0, 0);
}

 

  

模拟组合键:CTRL + A

public const int KEYEVENTF_KEYUP = 2;
 
private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Focus();
    keybd_event(Keys.ControlKey, 0, 0, 0);
    keybd_event(Keys.A, 0, 0, 0);
    keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);
}

 

 

 模拟组合键:CTRL + X ,CTRL + C ,CTRL + V ,CTRL + Z

[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 KEYEVENTF_KEYUP = 2;
 
      private void InitSheet(FarPoint.Win.Spread.SheetView sheet)
      {
          // 列の設定
          sheet.ColumnCount = 3;
          // 行の設定
          sheet.RowCount = 9;
      }
 
      private void BtnCtrlX_Click(object sender, EventArgs e)
      {
          textBox1.Focus();
          keybd_event(Keys.ControlKey, 0, 0, 0);
          keybd_event(Keys.X, 0, 0, 0);
          keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);
          SendKeys.Send("^{X}");
      }
 
      private void BtnCtrlC_Click(object sender, EventArgs e)
      {
          textBox1.Focus();
          keybd_event(Keys.ControlKey, 0, 0, 0);
          keybd_event(Keys.C, 0, 0, 0);
          keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);
          SendKeys.Send("^{C}");
      }
 
      private void BtnCtrlV_Click(object sender, EventArgs e)
      {
          spreadEx1.Focus();
          keybd_event(Keys.ControlKey, 0, 0, 0);
          keybd_event(Keys.V, 0, 0, 0);
          keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);
          SendKeys.Send("^{V}");
      }
 
      private void BtnCtrlZ_Click(object sender, EventArgs e)
      {
          textBox1.Focus();
          keybd_event(Keys.ControlKey, 0, 0, 0);
          keybd_event(Keys.Z, 0, 0, 0);
          keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);
          SendKeys.Send("^{Z}");
      }

 

  

上面两种方式都是全局范围呢,现在介绍如何对单个窗口进行模拟按键

 

模拟按键:A / 两次 

  

 

[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
public static extern int PostMessage(IntPtr hWnd, int Msg, Keys wParam, int lParam);
 
public const int WM_CHAR = 256;
 
private void button1_Click(object sender, EventArgs e)
{
    textBox1.Focus();
    PostMessage(textBox1.Handle, 256, Keys.A, 2);
}

 

模拟组合键:CTRL + A

       如下方式可能会失效,所以最好采用上述两种方式 

public const int WM_KEYDOWN = 256;
public const int WM_KEYUP = 257;
 
private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Focus();
    keybd_event(Keys.ControlKey, 0, 0, 0);
    keybd_event(Keys.A, 0, 0, 0);
    PostMessage(webBrowser1.Handle, WM_KEYDOWN, Keys.A, 0);
    keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);
//直接给你贴一个我自己写的类,模拟键盘输入字符
using System.Runtime.InteropServices;
  
namespace lisonFunc
{
    static class MyKey
    {
        [DllImport("user32.dll", EntryPoint = "keybd_event")] 
        private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
  
        /// <summary>
        /// 按一个键后等待一段时间
        /// </summary>
        /// <param name="keyCode">要按的键,如Keys.Enter</param>
        /// <param name="wait">等待的时间(毫秒)</param>
        public static void KeyPress(Keys keyCode, int wait)
        {
            keybd_event((byte)keyCode, 0, 0, 0);
            keybd_event((byte)keyCode, 0, 2, 0);
            Thread.Sleep(wait);
        }
        /// <summary>
        /// Ctrl+...组合键
        /// </summary>
        /// <param name="keyCode">要同时按下的键,如Keys.C</param>
        public static void ControlKey(Keys keyCode)
        {
            keybd_event((byte)Keys.ControlKey, 0, 0, 0);
            keybd_event((byte)keyCode, 0, 0, 0);
            keybd_event((byte)keyCode, 0, 2, 0);
            keybd_event((byte)Keys.ControlKey, 0, 2, 0);
        }
        /// <summary>
        /// 重复n次按某个键
        /// </summary>
        /// <param name="keyCode">要按的键,如Keys.Enter</param>
        /// <param name="Times">按键次数</param>
        /// <param name="wait">间隔时间(毫秒)</param>
        public static void KeyPressRep(Keys keyCode, int Times, int wait)
        {
            for (int i = 0; i < Times; i++)
            {
                KeyPress(keyCode,wait);
            }
        }
        /// <summary>
        /// 输入一串数字
        /// </summary>
        /// <param name="NumWords"></param>
        public static void TypeNums(string NumWords)
        {
            foreach (char c in NumWords.ToCharArray())
            {
                KeyPress(48 + c, 50);
            }
        }
    }
}

 

posted @ 2021-12-02 20:28  卖雨伞的小男孩  阅读(1517)  评论(1编辑  收藏  举报