[分享]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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM