一、键盘类和键盘事件

wpf提供了基础的键盘类(system.input.keyboard类),该类提供与键盘相关的事件、方法和属性,这些事件、方法和属性提供有关键盘状态的信息。keyboard的事件也通过uielement等xaml基元素类的事件向外提供。

对于键盘操作,其常用的事件有两组:

keydown事件和previewkeydown事件:处理键盘键按下
keyup事件和previewkeyup事件:处理键盘键抬起
其中keydown和keyup事件属于冒泡路由事件,而previewkeydown和previewkeyup属于隧道路由事件。

为了使元素能够接收键盘输入,该元素必须可获得焦点。默认情况下,大多数 uielement 派生对象都可获得焦点。如果不是这样,则要使元素可获得焦点,请将基元素上的 focusable 属性设置为 true。像 stackpanel 和 canvas 这样的 panel 类将 focusable 的默认值设置为 false。因此,对要获取键盘焦点的这些对象而言,必须将 focusable 设置为 true。

例如:在笔者的notebook中有“静音”、“增大音量”、“减小音量”这三个快捷键,在一个应用程序的窗体上处理这三个键的点击可以:

1: <window x:class="inputcommandandfocus.window1"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     title="window1" height="300" width="480" 
   5:     focusable="true" previewkeydown="window_previewkeydown">
   6:     <canvas>
   7:         <!-- ... -->
   8:     </canvas>
   9: </window>
   1: private void window_previewkeydown(object sender, keyeventargs e)
   2: {
   3:     if (e.key == key.volumemute)
   4:     {
   5:         // 按下“静音”键
   6:         txtmessage.text = "mute";
   7:         e.handled = true;
   8:     }
   9:     else if (e.key == key.volumeup)
  10:     {
  11:         // 按下“增大音量”键
  12:         txtmessage.text = "up";
  13:         e.handled = true;
  14:     }
  15:     else if (e.key == key.volumedown)
  16:     {
  17:         // 按下“减小音量”键
  18:         txtmessage.text = "down";
  19:         e.handled = true;
  20:     }
  21: }

 

二、鼠标类和鼠标事件

wpf提供的system.input.mouse类提供与鼠标相关的事件、方法和属性,这些事件、方法和属性提供有关鼠标状态的信息。与keyboard类类似,其事件也通过uielement等基元素向外提供。

其事件主要有以下几组(每个事件均包含xxx冒泡路由事件和previewxxx隧道路由事件)

mousedown、mouseup事件:处理鼠标键的按下与抬起
mouseenter、mouseleave、mousemove:处理鼠标进入、离开控件及在控件上移动
mousewheel:处理鼠标滚轮滚动
另外,对于鼠标位置的捕获,使用mouse类的getposition方法,其参数是一个uielement,表示其鼠标位置基于哪一个控件的坐标系。

例如,对于一个矩形图形,设置其鼠标的各种事件:

1: <rectangle canvas.left="246" canvas.top="46" height="118" 
   2:            name="mainrectangle" stroke="black" width="200" fill="white"
   3:            mouseenter="mainrectangle_mouseenter" mouseleave="mainrectangle_mouseleave"
   4:            mousemove="mainrectangle_mousemove" mousedown="mainrectangle_mousedown"
   5:            mousewheel="mainrectangle_mousewheel"/>
   1: private void mainrectangle_mouseenter(object sender, mouseeventargs e)
   2: {
   3:     // 鼠标进入控件时,控件的颜色为红色
   4:     mainrectangle.fill = new solidcolorbrush(colors.red);
   5: }
   6:  
   7: private void mainrectangle_mouseleave(object sender, mouseeventargs e)
   8: {
   9:     // 鼠标离开控件时,控件的颜色为红色
  10:     mainrectangle.fill = new solidcolorbrush(colors.white);
  11: }
  12:  
  13: private void mainrectangle_mousemove(object sender, mouseeventargs e)
  14: {
  15:     // 获取基于rectangle的鼠标的坐标
  16:     point pointbaserectangle = mouse.getposition(mainrectangle);
  17:     txtmessage.text = string.format(
  18:         "mouse position (base the rectangle) is ({0},{1})",
  19:         pointbaserectangle.x, pointbaserectangle.y);
  20:  
  21:     txtmessage.text += "\r\n";
  22:  
  23:     // 获取基于窗体的鼠标的坐标
  24:     point pointbasewindow = mouse.getposition(this);
  25:     txtmessage.text += string.format(
  26:         "mouse position (base the window) is ({0},{1})",
  27:         pointbasewindow.x, pointbasewindow.y);
  28: }
  29:  
  30: private void mainrectangle_mousedown(object sender, mousebuttoneventargs e)
  31: {
  32:     // 获取点出的鼠标的按钮
  33:     mousebutton button = e.changedbutton;
  34:  
  35:     txtmessage.text += "\r\n";
  36:     txtmessage.text += string.format(
  37:         " mouse button is {0}", button.tostring());
  38: }
  39:  
  40: private void mainrectangle_mousewheel(object sender, mousewheeleventargs e)
  41: {
  42:     if (e.delta > 0)
  43:     {
  44:         // 如果向上推动滚轮,图形的宽度增加
  45:         rectangle1.width++;
  46:     }
  47:  
  48:     if (e.delta < 0)
  49:     {
  50:         // 如果向下推动滚轮,图形的宽度减小
  51:         rectangle1.width--;
  52:     }
  53: }

 

三、焦点处理

在 wpf 中,有两个与焦点有关的主要概念:键盘焦点和逻辑焦点。 键盘焦点指接收键盘输入的元素,而逻辑焦点指焦点范围中具有焦点的元素。

1、键盘焦点:

键盘焦点指当前正在接收键盘输入的元素。 在整个桌面上,只能有一个具有键盘焦点的元素。 在 wpf 中,具有键盘焦点的元素会将 iskeyboardfocused 设置为 true。 keyboard 类的静态属性 focusedelement 获取当前具有键盘焦点的元素。

为了使元素能够获取键盘焦点,基元素的 focusable 和 isvisible 属性必须设置为 true。 有些类(如 panel 基类)默认情况下将 focusable 设置为 false;因此,如果您希望此类元素能够获取键盘焦点,必须将 focusable 设置为 true。

可以通过用户与 ui 交互(例如,按 tab 键定位到某个元素或者在某些元素上单击鼠标)来获取键盘焦点。 还可以通过使用 keyboard 类的 focus 方法,以编程方式获取键盘焦点。 focus 方法尝试将键盘焦点给予指定的元素。 返回的元素是具有键盘焦点的元素,如果有旧的或新的焦点对象阻止请求,则具有键盘焦点的元素可能不是所请求的元素。

2、逻辑焦点

逻辑焦点指焦点范围中的 focusmanager..::.focusedelement。 焦点范围是一个跟踪其范围内的 focusedelement 的元素。 当键盘焦点离开焦点范围时,焦点元素会失去键盘焦点,但保留逻辑焦点。 当键盘焦点返回到焦点范围时,焦点元素会再次获得键盘焦点。 这使得键盘焦点可以在多个焦点范围之间切换,但确保了在焦点返回到焦点范围时,焦点范围中的焦点元素再次获得键盘焦点。

一个应用程序中可以有多个具有逻辑焦点的元素,但在一个特定的焦点范围中只能有一个具有逻辑焦点的元素。

getfocusscope 返回指定元素的焦点范围。

wpf 中默认情况下即为焦点范围的类有 window、menuitem、toolbar 和 contextmenu。

getfocusedelement 获取指定焦点范围的焦点元素。setfocusedelement 设置指定焦点范围中的焦点元素。setfocusedelement 通常用于设置初始焦点元素。

3、键盘导航

当按下导航键之一时,keyboardnavigation 类将负责实现默认键盘焦点导航。 导航键有:tab、shift+tab、ctrl+tab、ctrl+shift+tab、向上键、向下键、向左键和向右键。

可以通过设置附加的 keyboardnavigation 属性 tabnavigation、controltabnavigation 和 directionalnavigation 来更改导航容器的导航行为。 这些属性是 keyboardnavigationmode 类型,可能值有 continue、local、contained、cycle、once 以及 none。 默认值是 continue,这意味着元素不是导航容器。

4、焦点事件
与键盘焦点相关的事件有 previewgotkeyboardfocus、gotkeyboardfocus、previewlostkeyboardfocus 以及 lostkeyboardfocus。 这些事件定义为 keyboard 类的附加事件,但更便于作为基元素类上的等效路由事件来访问。

当元素获取键盘焦点时,会引发 gotkeyboardfocus。当元素失去键盘焦点时,会引发 lostkeyboardfocus。 如果处理了 previewgotkeyboardfocus 事件或 previewlostkeyboardfocusevent 事件,并且 handled 设置为 true,则焦点将不会改变。

 

ps:本文非原创,从其它处搬运过来供自己学习,在此感谢原作者