Winform虚拟/模拟键盘

项目需要在触摸屏上增加一个虚拟键盘。记录下过程中遇到的问题及解决方法。

1.模拟按键

网上找到如下3种方法

1)SendKeys.Send

测试单独的shift不好用,所以最终未采纳此方法

SendKeys.Send("^{E}");//shift+e
SendKeys.Send("{Enter}");

2)keybd_event

最终选择了这个方法,简单有效。

虚拟按键对照表:https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
public static uint KEYEVENTF_KEYDOWN = 0;// 键按下 KEYEVENTF_KEYDOWN = 0
public static uint KEYEVENTF_KEYUP = 2;//键弹起 KEYEVENTF_KEYUP = 2
keybd_event(0x14, 0, KEYEVENTF_KEYDOWN, 0); //键按下 KEYEVENTF_KEYDOWN = 0
keybd_event(0x14, 0, KEYEVENTF_KEYUP, 0); //键弹起 KEYEVENTF_KEYUP = 2

3)PostMessage

这个方法也是简单有效,但是据说有些其他问题,没有仔细研究,有兴趣的可以自己研究。

[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
public static extern int PostMessage(IntPtr hWnd, int Msg, Keys wParam, int lParam);

PostMessage(textBox2.Handle, 256, Keys.D, 2);//模拟按下2次 Keys.D

2.各种类型键的处理

微软对键盘输入有进行了分类,可以参考这个。

https://learn.microsoft.com/zh-cn/windows/win32/learnwin32/keyboard-input

1)CapsLock键/NumLock键

想要实现键盘按下抬起与界面效果相同,就需要捕获键盘操作。我使用了Hook

以下这篇博客写的很清晰明了,就不赘述了。

https://www.cnblogs.com/chorm590/p/14199978.html

在hook回调函数中做了如下处理

private int keyboardHookCallback(int code, IntPtr wParam, IntPtr lParam)
{
  if (code < 0)
    {
       return User32.CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
    }
   else
   {
     Keyboard_LL_Hook_Data khd = (Keyboard_LL_Hook_Data)Marshal.PtrToStructure(lParam, typeof(Keyboard_LL_Hook_Data));
      System.Diagnostics.Debug.WriteLine($"key event:{wParam}, key code:{khd.vkCode}, event time:{khd.time}");
      if ((int)wParam == 256 && khd.vkCode == 0x14)
      {
        IsCapsLockDown = !IsCapsLockDown;
      }
      return User32.CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
   }
}
private bool isCapsLockDown;
private bool IsCapsLockDown
{
  get { return isCapsLockDown; }
   set
    {
      isCapsLockDown = value;
       this.buttonCapsLock.BackColor = value ? Color.Gray : Control.DefaultBackColor;
    }
}
private void buttonCapsLock_Click(object sender, EventArgs e)
{
  User32.keybd_event(VirtualKeyCode.CAPS_LOCK, 0, 0, 0);   //键按下 KEYEVENTF_KEYDOWN = 0
   User32.keybd_event(VirtualKeyCode.CAPS_LOCK, 0, 2, 0);  //键弹起 KEYEVENTF_KEYUP = 2
}

初始化时同步键盘CapsLock/NumLock状态,使用user32.GetKeyState.返回值等于1则按下状态

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern short GetKeyState(int keyCode);
public static bool GetState(byte VKeyCode)
{
   return (User32.GetKeyState(VKeyCode) == 1);
}

2)其他组合键处理

ctrl+shift切换输入法,只响应这一种功能键+功能键。其他小伙伴有好的方法可以分享一下。

3)输入键+组合键

如ctrl+c,ctrl+v,ctrl+a,ctrl+z等等。

此方法不是最优方式,可以改进功能键存放在Queue中,遍历功能键,按顺序响应。

private void HandleKeysCombin(byte VKCode)
{
  //设置焦点控件
   this.ActiveControl = this.m_CustomActiveControl;
   lock (this)
   {
     if (IsCtrlDown)
      {
        keybd_event(VirtualKeyCode.CTRL, 0, KEYEVENTF_KEYDOWN, 0);                //键按下 KEYEVENTF_KEYDOWN = 0
      }
      if (IsShiftDown)
      {
        keybd_event(VirtualKeyCode.SHIFT, 0, KEYEVENTF_KEYDOWN, 0);                //键按下 KEYEVENTF_KEYDOWN = 0
      }
      if (IsAltDown)
      {
        keybd_event(VirtualKeyCode.ALT, 0, KEYEVENTF_KEYDOWN, 0);                //键按下 KEYEVENTF_KEYDOWN = 0
      }
      keybd_event(VKCode, VKCode, User32.KEYEVENTF_KEYDOWN, 0);                //键按下 KEYEVENTF_KEYDOWN = 0
      keybd_event(VKCode, VKCode, User32.KEYEVENTF_KEYUP, 0);           //键弹起 KEYEVENTF_KEYUP = 2
      if (IsCtrlDown)
      {
        keybd_event(VirtualKeyCode.CTRL, 0, KEYEVENTF_KEYUP, 0);                //键弹起 KEYEVENTF_KEYUP = 2
         IsCtrlDown = false;
      }
      if (IsShiftDown)
      {
        keybd_event(VirtualKeyCode.SHIFT, 0, KEYEVENTF_KEYUP, 0);              //键弹起 KEYEVENTF_KEYUP = 2
         IsShiftDown = false;
      }
      if (IsAltDown)
      {
         keybd_event(VirtualKeyCode.ALT, 0, KEYEVENTF_KEYUP, 0);                //键弹起 KEYEVENTF_KEYUP = 2
        IsAltDown = false;
     }
  }
}

3.窗体/UserControl焦点的处理

这篇里面方法简单好用,不赘述

 https://blog.csdn.net/accomp/article/details/7209052

posted on 2023-05-23 16:11  LotusGu  阅读(809)  评论(0编辑  收藏  举报

导航