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

本文共 8817 字,大约阅读时间需要 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;                }            }        }

 

转载地址:http://lacra.baihongyu.com/

你可能感兴趣的文章
七:Mysql的乐观锁与悲观锁机制
查看>>
CSS滤镜及渐变 (filter样式表属性)
查看>>
调用上面的@InitBinder 解决客户端上传时间参数转换的问题
查看>>
net.sf.json.JSONException: There is a cycle in the hierarchy异常,解决方法
查看>>
Android自动化测试方向
查看>>
QT中常用数据之间转换
查看>>
向量的内积,长度,正交性
查看>>
app包中的fragment和v4包中的fragment的使用的区别
查看>>
Http协议与缓存
查看>>
监测超过特定内存阀值进程并结束
查看>>
Linux Centos 查询信息
查看>>
android adb命令
查看>>
python “双”稀疏矩阵转换为最小联通量“单”矩阵
查看>>
揭秘天猫双11背后:20万商家600万张海报,背后只有一个鹿班
查看>>
重置mysq root密码脚本
查看>>
我的友情链接
查看>>
MHA配置参数
查看>>
深入理解Lock
查看>>
vim的块选择
查看>>
HTML --块
查看>>