keyboard input, mouse input, focus processing

Source: Internet
Author: User

Keyboard class and keyboard events

WPF provides the underlying keyboard class (System.Input.Keyboard Class) that provides keyboard-related events, methods, and properties that provide information about the state of the keyboard. Keyboard events are also provided outside the event of a XAML base element class such as UIElement.

For keyboard operations, there are two groups of commonly used events:

    • KeyDown events and Previewkeydown events: Handling keyboard keys pressed
    • KeyUp events and Previewkeyup events: Handling Keyboard keys Lifting

Where the KeyDown and KeyUp events belong to the bubbling routed event, and Previewkeydown and Previewkeyup belong to the tunnel routed event.

In order for the element to receive keyboard input, the element must have focus. By default, most UIElement derived objects gain focus. If this is not the case, set the Focusable property on the base element to true if you want the element to have focus. A Panel class such as StackPanel and Canvas sets the default value of Focusable to false. Therefore, Focusable must be set to true for these objects to get keyboard focus.

For example: In the author's notebook There are "mute", "Increase volume", "volume reduction" of the three shortcut keys, in an application form on the processing of these three key clicks can be:

   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:     
   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:         //Press the "mute" key
   6:         txtmessage.text = "Mute";
   7:         e.handled = true;
   8:     }
   9:     Else if (E.key = = key.volumeup)
  Ten:     {
  One:         //Press the "Increase volume" button
  :         txtmessage.text = "Up";
  :         e.handled = true;
  :     }
  :     else if (E.key = = Key.volumedown)
  :     {
  :         //Press the "Decrease Volume" button
  :         txtmessage.text = "Down";
  :         e.handled = true;
  :     }
  21:}
Second, mouse and mouse events

The System.Input.Mouse class provided by WPF provides mouse-related events, methods, and properties that provide information about the state of the mouse. Similar to the keyboard class, its events are also provided out of the base elements such as UIElement.

Its events are mainly in the following groups (each event contains the XXX bubbling routed event and the Previewxxx tunnel routed event)

    • MouseDown, MouseUp event: Handling mouse button Press and lift
    • MouseEnter, MouseLeave, MouseMove: Handling mouse entry, leaving controls, and moving on controls
    • MouseWheel: Handling Mouse wheel scrolling

In addition, for capturing the mouse position, the GetPosition method of the mouse class is used, whose argument is a uielement that represents the coordinate system of the control whose mouse position is based.

For example, for a rectangular shape, set various events for its mouse:

   
   2:            name= "Mainrectangle" stroke= "Black" width= "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:     //When mouse enters control, the color of the control is red
   4:     Mainrectangle.fill = new SolidColorBrush (colors.red);
   5:}
   
   7:private void Mainrectangle_mouseleave (object sender, MouseEventArgs e)
   8: {
   9:     //When the mouse leaves the control, the color of the control is red
  Ten:     Mainrectangle.fill = new SolidColorBrush (colors.white);
  11:}
  
  13:private void Mainrectangle_mousemove (object sender, MouseEventArgs e)
  14: {
  :     //Get the coordinates of the mouse based on rectangle
  : Point     Pointbaserectangle = Mouse.getposition (Mainrectangle);
  :     Txtmessage.text = string. Format (
  :         "Mouse Position (Base the Rectangle) is ({0},{1})",
  :         pointbaserectangle.x, POINTBASERECTANGLE.Y);
  
  £     txtmessage.text + = "\ r \ n";
  
  :     //Get the coordinates of a form-based mouse
  : Point     Pointbasewindow = Mouse.getposition (this);
  :     Txtmessage.text + = string. Format (
  :         "Mouse Position (Base the Window) is ({0},{1})",
  :         pointbasewindow.x, POINTBASEWINDOW.Y);
  28:}
  
  30:private void Mainrectangle_mousedown (object sender, MouseButtonEventArgs e)
  31: {
  +:     //Get the mouse button out of the point
  :     Mousebutton button = E.changedbutton;
  
  :     txtmessage.text + = "\ r \ n";
  £ º     Txtmessage.text + = string. Format (
  PNS:         "Mouse button is {0}", button. ToString ());
  38:}
  
  40:private void Mainrectangle_mousewheel (object sender, MouseWheelEventArgs e)
  41: {
  :     if (E.delta > 0)
  :     {
  :         //If the wheel is pushed upward, the width of the graph increases
  :         rectangle1. width++;
  :     }
  
  :     if (E.delta < 0)
  $:     {
  :         //If the scroll wheel is pushed downward, the width of the graphic is reduced
  Wuyi:         rectangle1. width--;
  :     }
  :} 
Third, focus treatment

In WPF, there are two key concepts related to focus: keyboard focus and logical focus. Keyboard focus refers to the element that receives keyboard input, while logical focus refers to the element that has focus in the focus range.

1. Keyboard Focus:

Keyboard focus refers to the element that is currently receiving keyboard input. There can be only one element with keyboard focus on the entire desktop. In WPF, elements with keyboard focus set iskeyboardfocused to True. The static property of the Keyboard class Focusedelement gets the element that currently has keyboard focus.

For the element to be able to get keyboard focus, the Focusable and IsVisible properties of the base element must be set to true. Some classes, such as the Panel base class, set Focusable to False by default, so if you want such elements to be able to get keyboard focus, you must set Focusable to True.

You can get keyboard focus by interacting with the UI (for example, by tabbing to an element or by clicking a mouse on some elements). You can also get keyboard focus programmatically by using the focus method of the Keyboard class. The focus method attempts to give keyboard focus to the specified element. The returned element is an element that has keyboard focus, and if an old or new focus object blocks the request, the element that has keyboard focus may not be the requested element.

2. Logical focus

Logical focus refers to the Focusmanager in the Focus range ...::. Focusedelement. The focus range is an element that tracks the focusedelement within its scope. When keyboard focus leaves the focus range, the focus element loses keyboard focus, but retains logical focus. When keyboard focus returns to the focus range, the focus element gets keyboard focus again. This allows keyboard focus to switch between multiple focus ranges, but ensures that the focus elements in the focus range get keyboard focus again when the focus returns to the focus range.

An application can have multiple elements with logical focus, but there can be only one element with logical focus in a particular focus range.

Getfocusscope returns the focus range for the specified element.

The classes in WPF that are focus scopes by default are Window, MenuItem, ToolBar, and ContextMenu.

Getfocusedelement gets the focus element for the specified focus range. Setfocusedelement sets the focus element in the specified focus range. Setfocusedelement are typically used to set initial focus elements.

3. Keyboard navigation

When you press one of the navigation keys, the Keyboardnavigation class is responsible for implementing the default keyboard focus navigation. The Navigation keys are tab, Shift+tab, Ctrl+tab, Ctrl+shift+tab, UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT arrow.

You can change the navigation behavior of the navigation container by setting additional keyboardnavigation properties Tabnavigation, Controltabnavigation, and Directionalnavigation. These properties are keyboardnavigationmode types, with possible values of Continue, Local, Contained, Cycle, Once, and None. The default value is Continue, which means that the element is not a navigation container.

4. Focus Event

The events associated with keyboard focus are previewgotkeyboardfocus, Gotkeyboardfocus, Previewlostkeyboardfocus, and Lostkeyboardfocus. These events are defined as additional events for the Keyboard class, but are more convenient to access as equivalent routed events on the base element class.

Gotkeyboardfocus is raised when the element gets keyboard focus. Lostkeyboardfocus is raised when the element loses keyboard focus. If the Previewgotkeyboardfocus event or Previewlostkeyboardfocusevent event is processed and the Handled is set to true, the focus will not change.

keyboard input, mouse input, focus processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.