zoukankan      html  css  js  c++  java
  • WPF 虚拟键盘

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

    现将代码分享如下:

    按键布局如下:

       <Button Name="CmdTlide" Focusable="False" Click="Button_Click"  Style="{StaticResource InformButton}"  VerticalContentAlignment="Top" HorizontalContentAlignment="Left"  Width="60" Height="60">
                                        <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
                                            <TextBlock    HorizontalAlignment="Left" VerticalAlignment="Top"   ><Run Text="~"/></TextBlock>
                                            <TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom"   ><Run Text="'"/></TextBlock>
                                        </StackPanel>
                                    </Button>

    点击事件:

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

    源码下载

  • 相关阅读:
    Oracle基本操作汇总
    Oracle客户端+PLSQLDeveloper实现远程登录Oracle数据库
    ASP.NET后台执行JS代码
    ASP.NET 使用AJAX让GridView的数据行显示提示框(ToolTip)
    GridView如何实现双击行进行编辑,更新
    git push后是空目录,且提示modified content, untracked content
    SQL_2_查询Select语句的使用
    selenium2自动处理验证码
    jenkins配置邮箱时出错
    jenkins匿名用户登录
  • 原文地址:https://www.cnblogs.com/xiamojinnian/p/4284102.html
Copyright © 2011-2022 走看看