原文: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之路-键盘与鼠标事件 - 简书的更多相关文章

  1. WPF嵌入Unity3D之后,unity3D程序的键盘和鼠标事件无法触发(3D程序的焦点无法激活)的解决方案

    目前最通用的客户端调用3D的方式,就是WPF程序通过Process启动Unity3D的exe进程,直接上代码: //开启3D进程 internal void Create3DProcess(strin ...

  2. winform中键盘和鼠标事件的捕捉和重写(转)

    在 编写winform应用程序时,有时需要无论在哪个控件获取焦点时,对某一个键盘输入或者鼠标事件都进行同样的操作.比如编写一个处理图片的应用程序时, 希望无论当前哪个控件获得焦点,当用户按上.下.左. ...

  3. winform中键盘和鼠标事件的捕捉和重写

    在编写winform应用程序时,有时需要无论在哪个控件获取焦点时,对某一个键盘输入或者鼠标事件都进行同样的操作.比如编写一个处理图片的应用程序时,希望无论当前哪个控件获得焦点,当用户按上.下.左.右键 ...

  4. cocos2d-x 键盘和鼠标事件

    出了菜单可以响应用户事件外,cocos2d中的层(Layer)也可以响应事件.层能够自动响应窗口事件,这些事件主要是键盘和鼠标事件,cocos2d中事件处理是通过Pyglet的事件处理完成的. 1.键 ...

  5. WPF使用IDataErrorInfo接口进行数据校验 - 简书

    原文:WPF使用IDataErrorInfo接口进行数据校验 - 简书 class ValidationBindableBase : BindableBase, IDataErrorInfo { pu ...

  6. Selenium2+python自动化12-操作元素(键盘和鼠标事件)

    前言 在前面的几篇中重点介绍了一些元素的到位方法,到位到元素后,接下来就是需要操作元素了.本篇总结了web页面常用的一些操作元素方法,可以统称为行为事件 有些web界面的选项菜单需要鼠标悬停在某个元素 ...

  7. WPF 中模拟键盘和鼠标操作

    转载:http://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html 更多经典文章:http://www.qqpjzb.cn/65015.ht ...

  8. 自动化测试-8.selenium操作元素之键盘和鼠标事件

    前言 在前面的几篇中重点介绍了一些元素的定位方法,定位到元素后,接下来就是需要操作元素了.本篇总结了web页面常用的一些操作元素方法,可以统称为行为事件 有些web界面的选项菜单需要鼠标悬停在某个元素 ...

  9. tkinter 对键盘和鼠标事件的处理

    鼠标事件 <ButtonPress-n> <Button-n> <n> 鼠标按钮n被按下,n为1左键,2中键,3右键 <ButtonRelease-n> ...

随机推荐

  1. Linux 系统性能监控命令详解

    Linux 系统性能监控命令详解 CPU MEMORY IO NETWORK LINUX进程内存占用查看方法 系统负载过重时往往会引起其它子系统的问题,比如:->大量的读入内存的IO请求(pag ...

  2. Linux中安装Nginx

    1.安装编译文件及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel 2.安装PCRE,Ngi ...

  3. BZOJ 1036 树的统计Count 树链剖分模板题

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1036 题目大意: 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将 ...

  4. SOJ 4590 简单模拟

    Description Gandtom把家搬到了一个交通便利的地方.今天来通知他的朋友Sidney,但是Sidney好像不在家,出门了,敲门没有人开门.  于是Gandtom把家里的地址写了下来.他担 ...

  5. 1. Docker基础命令

    本文简要介绍Docker的基础命令,目的在于快速入门Dokcer,Docker的完整命令可以参考Docker官方手册. 0. 安装Docker Docker当前分为企业版(Docker Enterpr ...

  6. 【webpack】理解配置文件

    学习链接: http://blog.csdn.net/hongchh/article/details/55113751 https://segmentfault.com/a/1190000009356 ...

  7. mysql存储过程学习(一)

    转载 什么是存储过程,存储过程的作用及优点  mysql存储过程详细教程  mysql 使用存储过程批量插数据 一.存储过程介绍: 存储过程(Stored Procedure)是一组为了完成特定功能的 ...

  8. flink Window的Timestamps/Watermarks和allowedLateness的区别

    Watermartks是通过additional的时间戳来控制窗口激活的时间,allowedLateness来控制窗口的销毁时间.   注: 因为此特性包括官方文档在1.3-1.5版本均未做改变,所以 ...

  9. 修改Mac系统host文件

    第一步.在终端里面输入 sudo -i 获取临时获取管理员权限,会提示你输入密码,就是启动的密码. 第二步.输入 vi /etc/hosts  前面的vi是编辑器,当然也可以换用其他的,例如上面的na ...

  10. 通达信k线颜色设置

    通达信的k线函数没有颜色选项.如果想要画颜色可以使用STICKLINE函数来覆盖当前k线这样也是可以满足需求. 第一步画针 STICKLINE(条件 , L , H , 0 , 0 ) , 颜色; 第 ...