wpf 实现一个软键盘,
先发个图:
工作有需要实现一个软键盘,本来想用windows自带的软键盘凑合凑合得了,又觉得那个软键盘太大了,所以自己实现了一个。
说一下实现的思路,其实没什么思路 界面就是都由按钮实现,按照键盘的格式布的局。下面放软键盘控件的代码
前台:
<UserControl x:Class="softkeyboard.Controls.SoftKeyBoard" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:softkeyboard.Controls" xmlns:system="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" d:DesignHeight="190" d:DesignWidth="540"> <UserControl.Resources> <SolidColorBrush x:Key="background" Color="#E8E8E8"></SolidColorBrush> <SolidColorBrush x:Key="keybackground" Color="#FEFEFE"></SolidColorBrush> <SolidColorBrush x:Key="press" Color="#FFCD43"></SolidColorBrush> <local:UperConvert x:Key="UperConvert"></local:UperConvert> <local:ShiftConvert x:Key="ShiftConvert"></local:ShiftConvert> <system:String x:Key="eq">=</system:String> <system:String x:Key="gang"></system:String> <system:String x:Key="yinhao">'</system:String> <system:String x:Key="douhao">,</system:String> <Style x:Key="keyrow" TargetType="{x:Type StackPanel}"> <Setter Property="Orientation" Value="Horizontal"></Setter> <Setter Property="VerticalAlignment" Value="Center"></Setter> <Setter Property="HorizontalAlignment" Value="Left"></Setter> </Style> <Style x:Key="button" TargetType="{x:Type Button}"> <Setter Property="Width" Value="30"></Setter> <Setter Property="Height" Value="29"></Setter> <Setter Property="Margin" Value="3,1"></Setter> <Setter Property="Background" Value="{StaticResource keybackground}"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="PART_border" CornerRadius="4" Background="{TemplateBinding Background}"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}"></TextBlock> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="{StaticResource press}"></Setter> </Trigger> </Style.Triggers> </Style> <Style x:Key="toggle" TargetType="{x:Type ToggleButton}"> <Setter Property="Width" Value="30"></Setter> <Setter Property="Height" Value="29"></Setter> <Setter Property="Margin" Value="3,1"></Setter> <Setter Property="Background" Value="{StaticResource keybackground}"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Border x:Name="PART_border" CornerRadius="4" Background="{TemplateBinding Background}"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}"></TextBlock> </StackPanel> </Border> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="PART_border" Property="Background" Value="{StaticResource press}"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="{StaticResource press}"></Setter> </Trigger> </Style.Triggers> </Style> </UserControl.Resources> <Border CornerRadius="5" Background="{StaticResource background}"> <Grid Margin="2"> <Grid.RowDefinitions> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <!--row1--> <StackPanel Grid.Row="0" Style="{StaticResource keyrow}"> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=`}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=1}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=2}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=3}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=4}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=5}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=6}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=7}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=8}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=9}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=0}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=-}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter={StaticResource eq}}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Width="60" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}">Delete</Button> </StackPanel> <!--row2--> <StackPanel Grid.Row="1" Style="{StaticResource keyrow}"> <Button Style="{StaticResource button}" Width="45" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}">Tab</Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=q}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=w}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=e}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=r}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=t}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=y}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=u}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=i}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=o}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=p}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=[}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=]}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Width="45" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter={StaticResource gang}}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> </StackPanel> <!--row3--> <StackPanel Grid.Row="2" Style="{StaticResource keyrow}"> <ToggleButton x:Name="caps" Style="{StaticResource toggle}" Width="64" Content="Caps lock" IsChecked="{Binding Caps,Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}"></ToggleButton> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=a}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=s}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=d}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=f}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=g}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=h}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=j}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=k}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=l}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=;}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter={StaticResource yinhao}}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> </StackPanel> <Button Grid.Row="2" Grid.RowSpan="3" Height="105" HorizontalAlignment="Right" Style="{StaticResource button}" Width="65" Content="Enter" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <!--row4--> <StackPanel Grid.Row="3" Style="{StaticResource keyrow}"> <ToggleButton x:Name="shift" Style="{StaticResource toggle}" Width="82" IsChecked="{Binding Shift,Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}">Shift</ToggleButton> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=z}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=x}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=c}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=v}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=b}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=n}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=caps,Path=IsChecked,Converter={StaticResource UperConvert},ConverterParameter=m}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter={StaticResource douhao}}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=.}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <Button Style="{StaticResource button}" Content="{Binding ElementName=shift,Path=IsChecked,Converter={StaticResource ShiftConvert},ConverterParameter=/}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}"></Button> <!--<ToggleButton Style="{StaticResource toggle}" Width="80" >Shift</ToggleButton>--> </StackPanel> <!--row5--> <StackPanel Grid.Row="4" Style="{StaticResource keyrow}"> <ToggleButton x:Name="ctrl" Style="{StaticResource toggle}" IsChecked="{Binding Ctrl,Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}">Ctrl</ToggleButton> <!--<Button Style="{StaticResource button}" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}">Win</Button>--> <ToggleButton x:Name="alt" Style="{StaticResource toggle}" IsChecked="{Binding Alt,Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}">Alt</ToggleButton> <Button Style="{StaticResource button}" Width="388" Command="{Binding KeyCommand,RelativeSource={RelativeSource AncestorType={x:Type local:SoftKeyBoard}}}" CommandParameter="{Binding Path=Content,RelativeSource={RelativeSource Self}}">Space</Button> <!--<ToggleButton Style="{StaticResource toggle}" >Alt</ToggleButton> <ToggleButton Style="{StaticResource toggle}" >Ctrl</ToggleButton>--> </StackPanel> </Grid> </Border> </UserControl>
后台:
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using UserControl = System.Windows.Controls.UserControl; namespace softkeyboard.Controls { /// <summary> /// SoftKeyBoard.xaml 的交互逻辑 /// </summary> public partial class SoftKeyBoard : UserControl { /// <summary> /// 导入模拟键盘的方法 /// </summary> /// <param name="bVk" >按键的虚拟键值</param> /// <param name= "bScan" >扫描码,一般不用设置,用0代替就行</param> /// <param name= "dwFlags" >选项标志:0:表示按下,2:表示松开</param> /// <param name= "dwExtraInfo">一般设置为0</param> [DllImport("User32.dll")] public static extern void keybd_event(byte bVK, byte bScan, Int32 dwFlags, int dwExtraInfo); #region public static readonly DependencyProperty CapsProperty = DependencyProperty.Register( "Caps", typeof(bool), typeof(SoftKeyBoard), new PropertyMetadata(default(bool))); public bool Caps { get { return (bool)GetValue(CapsProperty); } set { SetValue(CapsProperty, value); } } public static readonly DependencyProperty CtrlProperty = DependencyProperty.Register( "Ctrl", typeof(bool), typeof(SoftKeyBoard), new PropertyMetadata(default(bool))); public bool Ctrl { get { return (bool)GetValue(CtrlProperty); } set { SetValue(CtrlProperty, value); } } public static readonly DependencyProperty AltProperty = DependencyProperty.Register( "Alt", typeof(bool), typeof(SoftKeyBoard), new PropertyMetadata(default(bool))); public bool Alt { get { return (bool)GetValue(AltProperty); } set { SetValue(AltProperty, value); } } public static readonly DependencyProperty ShiftProperty = DependencyProperty.Register( "Shift", typeof(bool), typeof(SoftKeyBoard), new PropertyMetadata(default(bool))); public bool Shift { get { return (bool)GetValue(ShiftProperty); } set { SetValue(ShiftProperty, value); } } #endregion public ICommand KeyCommand { get; set; } private Dictionary<string, byte> keys; public SoftKeyBoard() { InitializeComponent(); SetCommand(); CreateKeys(); } private void CreateKeys() { keys = new Dictionary<string, byte>() { #region row1 { "`",192}, { "1",49}, { "2",50}, { "3",51}, { "4",52}, { "5",53}, { "6",54}, { "7",55}, { "8",56}, { "9",57}, { "0",48}, { "-",189}, { "=",187}, { "Delete",8}, { "~",192}, { "!",49}, { "@",50}, { "#",51}, { "$",52}, { "%",53}, { "^",54}, { "&",55}, { "*",56}, { "(",57}, { ")",48}, { "_",189}, { "+",187}, #endregion #region row2 { "Tab",9}, { "q", 81}, { "w", 87}, { "e", 69}, { "r", 82}, { "t", 84}, { "y", 89}, { "u", 85}, { "i", 73}, { "o", 79}, { "p", 80}, { "[", 219}, { "]", 221}, { @"", 220}, { "Q", 81}, { "W", 87}, { "E", 69}, { "R", 82}, { "T", 84}, { "Y", 89}, { "U", 85}, { "I", 73}, { "O", 79}, { "P", 80}, { "{", 219}, { "}", 221}, { "|", 220}, #endregion #region row3 { "a",65}, { "s", 83}, { "d", 68}, { "f", 70}, { "g", 71}, { "h", 72}, { "j", 74}, { "k", 75}, { "l", 73}, { ";", 186}, { "'", 222}, { "Enter", 13}, { "A",65}, { "S", 83}, { "D", 68}, { "F", 70}, { "G", 71}, { "H", 72}, { "J", 74}, { "K", 75}, { "L", 73}, { ":", 186}, { """, 222}, #endregion #region row4 { "z",90}, { "x", 88}, { "c", 67}, { "v", 86}, { "b", 66}, { "n", 78}, { "m", 77}, { ",", 188}, { ".", 190}, { "/", 191}, { "Z",90}, { "X", 88}, { "C", 67}, { "V", 86}, { "B", 66}, { "N", 78}, { "M", 77}, { "<", 188}, { ">", 190}, { "?", 191}, #endregion #region other {"Space",32 }, #endregion }; } private void SetCommand() { KeyCommand = new RoutedCommand(); CommandBinding cbinding = new CommandBinding(KeyCommand, KeyCommand_Excuted, KeyCommand_CanExcute); this.CommandBindings.Add(cbinding); } private void KeyCommand_CanExcute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void KeyCommand_Excuted(object sender, ExecutedRoutedEventArgs e) { if (e.Parameter != null) { string code = e.Parameter.ToString(); if (keys.ContainsKey(code)) { byte b = keys[code]; try { keybd_event(b, 0, 0, 0); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(e); if (e.Property == CapsProperty) { //按下 keybd_event(20, 0, 0, 0); //抬起 keybd_event(20, 0, 2, 0); } else if(e.Property==ShiftProperty) { if (Shift) { //按下 keybd_event(16, 0, 0, 0); } else { //抬起 keybd_event(16, 0, 2, 0); } } else if (e.Property == CtrlProperty) { if (Ctrl) { //按下 keybd_event(17, 0, 0, 0); } else { //抬起 keybd_event(17, 0, 2, 0); } } else if (e.Property == AltProperty) { if (Alt) { //按下 keybd_event(18, 0, 0, 0); } else { //抬起 keybd_event(18, 0, 2, 0); } } } } public class UperConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (parameter == null) { return ""; } if (value == null) { if (parameter != null) { return parameter.ToString(); } else { return ""; } } bool isuper = (bool)value; if (isuper) { return parameter.ToString().ToUpper(); } else { return parameter.ToString().ToLower(); } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } public class ShiftConvert : IValueConverter { Dictionary<string, string> dic = new Dictionary<string, string>() { {"`","~" }, {"1","!" }, {"2","@" }, {"3","#" }, {"4","$" }, {"5","%" }, {"6","^" }, {"7","&" }, {"8","*" }, {"9","(" }, {"0",")" }, {"-","_" }, {"=","+" }, {"[","{" }, {"]","}" }, {";",":" }, {"'",""" }, {",","<" }, {".",">" }, {"/","?" }, {@"","|" }, }; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (parameter == null) { return ""; } if (value == null) { if (parameter != null) { return parameter.ToString(); } else { return ""; } } bool shift = (bool)value; if (shift) { var cond = parameter.ToString(); if (dic.ContainsKey(cond)) { return dic[cond]; } else { return ""; } } else { return parameter.ToString(); } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }
后来发现,把他放在窗口上 要实现使用的方法还需要改点东西,下面放上窗口的代码
前台:
<Window x:Class="softkeyboard.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:softkeyboard" xmlns:controls="clr-namespace:softkeyboard.Controls" mc:Ignorable="d" WindowStyle="None" WindowStartupLocation="Manual" AllowsTransparency="True" Background="Transparent" Title="MainWindow" Height="190" Width="540" MouseLeftButtonDown="MainWindow_OnMouseLeftButtonDown" ShowInTaskbar="True"> <Window.Resources> <SolidColorBrush x:Key="ColorBrush" Color="Red" Opacity="0.7"></SolidColorBrush> <PathGeometry x:Key="closed" Figures="M509.125332 997.359791C236.342166 997.359791 14.419101 775.403955 14.419101 502.620789 14.419101 229.804853 236.342166 7.849017 509.125332 7.849017s494.739002 221.955836 494.739002 494.771772c0 272.783166-221.955836 494.739002-494.739002 494.739002zM726.85375 321.923243l-36.998101-36.965331-180.697547 180.664776-180.697546-180.664776-36.998101 36.965331 180.697546 180.697546-180.697546 180.664776 36.998101 36.998101 180.697546-180.697546 180.697547 180.697546 36.998101-36.998101-180.697546-180.664776 180.697546-180.697546z"></PathGeometry> <Style x:Key="CloseBtn" TargetType="{x:Type Button}"> <Setter Property="Background" Value="{StaticResource ColorBrush}"></Setter> <Setter Property="Foreground" Value="White"></Setter> <Setter Property="FocusVisualStyle" Value="{x:Null}"></Setter> <Setter Property="Cursor" Value="Hand"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="Part_Border" BorderThickness="{TemplateBinding BorderThickness}"> <Grid> <Ellipse Width="Auto" Height="Auto" Stroke="LightGray"> <Ellipse.Fill> <SolidColorBrush Color="Red" Opacity="0.5"/> </Ellipse.Fill> </Ellipse> <TextBlock Text="×" HorizontalAlignment="Center" Foreground="LightGray" VerticalAlignment="Center" FontSize="20" /> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Red"></Setter> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="Red"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Viewbox Margin="0,0,6,0"> <controls:SoftKeyBoard></controls:SoftKeyBoard> </Viewbox> <Button Width="21" Height="21" Style="{StaticResource CloseBtn}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="519,0,0,0" Click="ButtonBase_OnClick"></Button> </Grid> </Window>
后台:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace softkeyboard { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { [DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hWnd, int nindex, IntPtr dwNewLong); [DllImport("user32.dll", SetLastError = true)] public static extern UInt32 GetWindowLong(IntPtr hWnd, int index); public MainWindow() { InitializeComponent(); this.Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this); IntPtr intPtr = windowInteropHelper.Handle; int value = -20; SetWindowLong(intPtr, value, (IntPtr)0x8000000); } private void MainWindow_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { base.DragMove(); } private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { this.Close(); } } }
所有的代码都已经写在这里了,就不单独上传代码了。