首页 新闻 会员 周边

WPF TextBox 如何获取自定义虚拟软键盘输入的值

0
悬赏园豆:5 [待解决问题]

如下图所示,目前是当窗体中的文本框获取焦点后将 自定义虚拟软键盘(keyboard.xaml) 通过keyboard.show() 显示出来,但是文本框获取不到通过虚拟软键盘输入的值,反而是电脑上任意一个软件中能输入文本值的地方,都可以通过这个弹出的软件盘进行字符输入,求教高手支招!谢谢


 

   public partial class MainWindow : Window
    {
        [DllImport("User32.dll")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIdex);

        [DllImport("User32.dll")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("User32.dll")]
        public static extern void keybd_event(byte bVK, byte bScan, Int32 dwFlags, int dwExtraInfo);

        [DllImport("User32.dll")]
        public static extern uint MapVirtualKey(uint uCode, uint uMapType);

        [DllImport("user32.dll", EntryPoint = "GetKeyboardState")]
        public static extern int GetKeyboardState(byte[] pbKeyState);

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Drop(object sender, System.Windows.DragEventArgs e)
        {
            // 在此处添加事件处理程序实现。
        }
        private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // 在此处添加事件处理程序实现。
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (CapsLockStatus == true)
            {
                checkImage.Visibility = Visibility.Visible;
            }
            else
            {
                checkImage.Visibility = Visibility.Hidden;

            }
            IntPtr a = new System.Windows.Interop.WindowInteropHelper(this).Handle;
            int temp = GetWindowLong(a, -20);
            SetWindowLong(a, -20, temp | 0x08000000);
            this.ShowInTaskbar = false;
        }
        private void cmd1_LostFocus(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
        }
        public static bool CapsLockStatus
        {
            get
            {
                byte[] bs = new byte[256];
                GetKeyboardState(bs);
                return (bs[0x14] == 1);
            }
        }
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == 0x0216)
            {
                System.Drawing.Rectangle rect = (System.Drawing.Rectangle)Marshal.PtrToStructure(lParam, typeof(System.Drawing.Rectangle));
                this.Left = rect.Left;
                this.Top = rect.Top;
            }
            return IntPtr.Zero;
        }

        #region Property & Variable & Constructor
        private static double _WidthTouchKeyboard = 605;

        public static double WidthTouchKeyboard
        {
            get { return _WidthTouchKeyboard; }
            set { _WidthTouchKeyboard = value; }

        }
        private static bool _ShiftFlag;

        protected static bool ShiftFlag
        {
            get { return _ShiftFlag; }
            set { _ShiftFlag = value; }
        }
        private static bool _CtrlFlag;
        protected static bool CtrlFlag
        {
            get { return _CtrlFlag; }
            set { _CtrlFlag = value; }
        }

        #endregion
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.Button keybtn = sender as System.Windows.Controls.Button;
            #region//First Row
            if (keybtn.Name == "CmdTlide")
            {
                addNumkeyINput(0xc0);
            }
            else if (keybtn.Name == "cmd1")
            {
                addNumkeyINput(0x31);
            }
            else if (keybtn.Name == "cmd2")
            {
                addNumkeyINput(0x32);
            }
            else if (keybtn.Name == "cmd3")
            {
                addNumkeyINput(0x33);
            }
            else if (keybtn.Name == "cmd4")
            {
                addNumkeyINput(0x34);
            }
            else if (keybtn.Name == "cmd5")
            {
                addNumkeyINput(0x35);
            }
            else if (keybtn.Name == "cmd6")
            {
                addNumkeyINput(0x36);

            }
            else if (keybtn.Name == "cmd7")
            {
                addNumkeyINput(0x37);
            }
            else if (keybtn.Name == "cmd8")
            {
                addNumkeyINput(0x38);
            }
            else if (keybtn.Name == "cmd9")
            {
                addNumkeyINput(0x39);
            }
            else if (keybtn.Name == "cmd0")
            {
                addNumkeyINput(0x30);

            }
            else if (keybtn.Name == "cmdminus")//-_
            {
                addNumkeyINput(0xbd);
            }
            else if (keybtn.Name == "cmd1")//+=
            {
                addNumkeyINput(0xbb);
            }
            else if (keybtn.Name == "cmdBackspace")//backspace
            {
                AddKeyBoardINput(0x08);
            }
            #endregion
            #region//Second Row
            else if (keybtn.Name == "CmdTab")
            {
                AddKeyBoardINput(0x09);
            }
            else if (keybtn.Name == "CmdQ")
            {
                AddKeyBoardINput(0x51);
            }
            else if (keybtn.Name == "Cmdw")
            {
                AddKeyBoardINput(0x57);

            }
            else if (keybtn.Name == "CmdE")
            {
                AddKeyBoardINput(0X45);

            }
            else if (keybtn.Name == "CmdR")
            {
                AddKeyBoardINput(0X52);

            }
            else if (keybtn.Name == "CmdT")
            {
                AddKeyBoardINput(0X54);

            }
            else if (keybtn.Name == "CmdY")
            {
                AddKeyBoardINput(0X59);

            }
            else if (keybtn.Name == "CmdU")
            {
                AddKeyBoardINput(0X55);

            }
            else if (keybtn.Name == "CmdI")
            {
                AddKeyBoardINput(0X49);

            }
            else if (keybtn.Name == "CmdO")
            {
                AddKeyBoardINput(0X4F);
            }
            else if (keybtn.Name == "CmdP")
            {
                AddKeyBoardINput(0X50);
            }
            else if (keybtn.Name == "CmdOpenCrulyBrace")
            {
                addNumkeyINput(0xdb);
            }
            else if (keybtn.Name == "CmdEndCrultBrace")
            {
                addNumkeyINput(0xdd);
            }
            else if (keybtn.Name == "CmdOR")
            {
                addNumkeyINput(0xdc);
            }
            #endregion
            #region///Third ROw

            else if (keybtn.Name == "CmdCapsLock")//caps lock
            {
                AddKeyBoardINput(0x14);
                if (checkImage.Visibility != Visibility.Visible)
                {
                    checkImage.Visibility = Visibility.Visible;
                }
                else
                {
                    checkImage.Visibility = Visibility.Hidden;
                }
            }
            else if (keybtn.Name == "CmdA")
            {
                AddKeyBoardINput(0x41);
            }
            else if (keybtn.Name == "CmdS")
            {
                AddKeyBoardINput(0x53);
            }
            else if (keybtn.Name == "CmdD")
            {
                AddKeyBoardINput(0x44);
            }
            else if (keybtn.Name == "CmdF")
            {
                AddKeyBoardINput(0x46);
            }
            else if (keybtn.Name == "CmdG")
            {
                AddKeyBoardINput(0x47);
            }
            else if (keybtn.Name == "CmdH")
            {
                AddKeyBoardINput(0x48);
            }
            else if (keybtn.Name == "CmdJ")
            {
                AddKeyBoardINput(0x4A);
            }
            else if (keybtn.Name == "CmdK")
            {
                AddKeyBoardINput(0X4B);
            }
            else if (keybtn.Name == "CmdL")
            {
                AddKeyBoardINput(0X4C);

            }
            else if (keybtn.Name == "CmdColon")//;:
            {
                addNumkeyINput(0xba);
            }
            else if (keybtn.Name == "CmdDoubleInvertedComma")//'"
            {
                addNumkeyINput(0xde);
            }
            else if (keybtn.Name == "CmdEnter")
            {
                AddKeyBoardINput(0x0d);
            }
            #endregion
            #region//Fourth Row
            else if (keybtn.Name == "CmdShift" || keybtn.Name == "CmdlShift")
            {
                if (CtrlFlag)
                {
                    CtrlFlag = false;
                    ShiftFlag = false;
                    changeInput();
                }
                else
                {
                    ShiftFlag = true;
                }
            }
            else if (keybtn.Name == "CmdZ")
            {

                AddKeyBoardINput(0X5A);

            }
            else if (keybtn.Name == "CmdX")
            {
                AddKeyBoardINput(0X58);

            }
            else if (keybtn.Name == "CmdC")
            {
                AddKeyBoardINput(0X43);

            }
            else if (keybtn.Name == "CmdV")
            {
                AddKeyBoardINput(0X56);

            }
            else if (keybtn.Name == "CmdB")
            {
                AddKeyBoardINput(0X42);

            }
            else if (keybtn.Name == "CmdN")
            {
                AddKeyBoardINput(0x4E);

            }
            else if (keybtn.Name == "CmdM")
            {
                AddKeyBoardINput(0x4D);
            }
            else if (keybtn.Name == "CmdLessThan")//<,
            {
                addNumkeyINput(0xbc);
            }
            else if (keybtn.Name == "CmdGreaterThan")//>.
            {
                addNumkeyINput(0xbe);
            }
            else if (keybtn.Name == "CmdQuestion")//?/
            {
                addNumkeyINput(0xbf);
            }

            else if (keybtn.Name == "CmdSpaceBar")
            {
                AddKeyBoardINput(0x20);
            }
            #endregion
            #region//Last row
            else if (keybtn.Name == "CmdCtrl" || keybtn.Name == "CmdlCtrl")//ctrl
            {
                if (ShiftFlag)
                {
                    ShiftFlag = false;
                    CtrlFlag = false;
                }
                else
                {
                    CtrlFlag = true;
                }
            }
            else if (keybtn.Name == "CmdpageUp")
            {
                AddKeyBoardINput(0x21);
            }
            else if (keybtn.Name == "CmdpageDown")
            {
                AddKeyBoardINput(0x22);
            }
            else if (keybtn.Name == "CmdClose")//关闭键盘
            {
                this.Close();
            }
            else if(keybtn.Name == "CmdTypeWriting")
            {
                changeInput();
            }

            #endregion
        }
     
        //切换输入法
        private void changeInput()
        {
            keybd_event(0x11, 0, 0, 0);
            keybd_event(0x10, 0, 0, 0);
            keybd_event(0x11, 0, 0x02, 0);
            keybd_event(0x10, 0, 0x02, 0);
        }

        private static void addNumkeyINput(byte input)
        {
            if (CtrlFlag)
            {
                CtrlFlag = false;
                ShiftFlag = false;
                keybd_event(input, 0, 0, 0);
                keybd_event(input, 0, 0x02, 0);
            }
            else
            {
                if (!ShiftFlag)
                {
                    keybd_event(input, 0, 0, 0);
                    keybd_event(input, 0, 0x02, 0);
                }
                else
                {
                    keybd_event(0x10, 0, 0, 0);//shift
                    keybd_event(input, 0, 0, 0);
                    keybd_event(input, 0, 0x02, 0);
                    keybd_event(0x10, 0, 0x02, 0);

                    ShiftFlag = false;
                }
            }
        }
        private static void AddKeyBoardINput(byte input)
        {

            if (CtrlFlag)
            {
                CtrlFlag = false;
                ShiftFlag = false;
                keybd_event(input, 0, 0, 0);
                keybd_event(input, 0, 0x02, 0);
            }
            else
            {
                if (ShiftFlag)
                {
                    keybd_event(input, 0, 0, 0);
                    keybd_event(input, 0, 0x02, 0);
                    ShiftFlag = false;
                }
                else
                {
                    keybd_event(input, 0, 0, 0);
                    keybd_event(input, 0, 0x02, 0);
                }
            }
        }

        public struct GUITHREADINFO
        {
            public int cbSize;
            public int flags;
            public int hwndActive;
            public int hwndFocus;
            public int hwndCapture;
            public int hwndMenuOwner;
            public int hwndMoveSize;
            public int hwndCaret;
            public System.Drawing.Rectangle rcCaret;
        }

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);

        [DllImport("user32")]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32", SetLastError = true)]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            if (hwndSource != null)
                hwndSource.AddHook(new HwndSourceHook(this.WndProc));
        }
    }
虚拟软件盘源码
阿磊ing的主页 阿磊ing | 初学一级 | 园豆:2
提问于:2017-01-10 13:25
< >
分享
所有回答(1)
0

1.处理焦点(无焦点状态);

2.模拟键盘(可模拟窗口消息,也可修改虚拟键盘)

花飘水流兮 | 园豆:13571 (专家六级) | 2017-01-10 18:00

不懂,能否详细说说

支持(0) 反对(0) 阿磊ing | 园豆:2 (初学一级) | 2017-01-10 22:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册