博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF 虚拟键盘
阅读量:6670 次
发布时间:2019-06-25

本文共 8869 字,大约阅读时间需要 29 分钟。

之前做了一款WPF虚拟键盘,调用Win32的API,可以模拟键盘事件。

现将代码分享如下:

按键布局如下:

点击事件:

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;                }                //AddKeyBoardINput(0x11);            }            else if (keybtn.Name == "CmdpageUp")            {                AddKeyBoardINput(0x21);            }            else if (keybtn.Name == "CmdpageDown")            {                AddKeyBoardINput(0x22);            }            else if (keybtn.Name == "CmdClose")//关闭键盘            {                //this.Opacity = 0;                // this.Close();                //  this.keyboard.Visibility = Visibility.Hidden;                //this.keyboard.Focusable = false;                System.Windows.Application.Current.Shutdown();            }            #endregion        }

触发键盘事件:

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;                }            }        }

 

转载于:https://www.cnblogs.com/xiamojinnian/p/4284102.html

你可能感兴趣的文章
jtds连接sqlserver数据库实例的写法
查看>>
Android中的File文件存储及读取file中的Bitmap
查看>>
AngularJS(1)中的5种服务
查看>>
android编译
查看>>
开源|基于TensorFlow的聊天机器人-ErGo
查看>>
lucene4.0入门1
查看>>
Svn结合hook实现自动更新及多Project管理更新
查看>>
Java中sleep()与wait()区别
查看>>
大型网站架构演变和知识体系
查看>>
Java垃圾回收机制浅谈
查看>>
自定义NSOperation 操作
查看>>
字符编码-- Unicode(1991年)
查看>>
【加密解密】阴符,阴书,字验
查看>>
【加密解密】数据加密标准DES加密(Javascript实现)
查看>>
第三十六讲:tapestry表单组件详解之PasswordField
查看>>
Easyui datagrid editor 修改DateBox 返回值格式
查看>>
Mybatis技术原理与实践——读书笔记(五)
查看>>
yum error rpmts_HdrFromFdno: V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
查看>>
Access forbidden!
查看>>
码云五周年 —— 善待你的每一行代码
查看>>