[分享]WPF 虚拟键盘

场景:用WPF做触屏的软件难免会需要用户输入的问题,至少是简单的数字,这个时候就免不了需要键盘输入。

思路:既然是虚拟键盘,那么我的目的就是模拟键盘输入即可。

1.模拟键盘输入

模拟键盘输入?那么肯定免不了调用Win32API了。所以查看下Win32API是否提供了键盘输入的功能,找到发送按键的函数:

  

[DllImport("user32.dll", EntryPoint = "PostMessageW")]

public static extern int PostMessage( int hwnd,int wMsg,int wParam,int lParam);

2.找到需要输入的控件:

[DllImport("user32.dll")]
public static extern int GetFocus();

3.找到当前窗体

 

[DllImport("user32.dll")]
public static extern int GetForegroundWindow();

[DllImport("user32.dll")]
public static extern int AttachThreadInput(int idAttach,int idAttachTo,int fAttach);

[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(int hwnd, int lpdwProcessId);

 

关键代码:

(1)Win32API功能类:

   /// <summary>
    /// Win32接口功能类
    /// </summary>
    public static class Win32API
    {
        /// <summary>
        /// 键入
        /// </summary>
        public const int WM_KEYDOWN = 0x100;

        [DllImport("user32.dll", EntryPoint = "SendMessageW")]
        public static extern int SendMessage(
             int hwnd,
             int wMsg,
             int wParam,
             int lParam);
        [DllImport("user32.dll", EntryPoint = "PostMessageW")]
        public static extern int PostMessage(
             int hwnd,
             int wMsg,
             int wParam,
             int lParam);
        [DllImport("user32.dll")]
        public static extern int GetForegroundWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetLastActivePopup(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern int GetFocus();
        [DllImport("user32.dll")]
        public static extern int AttachThreadInput(
             int idAttach,
             int idAttachTo,
             int fAttach);
        [DllImport("user32.dll")]
        public static extern int GetWindowThreadProcessId(
             int hwnd,
             int lpdwProcessId);
        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();

        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
    }

(2)发送按键实现

     /// <summary>
        /// 发送按键
        /// </summary>
        /// <param name="asiiCode">键盘ascii码</param>
        private void SendKey(byte asiiCode)
        {
            AttachThreadInput(true);
            int getFocus = Win32API.GetFocus();
            //向前台窗口发送按键消息
            Win32API.PostMessage(getFocus, Win32API.WM_KEYDOWN, asiiCode, 0);
            AttachThreadInput(false); //取消线程亲和的关联
        }
        /// <summary>
        /// 设置线程亲和,附到前台窗口所在线程,只有在线程内才可以获取线程内控件的焦点
        /// </summary>
        /// <param name="b">是否亲和</param>
        private void AttachThreadInput(bool b)
        {
            Win32API.AttachThreadInput(
                   Win32API.GetWindowThreadProcessId(
                   Win32API.GetForegroundWindow(), 0),
                   Win32API.GetCurrentThreadId(), Convert.ToInt32(b));
        }

(3)附上按键ascii码表:

4.时间关系就做个简单的吧:

 

欢迎指正,转载请注明出处http://www.cnblogs.com/xinwang/p/6143169.html

 

转载于:https://www.cnblogs.com/xinwang/p/6143169.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值