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
{ "`",},
{ "",},
{ "",},
{ "",},
{ "",},
{ "",},
{ "",},
{ "",},
{ "",},
{ "",},
{ "",},
{ "-",},
{ "=",},
{ "Delete",},
{ "~",},
{ "!",},
{ "@",},
{ "#",},
{ "$",},
{ "%",},
{ "^",},
{ "&",},
{ "*",},
{ "(",},
{ ")",},
{ "_",},
{ "+",},
#endregion #region row2
{ "Tab",},
{ "q", },
{ "w", },
{ "e", },
{ "r", },
{ "t", },
{ "y", },
{ "u", },
{ "i", },
{ "o", },
{ "p", },
{ "[", },
{ "]", },
{ @"\", },
{ "Q", },
{ "W", },
{ "E", },
{ "R", },
{ "T", },
{ "Y", },
{ "U", },
{ "I", },
{ "O", },
{ "P", },
{ "{", },
{ "}", },
{ "|", },
#endregion #region row3
{ "a",},
{ "s", },
{ "d", },
{ "f", },
{ "g", },
{ "h", },
{ "j", },
{ "k", },
{ "l", },
{ ";", },
{ "'", },
{ "Enter", },
{ "A",},
{ "S", },
{ "D", },
{ "F", },
{ "G", },
{ "H", },
{ "J", },
{ "K", },
{ "L", },
{ ":", },
{ "\"", }, #endregion #region row4
{ "z",},
{ "x", },
{ "c", },
{ "v", },
{ "b", },
{ "n", },
{ "m", },
{ ",", },
{ ".", },
{ "/", },
{ "Z",},
{ "X", },
{ "C", },
{ "V", },
{ "B", },
{ "N", },
{ "M", },
{ "<", },
{ ">", },
{ "?", }, #endregion #region other
{"Space", }, #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, , , );
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
} } protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == CapsProperty)
{
//按下
keybd_event(, , , );
//抬起
keybd_event(, , , );
}
else if(e.Property==ShiftProperty)
{
if (Shift)
{
//按下
keybd_event(, , , );
}
else
{
//抬起
keybd_event(, , , );
}
}
else if (e.Property == CtrlProperty)
{
if (Ctrl)
{
//按下
keybd_event(, , , );
}
else
{
//抬起
keybd_event(, , , );
}
}
else if (e.Property == AltProperty)
{
if (Alt)
{
//按下
keybd_event(, , , );
}
else
{
//抬起
keybd_event(, , , );
}
}
}
} 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>()
{
{"`","~" },
{"","!" },
{"","@" },
{"","#" },
{"","$" },
{"","%" },
{"","^" },
{"","&" },
{"","*" },
{"","(" },
{"",")" },
{"-","_" },
{"=","+" },
{"[","{" },
{"]","}" },
{";",":" },
{"'","\"" },
{",","<" },
{".",">" },
{"/","?" },
{@"\","|" },
}; 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 = -; 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();
}
}
}

所有的代码都已经写在这里了,就不单独上传代码了。

WPF实现软键盘的更多相关文章

  1. Win8环境WPF打开和关闭软键盘

    代码如下: public class KeyBoardHelper { #region 键盘控制 /// <summary> /// 显示键盘 /// </summary> p ...

  2. Android中点击隐藏软键盘最佳方法——Android开发之路4

    Android中点击隐藏软键盘最佳方法 实现功能:点击EditText,软键盘出现并且不会隐藏,点击或者触摸EditText以外的其他任何区域,软键盘被隐藏: 1.重写dispatchTouchEve ...

  3. Android之弹出/隐藏系统软键盘

    Android弹出/隐藏系统软键盘的代码如下: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT ...

  4. EditText获取和失去焦点,软键盘的关闭,和软键盘的显示和隐藏的监听

    软键盘显示和隐藏的监听: 注: mReplayRelativeLayout是EditText的父布局 //监听软键盘是否显示或隐藏 mReplayRelativeLayout.getViewTreeO ...

  5. Android -- 软键盘

    1. 应用启动后,自动打开软键盘 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD ...

  6. form表单提交和ajax表单提交,关于移动端如何通过软键盘上的【搜索】和【前进】进行提交操作

    [文章来源]由于自己对于form研究甚少,所以一直用的都是AJAX进行提交,这次后台提出要用form提交,顺便深入研究一下:之前在做表单的时候,发现input可以通过设置不同的type属性,调用不同的 ...

  7. iOS 点击TextField不弹出软键盘的解决方案

    开发中遇到: 在模拟器里面,textfield可以通过电脑键盘输入,可是怎么也不会自动弹出模拟器软键盘 解决方案: 切换一下键盘,command+shift+k,Xcode6.3 中只能是一种输入源

  8. (原)android的alertdialog中加入edittext但是不弹出软键盘等问题的解决与原因

    摘要:alertdialog中加入edittext但是不弹出软键盘等问题网上有很多不管用的解决方案, 本文意在给出更有效的解决办法,并初步探究其原因 正文 在对话框中插入文本框是十分常见的需求 通常我 ...

  9. edittext把软键盘上的回车键改为搜索、发送或者 下一步,窗口随软键盘弹出而改变。

    http://m.blog.csdn.net/article/details?id=51350501 以上博文讲解很详细. 如图所示,有时候为了布局美观,在搜索时没有搜索按钮,而是调用软件盘上的按钮. ...

随机推荐

  1. PLAY2.6-SCALA(七) Streaming HTTP response

    1.从HTTP1.1开始,服务端为了在single connection下对HTTP请求及响应提供服务,需要在response中提供响应的Content-Length. 默认情况下,不需要显示的指明C ...

  2. Laravel请求和输入

    该篇文章主要介绍Laravel获取用户请求和输入信息的方法.获取基本输入信息: //获取输入数据,不用担心所使用的HTTP方法 $id = Input::get('id'); //可以指定默认值 $i ...

  3. 2019-4-7-VisualStudio-解决方案筛选器-slnf-文件

    title author date CreateTime categories VisualStudio 解决方案筛选器 slnf 文件 lindexi 2019-04-07 11:34:59 +08 ...

  4. Java练习 SDUT-2749_区域内点的个数

    区域内点的个数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description X晚上睡不着的时候不喜欢玩手机,也不喜欢打游戏,他喜欢数 ...

  5. 18.libgdx制作预览图,背景移动循环,改变地图颜色

    经过构思,游戏将分为两部分, 1,预览图,只负责展示世界形势 2,根据预览图生成的战役项 现在要记录的是我制作预览图的部分 1.预览图只有实际地图的1/4,首先生成地图(建议不要缩放以前地图,由于误差 ...

  6. Gym-101623H_High Score

    题意:t组数据,每组数据有abcd四个数,其中d可以加到abc任意一个数上(d可以拆分),求公式a^2 + b^2 + c^2 + 7 * min(a,b,c)的最大值. 题解:首先明确一点,平方的增 ...

  7. qt 自定义窗口绘制正弦曲线

    circlewidget.h #ifndef CIRCLAWIDGET_H #define CIRCLAWIDGET_H #include <QFrame> #include<QVe ...

  8. Flask学习之七 单元测试

    英文博客地址:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-unit-testing 中文翻译地址:http ...

  9. 使用epoll实现简单的服务器

    1. 头文件 #ifndef __TCP_SERVER_H__ #define __TCP_SERVER_H__ #include <unistd.h> #include <stdi ...

  10. Python语言的缺点