WPF之路-键盘与鼠标事件 - 简书

键盘事件
事件类型分为以下几个类型
- 生命周期事件:在元素加载与卸载的时候发生
- 鼠标事件:鼠标动作
- 键盘事件:键盘动作
- 手写笔事件:适用于win7以上的系统
- 多点触控事件:一个手指或多个手指的触控动作
键盘事件
键盘事件的执行顺序:
PrevieKeyDown
KeyDown
PreviewTextInput
TextInput
PreviewKeyUp
KeyUp
下面以实例代码证实:
在TextBox中分别添加PreviewKeyDown/KeyDown/PreviewTextInput/PreviewKeyUp/KeyUp/TextChanged事件
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<!--将容器分为三行-->
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<!--添加一个停靠容器-->
<DockPanel Margin="5">
<TextBlock >请输入文本:</TextBlock>
<!--Focusable="True" TabIndex="0" 表示是否开启焦点,和设置tab切换焦点的顺序 -->
<TextBox Name="txt_input"
PreviewKeyDown="txt_input_KeyEnvent"
KeyDown="txt_input_KeyEnvent"
PreviewTextInput="txt_input_TextInput"
PreviewKeyUp="txt_input_KeyEnvent"
KeyUp="txt_input_KeyEnvent"
TextChanged="txt_input_TextChanged" >
</TextBox>
</DockPanel>
<!--内容展示与内容清除-->
<ListBox Name="list_box" Margin="5" Grid.Row="1"></ListBox>
<Button Name="btn_clear" Content="清空内容" Grid.Row="2" Margin="5" Padding="3" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="Auto" Click="btn_clear_Click"/>
</Grid>
</Window>
事件对应自带程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//清空ListBox的内容
private void btn_clear_Click(object sender, RoutedEventArgs e)
{
this.list_box.Items.Clear();
}
//文本框按下事件
private void txt_input_KeyEnvent(object sender, KeyEventArgs e)
{
string message = "事件:"+e.RoutedEvent+"-键:"+e.Key;
this.list_box.Items.Add(message);
}
// 文本输入事件
private void txt_input_TextInput(object sender, TextCompositionEventArgs e)
{
string message = "事件:" + e.RoutedEvent + "-值:" + e.Text;
this.list_box.Items.Add(message);
}
private void txt_input_TextChanged(object sender, TextChangedEventArgs e)
{
string message = "事件:" + e.RoutedEvent ;
this.list_box.Items.Add(message);
}
}
}
运行程序,查看结果:
在文本框中输入"s",从ListBox中的输出可以看出,先执行的是“按下事件”,然后是“文本输入事件”,然后是“键的释放事件”,对应的隧道事件总先于对应的冒泡事件
以上示例有助于理解隧道事件与冒泡事件、还有键盘事件
鼠标事件
鼠标单击
捕获鼠标
鼠标拖放
鼠标事件有:
- MouseEnter事件
- MouseLeave事件,这两个事件都是直接事件
- PreviewMouseMove事件
- MouseMove事件,这两个会提供MouseEventArgs属性
鼠标移动事件MouseMove
在界面定义一个矩形,在该矩形上绑定鼠标移动事件,将鼠标的坐标点里时时的显示在一个TextBox中,这样,便能清晰的看出鼠标移动事件的作用
<Window x:Class="WpfApp1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="450" Width="800">
<Grid Margin="5">
<!--定义三行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<!--定义一个浅蓝色的矩形,并绑定鼠标移动事件-->
<Rectangle Name="rect" Fill="LightBlue" MouseMove="Event_MouseMove"></Rectangle>
<Button Grid.Row="1" Name="cmdCallpture">捕获鼠标</Button>
<TextBlock Name="lblInfo" Grid.Row="2"></TextBlock>
</Grid>
</Window>
鼠标移动事件处理程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
//鼠标移动事件处理程序
private void Event_MouseMove(object sender, MouseEventArgs e)
{
//GetPosition返回相对指定元素的鼠标位置
Point pt = e.GetPosition(this);
//移动鼠标的时候,将鼠标的坐标显示出来
this.lblInfo.Text = ("You are at ("+pt.X+","+pt.Y+") in window coordinates");
}
}
}
运行效果:
鼠标的单击事件
PreviewMouseLeftButtonDown
PreviewMouseRightButtonDown
MouseLeftButtonDown
MouseRightButtonDown
PreviewMouseLeftButtonUp
PreviewMouseRightButtonUp
MouseLeftButtonUp
MouseRightButtonUp
捕获鼠标
当鼠标被捕获以后,鼠标便不能再点击容器的其它元素
<Window x:Class="WpfApp1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="450" Width="800">
<Grid Margin="5">
<!--定义三行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<!--定义一个浅蓝色的矩形,并绑定鼠标移动事件-->
<Rectangle Name="rect" Fill="LightBlue" MouseMove="Event_MouseMove"></Rectangle>
<!--为按钮绑定单击事件,用来捕获鼠标-->
<Button Grid.Row="1" Name="cmdCapture" Click="Event_Click">捕获鼠标</Button>
<TextBlock Name="lblInfo" Grid.Row="2"></TextBlock>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Event_MouseMove(object sender, MouseEventArgs e)
{
//GetPosition返回相对指定元素的鼠标位置
Point pt = e.GetPosition(this);
//移动鼠标的时候,将鼠标的坐标显示出来
this.lblInfo.Text = ("You are at ("+pt.X+","+pt.Y+") in window coordinates");
}
//单击事件处理程序
private void Event_Click(object sender, RoutedEventArgs e)
{
//Mouse.Capture()将鼠标输入捕获到指定元素
//当鼠标被捕获以后,鼠标便无法再点击窗口上其他元素
Mouse.Capture(this.rect);
this.cmdCapture.Content = "鼠标已经被捕获";
}
}
}
如图,当鼠标被捕获以后,点击窗口的最大化最小化都无法工作了
鼠标拖动
鼠标的拖动指将鼠标从一个元素上按下鼠标左键,然后拖动到另一个元素上,释放鼠标左键,从而将相应的内容移动到第二个元素上去
TextBox控件默认支持鼠标拖动,请看示例:
<Window x:Class="WPF_CODE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Name="txt_1" Grid.Column="0" Width="Auto" HorizontalAlignment="Center" VerticalAlignment="Center">TextBox Hello</TextBox>
<!--AllowDrop属性允许自身成为拖动源的目标-->
<Label Name="lab_fir" AllowDrop="True" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" MouseDown="lab_fir_MouseDown">Lable First</Label>
<Label Name="lab_sec" AllowDrop="True" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" Drop="lab_sec_Drop">Lable Second</Label>
</Grid>
</Window>
后端代码:
using System;
using System.Collections.Generic;
using System.Linq;
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.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_CODE
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//第一个lable,当鼠标按下时,启动拖动效果
private void lab_fir_MouseDown(object sender, MouseButtonEventArgs e)
{
//获取事件激发者,即第一个lable
Label lb = (Label)sender;
//DragDrop.DoDragDrop指启动拖放操作
//参数分别是启动拖动的对象,要操作的类型,拖动类型
DragDrop.DoDragDrop(lb, lb.Content, DragDropEffects.Move);
}
//第二个lable,当拖拽事件在它身上发生时
private void lab_sec_Drop(object sender, DragEventArgs e)
{
//从DragEventArgs里获取到拖拽的内容进行加载
((Label)sender).Content = e.Data.GetData(DataFormats.Text);
}
}
}
效果是:
文本框的内容可以拖拽到第一个和第二个lable上
第一个lable的内容可以拖拽到文本框和第二个lable上
第二个lable可以拖拽到文本框上
WPF之路-键盘与鼠标事件 - 简书的更多相关文章
- WPF嵌入Unity3D之后,unity3D程序的键盘和鼠标事件无法触发(3D程序的焦点无法激活)的解决方案
目前最通用的客户端调用3D的方式,就是WPF程序通过Process启动Unity3D的exe进程,直接上代码: //开启3D进程 internal void Create3DProcess(strin ...
- winform中键盘和鼠标事件的捕捉和重写(转)
在 编写winform应用程序时,有时需要无论在哪个控件获取焦点时,对某一个键盘输入或者鼠标事件都进行同样的操作.比如编写一个处理图片的应用程序时, 希望无论当前哪个控件获得焦点,当用户按上.下.左. ...
- winform中键盘和鼠标事件的捕捉和重写
在编写winform应用程序时,有时需要无论在哪个控件获取焦点时,对某一个键盘输入或者鼠标事件都进行同样的操作.比如编写一个处理图片的应用程序时,希望无论当前哪个控件获得焦点,当用户按上.下.左.右键 ...
- cocos2d-x 键盘和鼠标事件
出了菜单可以响应用户事件外,cocos2d中的层(Layer)也可以响应事件.层能够自动响应窗口事件,这些事件主要是键盘和鼠标事件,cocos2d中事件处理是通过Pyglet的事件处理完成的. 1.键 ...
- WPF使用IDataErrorInfo接口进行数据校验 - 简书
原文:WPF使用IDataErrorInfo接口进行数据校验 - 简书 class ValidationBindableBase : BindableBase, IDataErrorInfo { pu ...
- Selenium2+python自动化12-操作元素(键盘和鼠标事件)
前言 在前面的几篇中重点介绍了一些元素的到位方法,到位到元素后,接下来就是需要操作元素了.本篇总结了web页面常用的一些操作元素方法,可以统称为行为事件 有些web界面的选项菜单需要鼠标悬停在某个元素 ...
- WPF 中模拟键盘和鼠标操作
转载:http://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html 更多经典文章:http://www.qqpjzb.cn/65015.ht ...
- 自动化测试-8.selenium操作元素之键盘和鼠标事件
前言 在前面的几篇中重点介绍了一些元素的定位方法,定位到元素后,接下来就是需要操作元素了.本篇总结了web页面常用的一些操作元素方法,可以统称为行为事件 有些web界面的选项菜单需要鼠标悬停在某个元素 ...
- tkinter 对键盘和鼠标事件的处理
鼠标事件 <ButtonPress-n> <Button-n> <n> 鼠标按钮n被按下,n为1左键,2中键,3右键 <ButtonRelease-n> ...
随机推荐
- 基础知识:Promise(整理)
基础知识:Promise(整理) (来自牛客网)下面关于promise的说法中,错误的是(D) A. resolve和reject都是直接生成一个进入相应状态的promise对象,其参数就是进入相应状 ...
- 0-创建scott数据
CREATE TABLE dept ( deptno INT PRIMARY KEY, dname VARCHAR(14), loc VARCHAR(13) ); INSERT INTO d ...
- python with原型
@Python 的 with 语句详解 这篇文章主要介绍了Python 的 with 语句,本文详细讲解了with语句.with语句的历史.with语句的使用例子等,需要的朋友可以参考下 一. ...
- Python读取Json字典写入Excel表格的方法
需求: 因需要将一json文件中大量的信息填入一固定格式的Excel表格,单纯的复制粘贴肯定也能完成,但是想偷懒一下,于是借助Python解决问题. 环境: Windows7 +Python2.7 + ...
- 【bbs】login.php
require的路径 整体结构的复用 渐变效果的加入 按钮:实现背景透明,文字不透明:在background-color中使用rgba,标准浏览器中,背景透明,文字不透明background-colo ...
- ant design 修改tab样式
.ant-tabs-ink-bar{ background-color: transparent !important; } .ant-tabs-top .ant-tabs-ink-bar-anima ...
- CSS动画总结效果
CSS3添加了几个动画效果的属性,通过设置这些属性,可以做出一些简单的动画效果而不需要再去借助JavaScript.CSS3动画的属性主要分为三类:transform.transition以及a ...
- NMON监控linux性能
NMON监控linux性能 一.下载nmon压缩包,下载地址:http://download.csdn.net/download/fhqsse220/6699865 二.安装方法: 1.将nmon_l ...
- ovs的卸载删除
参考博客1 参考博客2 需要更新.重装ovs时需要先卸载. 删除网桥.端口 ovs-vsctl del-br 交换机名 ovs-vsctl del-port 交换机名 端口名(网卡名) 关闭服务 su ...
- 如何处理Entity Framework / Entity Framework Core中的DbUpdateConcurrencyException异常(转载)
1. Concurrency的作用 场景有个修改用户的页面功能,我们有一条数据User, ID是1的这个User的年龄是20, 性别是female(数据库中的原始数据)正确的该User的年龄是25, ...