原文: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. Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks

    原文链接:http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips- ...

  2. CSS 预处理器

    在程序员眼里,css不像其他程序语言(例如PHP, Javascript等等),有自己的变量.常量.条件语句以及一些编程语法,它只是一行行单纯的属性描述,写起来相当费事,而且代码难以组织和维护.自然的 ...

  3. Angular2 Router路由相关

    路由设置 Angular中路由的配置应该按照先具体路由到通用路由的设置,因为Angular使用先匹配者优先的原则. 示例: 路由设置如下: export const reportRoute: Rout ...

  4. Web App, Native APP,Hybird App 介绍

    一.Web App 这个主要是采用统一的标准的 HTML,JavaScript.CSS 等 web 技术开发. 用户无需下载,通过不同平台 的浏览器访问来实现跨平台, 同时可以通过浏览器支持充分使用 ...

  5. Java(Android)编程思想笔记03:在Android开发中使用MVP模式

    1. MVP模式简介: MVC模式相信大家肯定是比较熟悉的:M-Model-模型.V-View-视图.C-Controller-控制器. MVP作为MVC的演化版本,那么类似的MVP所对应的意义:M- ...

  6. Java虚拟机11:内存分配原则

    前言 JVM的自动内存管理要自动化的解决两个问题:对象分配内存以及回收分配给对象的内存.对象的内存分配一般是指在堆上分配,少数情况下也可能会直接分配在老年代上,对象主要分配在新生代的Eden 区上,如 ...

  7. [19/04/27-星期六] GOF23_结构型模式(装饰模式、外观模式)

    一.装饰模式(decorator) 职责:动态的为一个对象增加新的功能. 是一种用于代替继承的技术,无须通过继承增加子类就能扩展对象的新功能.使用对象的关联关系代替继承关系,更加灵活,避免类体系的膨胀 ...

  8. [Java123] JDBC and Multi-Threading 多线程编程学习笔记

    项目实际需求:DB交互使用多线程实现 多线程编程基础:1.5  :( (假设总分10) 计划一个半月从头学习梳理Java多线程编程基础以及Oracle数据库交互相关的多线程实现 学习如何通过代码去验证 ...

  9. 【SP2713 GSS4 - Can you answer these queries IV】 题解

    题目链接:https://www.luogu.org/problemnew/show/SP2713 真暴力啊. 开方你开就是了,开上6次就都没了. #include <cmath> #in ...

  10. 查看mysql中所有表的数据记录

    select table_name,table_rows from tables where TABLE_SCHEMA = 'database name' order by table_rows de ...