WPF实现软键盘
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实现软键盘的更多相关文章
- Win8环境WPF打开和关闭软键盘
代码如下: public class KeyBoardHelper { #region 键盘控制 /// <summary> /// 显示键盘 /// </summary> p ...
- Android中点击隐藏软键盘最佳方法——Android开发之路4
Android中点击隐藏软键盘最佳方法 实现功能:点击EditText,软键盘出现并且不会隐藏,点击或者触摸EditText以外的其他任何区域,软键盘被隐藏: 1.重写dispatchTouchEve ...
- Android之弹出/隐藏系统软键盘
Android弹出/隐藏系统软键盘的代码如下: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT ...
- EditText获取和失去焦点,软键盘的关闭,和软键盘的显示和隐藏的监听
软键盘显示和隐藏的监听: 注: mReplayRelativeLayout是EditText的父布局 //监听软键盘是否显示或隐藏 mReplayRelativeLayout.getViewTreeO ...
- Android -- 软键盘
1. 应用启动后,自动打开软键盘 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD ...
- form表单提交和ajax表单提交,关于移动端如何通过软键盘上的【搜索】和【前进】进行提交操作
[文章来源]由于自己对于form研究甚少,所以一直用的都是AJAX进行提交,这次后台提出要用form提交,顺便深入研究一下:之前在做表单的时候,发现input可以通过设置不同的type属性,调用不同的 ...
- iOS 点击TextField不弹出软键盘的解决方案
开发中遇到: 在模拟器里面,textfield可以通过电脑键盘输入,可是怎么也不会自动弹出模拟器软键盘 解决方案: 切换一下键盘,command+shift+k,Xcode6.3 中只能是一种输入源
- (原)android的alertdialog中加入edittext但是不弹出软键盘等问题的解决与原因
摘要:alertdialog中加入edittext但是不弹出软键盘等问题网上有很多不管用的解决方案, 本文意在给出更有效的解决办法,并初步探究其原因 正文 在对话框中插入文本框是十分常见的需求 通常我 ...
- edittext把软键盘上的回车键改为搜索、发送或者 下一步,窗口随软键盘弹出而改变。
http://m.blog.csdn.net/article/details?id=51350501 以上博文讲解很详细. 如图所示,有时候为了布局美观,在搜索时没有搜索按钮,而是调用软件盘上的按钮. ...
随机推荐
- 重磅!容器集群监控利器 阿里云Prometheus 正式免费公测
Prometheus 作为容器生态下集群监控的首选方案,是一套开源的系统监控报警框架.它启发于 Google 的 borgmon 监控系统,并于 2015 年正式发布.2016 年,Prometheu ...
- js函数易犯的错误
1.定义一个js函数时不能写在另一个函数里面. 2.定义一个打开网页自动执行的函数,一定要注意加载的顺序.如果是不是自动执行的,而是等页面加载完后事件触发的,那写在任何地方都没问题. 错误的:
- day18 jQuery,JavaScript高级&Django
回顾: 整体: - HTML - CSS - JavaScript:基本数据类型:流程控制语句 - DOM - BOM:setInterval() - jQuery - 选择器 - 筛选器 - 内容和 ...
- hdu3472 混合图判断欧拉通路
对于欧拉回路,先判断出度入度的差是否为偶数,然后最大流一次. 此题是判断有无欧拉通路,前提要判断图是否连通,然后欧拉通路的条件:要么出入度差没有奇数,或者只有2个点. 所以先统计差为奇数的个数,如果不 ...
- IDEA切换git分支
查看当前所在分支 场景:在多人开发中,需要在主分支的基础上创建一些分支分配给小团队或个人去开发,然后小分支上的小功能开发完毕之后,再merge(合并)到主分支. 1.查看当前所在的分支 下图1.1中是 ...
- 2018-12-24-win10-uwp-求两个矩形相连的几何
title author date CreateTime categories win10 uwp 求两个矩形相连的几何 lindexi 2018-12-24 20:51:49 +0800 2018- ...
- Launch configuration JUnitCore references non-existing project XXX.
- Xcode无法退出,报错提示 The document “xxx.h” could not be saved. The file doesn’t exist.
记录一个问题 场景:Xcode编辑一个工程时直接在工程内部修改了某个目录的文件夹名字,而后删除了其下的某 .h.m 文件 之后总是提示上述错误且无法强制退出Xcode,clean等操作基本没用 查找本 ...
- 第四次C++
继承与派生 一.什么是继承和派生 所谓继承就是从先辈处得到属性和行为特征.类的继承,是新的类从已有类那里得到已有的特性.从另一个角度来看这个问题,从已有类产生新类的过程就是类的派生.类的继承与派生机制 ...
- Libev源码分析03:Libev使用堆管理定时器
Libev中在管理定时器时,使用了堆这种结构,而且除了常见的最小2叉堆之外,它还实现了更高效的4叉堆. 之所以要实现4叉堆,是因为普通2叉堆的缓存效率较低,所谓缓存效率低,也就是说对CPU缓存的利用率 ...