輸入概觀Input Overview

Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 子系統提供強大的 API,可讓您從各種裝置(包括滑鼠、鍵盤、觸控和手寫筆)取得輸入。The Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) subsystem provides a powerful API for obtaining input from a variety of devices, including the mouse, keyboard, touch, and stylus. 本主題描述 WPFWPF 所提供的服務,以及說明輸入系統的架構。This topic describes the services provided by WPFWPF and explains the architecture of the input systems.

輸入 APIInput API

主要的輸入 API 暴露于基底元素類別: UIElementContentElementFrameworkElementFrameworkContentElementThe primary input API exposure is found on the base element classes: UIElement, ContentElement, FrameworkElement, and FrameworkContentElement. 如需基底項目的詳細資訊,請參閱基底項目概觀For more information about the base elements, see Base Elements Overview. 這些類別提供按鍵動作、滑鼠按鈕、滑鼠滾輪、滑鼠移動、焦點管理和滑鼠捕捉等相關輸入事件的功能。These classes provide functionality for input events related to key presses, mouse buttons, mouse wheel, mouse movement, focus management, and mouse capture, to name a few. 藉由將輸入 API 放在基底專案上,而不是將所有輸入事件視為服務,輸入架構可讓輸入事件成為 UI 中特定物件的來源,並支援一個以上專案具有商機的事件路由配置。用來處理輸入事件的 ortunity。By placing the input API on the base elements, rather than treating all input events as a service, the input architecture enables the input events to be sourced by a particular object in the UI, and to support an event routing scheme whereby more than one element has an opportunity to handle an input event. 許多輸入事件都有一組與其建立關聯的事件。Many input events have a pair of events associated with them. 例如,「按鍵關閉」事件會與 KeyDownPreviewKeyDown 事件相關聯。For example, the key down event is associated with the KeyDown and PreviewKeyDown events. 這些事件的差異在於如何將它們路由傳送至目標項目。The difference in these events is in how they are routed to the target element. 預覽事件會從根項目到目標項目往下瀏覽通道項目樹狀結構。Preview events tunnel down the element tree from the root element to the target element. 事件反昇事件會從目標項目往上反昇到根項目。Bubbling events bubble up from the target element to the root element. 這個概觀和WPFWPF路由事件概觀稍後會更詳細討論 中的事件路由。Event routing in WPFWPF is discussed in more detail later in this overview and in the Routed Events Overview.

鍵盤和滑鼠類別Keyboard and Mouse Classes

除了基底元素類別上的輸入 API 之外,Keyboard 類別和 Mouse 類別還提供額外的 API 來處理鍵盤和滑鼠輸入。In addition to the input API on the base element classes, the Keyboard class and Mouse classes provide additional API for working with keyboard and mouse input.

Keyboard 類別上的輸入 API 範例是 Modifiers 屬性,它會傳回目前所按下的 ModifierKeys,以及 IsKeyDown 方法,決定是否按下指定的按鍵。Examples of input API on the Keyboard class are the Modifiers property, which returns the ModifierKeys currently pressed, and the IsKeyDown method, which determines whether a specified key is pressed.

下列範例會使用 GetKeyStates 方法來判斷 Key 是否處於關閉狀態。The following example uses the GetKeyStates method to determine if a Key is in the down state.

// Uses the Keyboard.GetKeyStates to determine if a key is down.
// A bitwise AND operation is used in the comparison. 
// e is an instance of KeyEventArgs.
if ((Keyboard.GetKeyStates(Key.Return) & KeyStates.Down) > 0)
{
    btnNone.Background = Brushes.Red;
}
' Uses the Keyboard.GetKeyStates to determine if a key is down.
' A bitwise AND operation is used in the comparison. 
' e is an instance of KeyEventArgs.
If (Keyboard.GetKeyStates(Key.Return) And KeyStates.Down) > 0 Then
    btnNone.Background = Brushes.Red

Mouse 類別上的輸入 API 範例是 MiddleButton,它會取得滑鼠中間按鈕的狀態,而 DirectlyOver則會取得滑鼠指標目前所在的元素。Examples of input API on the Mouse class are MiddleButton, which obtains the state of the middle mouse button, and DirectlyOver, which gets the element the mouse pointer is currently over.

下列範例會判斷滑鼠上的 LeftButton 是否處於 Pressed 狀態。The following example determines whether the LeftButton on the mouse is in the Pressed state.

if (Mouse.LeftButton == MouseButtonState.Pressed)
{
    UpdateSampleResults("Left Button Pressed");
}
If Mouse.LeftButton = MouseButtonState.Pressed Then
    UpdateSampleResults("Left Button Pressed")
End If

在此總覽中,將會更詳細地討論 MouseKeyboard 類別。The Mouse and Keyboard classes are covered in more detail throughout this overview.

手寫筆輸入Stylus Input

WPFWPF 具有 Stylus的整合支援。has integrated support for the Stylus. Stylus 是 Tablet PC 所提供的手寫筆輸入。The Stylus is a pen input made popular by the Tablet PC. WPFWPF 的應用程式可以使用滑鼠 API 將手寫筆視為滑鼠,但 WPFWPF 也會公開使用類似鍵盤和滑鼠之模型的手寫筆裝置抽象概念。applications can treat the stylus as a mouse by using the mouse API, but WPFWPF also exposes a stylus device abstraction that use a model similar to the keyboard and mouse. 所有手寫筆相關的 Api 都包含「手寫筆」一詞。All stylus-related APIs contain the word "Stylus".

因為手寫筆可以當作滑鼠,所以只支援滑鼠輸入的應用程式仍然可以自動取得某種程度的手寫筆支援。Because the stylus can act as a mouse, applications that support only mouse input can still obtain some level of stylus support automatically. 以這種方式使用手寫筆時,應用程式可以處理適當的手寫筆事件,然後處理對應的滑鼠事件。When the stylus is used in such a manner, the application is given the opportunity to handle the appropriate stylus event and then handles the corresponding mouse event. 此外,還可以透過手寫筆裝置抽象概念來取得筆跡輸入這類較高階服務。In addition, higher-level services such as ink input are also available through the stylus device abstraction. 如需將筆跡作為輸入的詳細資訊,請參閱筆跡入門For more information about ink as input, see Getting Started with Ink.

事件路由Event Routing

FrameworkElement 可以包含其他元素做為其內容模型中的子專案,以形成元素的樹狀結構。A FrameworkElement can contain other elements as child elements in its content model, forming a tree of elements. WPFWPF 中,父項目可以透過處理事件,來參與導向至其子項目或其他子系的輸入。In WPFWPF, the parent element can participate in input directed to its child elements or other descendants by handing events. 這特別適用於透過較小的控制項來建置控制項,即稱為「控制項組合」或「組合」的程序。This is especially useful for building controls out of smaller controls, a process known as "control composition" or "compositing." 如需項目樹狀結構以及項目樹狀結構與事件路由間之關聯性的詳細資訊,請參閱 WPF 中的樹狀結構For more information about element trees and how element trees relate to event routes, see Trees in WPF.

事件路由是將事件轉遞至多個項目的程序,因此,沿著路由的特定物件或項目可以選擇將重大回應提供給不同項目可能設為來源的事件 (透過處理)。Event routing is the process of forwarding events to multiple elements, so that a particular object or element along the route can choose to offer a significant response (through handling) to an event that might have been sourced by a different element. 路由事件使用三種路由機制中的其中一種︰直接、事件反昇和通道。Routed events use one of three routing mechanisms: direct, bubbling, and tunneling. 在直接路由中,來源項目是唯一收到通知的項目,而且不會將事件路由傳送至任何其他項目。In direct routing, the source element is the only element notified, and the event is not routed to any other elements. 不過,直接路由事件仍會提供一些額外的功能,只對路由事件顯示,而非標準 CLR 事件。However, the direct routed event still offers some additional capabilities that are only present for routed events as opposed to standard CLR events. 事件反昇處理項目樹狀結構的方式是先通知將事件設為來源的項目,接著通知父項目,依此類推。Bubbling works up the element tree by first notifying the element that sourced the event, then the parent element, and so on. 通道會從項目樹狀結構的根項目開始,然後往下進行,並結束於原始來源項目。Tunneling starts at the root of the element tree and works down, ending with the original source element. 如需路由事件的詳細資訊,請參閱路由事件概觀For more information about routed events, see Routed Events Overview.

WPFWPF 輸入事件一般會成對出現,而此配對包含通道事件和事件反昇事件。input events generally come in pairs that consists of a tunneling event and a bubbling event. 通道事件與事件反昇事件的區別在於 "Preview" 前置詞。Tunneling events are distinguished from bubbling events with the "Preview" prefix. 例如,PreviewMouseMove 是滑鼠移動事件的通道版本,而 MouseMove 是此事件的反升版本。For instance, PreviewMouseMove is the tunneling version of a mouse move event and MouseMove is the bubbling version of this event. 事件配對是一項在項目層級實作的慣例,而不是 WPFWPF 事件系統的一項固有功能。This event pairing is a convention that is implemented at the element level and is not an inherent capability of the WPFWPF event system. 如需詳細資訊,請參閱路由事件概觀中的<WPF 輸入事件>一節。For details, see the WPF Input Events section in Routed Events Overview.

處理輸入事件Handling Input Events

若要接收項目的輸入,事件處理常式必須與該特定事件建立關聯。To receive input on an element, an event handler must be associated with that particular event. XAMLXAML 中,這相當簡單︰您可以參考事件名稱作為將接聽此事件之項目的屬性。In XAMLXAML this is straightforward: you reference the name of the event as an attribute of the element that will be listening for this event. 然後,您可以根據委派,將屬性的值設定為您所定義之事件處理常式的名稱。Then, you set the value of the attribute to the name of the event handler that you define, based on a delegate. 事件處理常式必須以程式碼(例如) C#撰寫,而且可以包含在程式碼後置檔案中。The event handler must be written in code such as C# and can be included in a code-behind file.

作業系統報告在鍵盤焦點位於項目時所發生的按鍵動作時,會發生鍵盤事件。Keyboard events occur when the operating system reports key actions that occur while keyboard focus is on an element. 滑鼠和手寫筆事件各分為兩個分類︰報告相對於項目之指標位置變更的事件,以及報告裝置按鈕狀態變更的事件。Mouse and stylus events each fall into two categories: events that report changes in pointer position relative to the element, and events that report changes in the state of device buttons.

鍵盤輸入事件範例Keyboard Input Event Example

下列範例會接聽按下向左鍵作業。The following example listens for a left arrow key press. 建立具有 ButtonStackPanelA StackPanel is created that has a Button. 用來接聽向左箭號按鍵的事件處理常式會附加至 Button 實例。An event handler to listen for the left arrow key press is attached to the Button instance.

範例的第一個區段會建立 StackPanelButton,並附加 KeyDown的事件處理常式。The first section of the example creates the StackPanel and the Button and attaches the event handler for the KeyDown.

<StackPanel>
  <Button Background="AliceBlue"
          KeyDown="OnButtonKeyDown"
          Content="Button1"/>
</StackPanel>
// Create the UI elements.
StackPanel keyboardStackPanel = new StackPanel();
Button keyboardButton1 = new Button();

// Set properties on Buttons.
keyboardButton1.Background = Brushes.AliceBlue;
keyboardButton1.Content = "Button 1";

// Attach Buttons to StackPanel.
keyboardStackPanel.Children.Add(keyboardButton1);

// Attach event handler.
keyboardButton1.KeyDown += new KeyEventHandler(OnButtonKeyDown);
' Create the UI elements.
Dim keyboardStackPanel As New StackPanel()
Dim keyboardButton1 As New Button()

' Set properties on Buttons.
keyboardButton1.Background = Brushes.AliceBlue
keyboardButton1.Content = "Button 1"

' Attach Buttons to StackPanel.
keyboardStackPanel.Children.Add(keyboardButton1)

' Attach event handler.
AddHandler keyboardButton1.KeyDown, AddressOf OnButtonKeyDown

第二個區段是使用程式碼所撰寫,並定義事件處理常式。The second section is written in code and defines the event handler. 當按下向左鍵且 Button 具有鍵盤焦點時,就會執行處理常式,並變更 ButtonBackground 色彩。When the left arrow key is pressed and the Button has keyboard focus, the handler runs and the Background color of the Button is changed. 如果按下按鍵,但它不是向左鍵,ButtonBackground 色彩會變更回其開始色彩。If the key is pressed, but it is not the left arrow key, the Background color of the Button is changed back to its starting color.

private void OnButtonKeyDown(object sender, KeyEventArgs e)
{
    Button source = e.Source as Button;
    if (source != null)
    {
        if (e.Key == Key.Left)
        {
            source.Background = Brushes.LemonChiffon;
        }
        else
        {
            source.Background = Brushes.AliceBlue;
        }
    }
}
Private Sub OnButtonKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    Dim source As Button = TryCast(e.Source, Button)
    If source IsNot Nothing Then
        If e.Key = Key.Left Then
            source.Background = Brushes.LemonChiffon
        Else
            source.Background = Brushes.AliceBlue
        End If
    End If
End Sub

滑鼠輸入事件範例Mouse Input Event Example

在下列範例中,當滑鼠指標進入 Button時,ButtonBackground 色彩就會變更。In the following example, the Background color of a Button is changed when the mouse pointer enters the Button. 當滑鼠離開 Button時,就會還原 Background 色彩。The Background color is restored when the mouse leaves the Button.

範例的第一個區段會建立 StackPanelButton 控制項,並將 MouseEnterMouseLeave 事件的事件處理常式附加至 ButtonThe first section of the example creates the StackPanel and the Button control and attaches the event handlers for the MouseEnter and MouseLeave events to the Button.

<StackPanel>
  <Button Background="AliceBlue"
          MouseEnter="OnMouseExampleMouseEnter"
          MouseLeave="OnMosueExampleMouseLeave">Button
          
  </Button>
</StackPanel>
// Create the UI elements.
StackPanel mouseMoveStackPanel = new StackPanel();
Button mouseMoveButton = new Button();

// Set properties on Button.
mouseMoveButton.Background = Brushes.AliceBlue;
mouseMoveButton.Content = "Button";

// Attach Buttons to StackPanel.
mouseMoveStackPanel.Children.Add(mouseMoveButton);

// Attach event handler.
mouseMoveButton.MouseEnter += new MouseEventHandler(OnMouseExampleMouseEnter);
mouseMoveButton.MouseLeave += new MouseEventHandler(OnMosueExampleMouseLeave);
' Create the UI elements.
Dim mouseMoveStackPanel As New StackPanel()
Dim mouseMoveButton As New Button()

' Set properties on Button.
mouseMoveButton.Background = Brushes.AliceBlue
mouseMoveButton.Content = "Button"

' Attach Buttons to StackPanel.
mouseMoveStackPanel.Children.Add(mouseMoveButton)

' Attach event handler.
AddHandler mouseMoveButton.MouseEnter, AddressOf OnMouseExampleMouseEnter
AddHandler mouseMoveButton.MouseLeave, AddressOf OnMosueExampleMouseLeave

此範例的第二個區段是使用程式碼所撰寫,並定義事件處理常式。The second section of the example is written in code and defines the event handlers. 當滑鼠進入 Button時,ButtonBackground 色彩會變更為 [SlateGray]。When the mouse enters the Button, the Background color of the Button is changed to SlateGray. 當滑鼠離開 Button時,ButtonBackground 色彩會變更回 AliceBlueWhen the mouse leaves the Button, the Background color of the Button is changed back to AliceBlue.

private void OnMouseExampleMouseEnter(object sender, MouseEventArgs e)
{
    // Cast the source of the event to a Button.
    Button source = e.Source as Button;

    // If source is a Button.
    if (source != null)
    {
        source.Background = Brushes.SlateGray;
    }
}
Private Sub OnMouseExampleMouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)
    ' Cast the source of the event to a Button.
    Dim source As Button = TryCast(e.Source, Button)

    ' If source is a Button.
    If source IsNot Nothing Then
        source.Background = Brushes.SlateGray
    End If
End Sub
private void OnMosueExampleMouseLeave(object sender, MouseEventArgs e)
{
    // Cast the source of the event to a Button.
    Button source = e.Source as Button;

    // If source is a Button.
    if (source != null)
    {
        source.Background = Brushes.AliceBlue;
    }
}
Private Sub OnMosueExampleMouseLeave(ByVal sender As Object, ByVal e As MouseEventArgs)
    ' Cast the source of the event to a Button.
    Dim source As Button = TryCast(e.Source, Button)

    ' If source is a Button.
    If source IsNot Nothing Then
        source.Background = Brushes.AliceBlue
    End If
End Sub

文字輸入Text Input

TextInput 事件可讓您以與裝置無關的方式來接聽文字輸入。The TextInput event enables you to listen for text input in a device-independent manner. 鍵盤是文字輸入的主要方法,但是語音、手寫和其他輸入裝置也可以產生文字輸入。The keyboard is the primary means of text input, but speech, handwriting, and other input devices can generate text input also.

針對鍵盤輸入,WPFWPF 會先將適當的 KeyDown/KeyUp 事件。For keyboard input, WPFWPF first sends the appropriate KeyDown/KeyUp events. 如果未處理這些事件,而且索引鍵是文字(而不是方向箭號或功能鍵之類的控制鍵),則會引發 TextInput 事件。If those events are not handled and the key is textual (rather than a control key such as directional arrows or function keys), then a TextInput event is raised. KeyDown/KeyUpTextInput 事件之間並不一定有簡單的一對一對應,因為多個擊鍵可以產生文字輸入的單一字元,而單一按鍵可以產生多字元字串。There is not always a simple one-to-one mapping between KeyDown/KeyUp and TextInput events because multiple keystrokes can generate a single character of text input and single keystrokes can generate multi-character strings. 這特別適用于中文、日文和韓文等語言,其使用輸入法(Ime)在其對應的字母中產生數以千計的可能字元。This is especially true for languages such as Chinese, Japanese, and Korean which use Input Method Editors (IMEs) to generate the thousands of possible characters in their corresponding alphabets.

WPFWPFKeyUp/KeyDown 事件時,如果按鍵可能成為 Key 事件的一部分(例如,按下 ALT + S),Key.System 會設定為 TextInputWhen WPFWPF sends a KeyUp/KeyDown event, Key is set to Key.System if the keystrokes could become part of a TextInput event (if ALT+S is pressed, for example). 這可讓 KeyDown 事件處理常式中的程式碼檢查 Key.System,並在找到時,針對後續引發之 TextInput 事件的處理常式保留處理。This allows code in a KeyDown event handler to check for Key.System and, if found, leave processing for the handler of the subsequently raised TextInput event. 在這些情況下,可以使用 TextCompositionEventArgs 引數的各種屬性來判斷原始按鍵。In these cases, the various properties of the TextCompositionEventArgs argument can be used to determine the original keystrokes. 同樣地,如果輸入法為使用中,Key 的值為 Key.ImeProcessedImeProcessedKey 則會提供原始的按鍵或按鍵。Similarly, if an IME is active, Key has the value of Key.ImeProcessed, and ImeProcessedKey gives the original keystroke or keystrokes.

下列範例會定義 Click 事件的處理常式和 KeyDown 事件的處理常式。The following example defines a handler for the Click event and a handler for the KeyDown event.

程式碼或標記的第一個區段會建立使用者介面。The first segment of code or markup creates the user interface.

<StackPanel KeyDown="OnTextInputKeyDown">
  <Button Click="OnTextInputButtonClick"
          Content="Open" />
  <TextBox> . . . </TextBox>
</StackPanel>
// Create the UI elements.
StackPanel textInputStackPanel = new StackPanel();
Button textInputeButton = new Button();
TextBox textInputTextBox = new TextBox();
textInputeButton.Content = "Open";

// Attach elements to StackPanel.
textInputStackPanel.Children.Add(textInputeButton);
textInputStackPanel.Children.Add(textInputTextBox);

// Attach event handlers.
textInputStackPanel.KeyDown += new KeyEventHandler(OnTextInputKeyDown);
textInputeButton.Click += new RoutedEventHandler(OnTextInputButtonClick);
' Create the UI elements.
Dim textInputStackPanel As New StackPanel()
Dim textInputeButton As New Button()
Dim textInputTextBox As New TextBox()
textInputeButton.Content = "Open"

' Attach elements to StackPanel.
textInputStackPanel.Children.Add(textInputeButton)
textInputStackPanel.Children.Add(textInputTextBox)

' Attach event handlers.
AddHandler textInputStackPanel.KeyDown, AddressOf OnTextInputKeyDown
AddHandler textInputeButton.Click, AddressOf OnTextInputButtonClick

程式碼的第二個區段包含事件處理常式。The second segment of code contains the event handlers.

private void OnTextInputKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.O && Keyboard.Modifiers == ModifierKeys.Control)
    {
        handle();
        e.Handled = true;
    }
}

private void OnTextInputButtonClick(object sender, RoutedEventArgs e)
{
    handle();
    e.Handled = true;
} 

public void handle()
{
    MessageBox.Show("Pretend this opens a file");
}
Private Sub OnTextInputKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    If e.Key = Key.O AndAlso Keyboard.Modifiers = ModifierKeys.Control Then
        handle()
        e.Handled = True
    End If
End Sub

Private Sub OnTextInputButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
    handle()
    e.Handled = True
End Sub

Public Sub handle()
    MessageBox.Show("Pretend this opens a file")
End Sub

因為輸入事件會向上反升事件路由,所以不論哪一個專案具有鍵盤焦點,StackPanel 都會收到輸入。Because input events bubble up the event route, the StackPanel receives the input regardless of which element has keyboard focus. 系統會先通知 TextBox 控制項,而且只有在 TextBox 未處理輸入時,才會呼叫 OnTextInputKeyDown 處理常式。The TextBox control is notified first and the OnTextInputKeyDown handler is called only if the TextBox did not handle the input. 如果使用了 PreviewKeyDown 事件,而不是 KeyDown 事件,則會先呼叫 OnTextInputKeyDown 處理常式。If the PreviewKeyDown event is used instead of the KeyDown event, the OnTextInputKeyDown handler is called first.

在此範例中,處理邏輯會撰寫兩次:一次針對 CTRL+O,一次則是針對按鈕的 Click 事件。In this example, the handling logic is written two times—one time for CTRL+O, and again for button's click event. 這可以使用命令進行簡化,而不是直接處理輸入事件。This can be simplified by using commands, instead of handling the input events directly. 這個概觀和命令概觀討論命令。Commands are discussed in this overview and in Commanding Overview.

觸控和操作Touch and Manipulation

Windows 7 作業系統中的新硬體和 API 讓應用程式可以同時接收來自多個觸控的輸入。New hardware and API in the Windows 7 operating system provide applications the ability to receive input from multiple touches simultaneously. WPFWPF 可讓應用程式在發生觸控時引發事件,以透過回應其他輸入 (例如滑鼠或鍵盤) 的類似方式來偵測和回應觸控。enables applications to detect and respond to touch in a manner similar to responding to other input, such as the mouse or keyboard, by raising events when touch occurs.

WPFWPF 會在觸控發生時公開兩種類型的事件︰觸控事件和操作事件。exposes two types of events when touch occurs: touch events and manipulation events. 觸控事件提供觸控式螢幕上每根手指的未經處理資料和其移動。Touch events provide raw data about each finger on a touchscreen and its movement. 操作事件會將輸入解譯成特定動作。Manipulation events interpret the input as certain actions. 本節討論這兩種類型的事件。Both types of events are discussed in this section.

必要條件Prerequisites

您需要下列元件,才能開發回應觸控的應用程式。You need the following components to develop an application that responds to touch.

  • Visual Studio 2010。Visual Studio 2010.

  • Windows 7。Windows 7.

  • 支援 Windows Touch 的觸控式螢幕這類裝置。A device, such as a touchscreen, that supports Windows Touch.

用語Terminology

討論觸控時,會使用下列詞彙。The following terms are used when touch is discussed.

  • 觸控是 Windows 7 可辨識的一種使用者輸入類型。Touch is a type of user input that is recognized by Windows 7. 通常,將手指放在觸控式螢幕上,就會起始觸控。Usually, touch is initiated by putting fingers on a touch-sensitive screen. 請注意,如果裝置只會將手指的位置和移動轉換為滑鼠輸入,則膝上型電腦上常見的觸控板這類裝置不支援觸控。Note that devices such as a touchpad that is common on laptop computers do not support touch if the device merely converts the finger's position and movement as mouse input.

  • 多點觸控是同時從多點發生的觸控。Multitouch is touch that occurs from more than one point simultaneously. Windows 7 和 WPFWPF 支援多點觸控。Windows 7 and WPFWPF supports multitouch. 只要 WPFWPF 文件討論觸控,這些概念就適用於多點觸控。Whenever touch is discussed in the documentation for WPFWPF, the concepts apply to multitouch.

  • 將觸控解譯為套用至物件的實體動作時,會發生操作A manipulation occurs when touch is interpreted as a physical action that is applied to an object. WPFWPF 中,操作事件會將輸入解譯為平移、擴充或旋轉操作。In WPFWPF, manipulation events interpret input as a translation, expansion, or rotation manipulation.

  • touch device 所代表的裝置會產生觸控式螢幕上的一根手指這類觸控輸入。A touch device represents a device that produces touch input, such as a single finger on a touchscreen.

回應觸控的控制項Controls that Respond to Touch

如果控制項具有可捲動到檢視外部的內容,則將手指拖曳過該控制項即可捲動下列控制項。The following controls can be scrolled by dragging a finger across the control if it has content that is scrolled out of view.

ScrollViewer 定義 ScrollViewer.PanningMode 附加屬性,可讓您指定是否要水準、垂直、兩者或兩者皆啟用觸控式移動。The ScrollViewer defines the ScrollViewer.PanningMode attached property that enables you to specify whether touch panning is enabled horizontally, vertically, both, or neither. [ScrollViewer.PanningDeceleration] 屬性會指定當使用者從觸控觸控中提起手指時,滾動的速度會變慢。The ScrollViewer.PanningDeceleration property specifies how quickly the scrolling slows down when the user lifts the finger from the touchscreen. ScrollViewer.PanningRatio 附加屬性會指定滾動位移轉換操作位移的比率。The ScrollViewer.PanningRatio attached property specifies the ratio of scrolling offset to translate manipulation offset.

觸控事件Touch Events

基類、UIElementUIElement3DContentElement會定義您可以訂閱的事件,讓您的應用程式能夠回應觸控。The base classes, UIElement, UIElement3D, and ContentElement, define events that you can subscribe to so your application will respond to touch. 您的應用程式將觸控解譯為非操作物件的動作時,觸控事件十分有用。Touch events are useful when your application interprets touch as something other than manipulating an object. 例如,可讓使用者使用一或多根手指繪製的應用程式訂閱觸控事件。For example, an application that enables a user to draw with one or more fingers would subscribe to touch events.

不論定義類別為何,所有這三個類別都會定義下列行為類似的事件。All three classes define the following events, which behave similarly, regardless of the defining class.

與鍵盤和滑鼠事件類似,觸控事件都是路由事件。Like keyboard and mouse events, the touch events are routed events. 開頭為 Preview 的事件是通道事件,而開頭為 Touch 的事件是事件反昇事件。The events that begin with Preview are tunneling events and the events that begin with Touch are bubbling events. 如需路由事件的詳細資訊,請參閱路由事件概觀For more information about routed events, see Routed Events Overview. 當您處理這些事件時,您可以藉由呼叫 GetTouchPointGetIntermediateTouchPoints 方法,取得輸入的位置(相對於任何元素)。When you handle these events, you can get the position of the input, relative to any element, by calling the GetTouchPoint or GetIntermediateTouchPoints method.

若要了解觸控事件之間的互動,請考慮在使用者將一根手指放在項目上,並將手指移至項目中,然後將手指移開項目。To understand the interaction among the touch events, consider the scenario where a user puts one finger on an element, moves the finger in the element, and then lifts the finger from the element. 下圖顯示事件反昇事件的執行 (為求簡化,會省略通道事件)。The following illustration shows the execution of the bubbling events (the tunneling events are omitted for simplicity).

觸控事件的順序。觸控事件The sequence of touch events. Touch events

下列清單描述上圖中的事件順序。The following list describes the sequence of the events in the preceding illustration.

  1. 當使用者在元素上放置手指時,就會發生 TouchEnter 事件一次。The TouchEnter event occurs one time when the user puts a finger on the element.

  2. TouchDown 事件會發生一次。The TouchDown event occurs one time.

  3. 當使用者在元素內移動手指時,TouchMove 事件會多次發生。The TouchMove event occurs multiple times as the user moves the finger within the element.

  4. 當使用者從元素中提起手指時,就會發生 TouchUp 事件一次。The TouchUp event occurs one time when the user lifts the finger from the element.

  5. TouchLeave 事件會發生一次。The TouchLeave event occurs one time.

使用兩根以上的手指時,每根手指都會發生這些事件。When more than two fingers are used, the events occur for each finger.

操作事件Manipulation Events

在應用程式可讓使用者操作物件的情況下,UIElement 類別會定義操作事件。For cases where an application enables a user to manipulate an object, the UIElement class defines manipulation events. 與只會報告觸控位置的觸控事件不同,操作事件會報告輸入解譯方式。Unlike the touch events that simply report the position of touch, the manipulation events report how the input can be interpreted. 操作有三種類型:平移、擴充和旋轉。There are three types of manipulations, translation, expansion, and rotation. 下列清單描述如何叫用這三種類型的操作。The following list describes how to invoke the three types of manipulations.

  • 將手指放在物件上,然後將手指移過觸控式螢幕,以叫用轉譯操作。Put a finger on an object and move the finger across the touchscreen to invoke a translation manipulation. 這通常會移動物件。This usually moves the object.

  • 將兩根手指放在物件上,然後拉近兩根手指或分開兩根手指,以叫用擴充操作。Put two fingers on an object and move the fingers closer together or farther apart from one another to invoke an expansion manipulation. 這通常會調整物件大小。This usually resizes the object.

  • 將兩根手指放在物件上,然後旋轉這兩根手指,以叫用旋轉操作。Put two fingers on an object and rotate the fingers around each other to invoke a rotation manipulation. 這通常會旋轉物件。This usually rotates the object.

可以同時進行多種類型的操作。More than one type of manipulation can occur simultaneously.

當您讓物件回應操作時,可以讓物件看起來像有慣性。When you cause objects to respond to manipulations, you can have the object appear to have inertia. 這可讓您的物件模擬真實世界。This can make your objects simulate the physical world. 例如,當您在桌上推動書本時,如果推得夠用力,則在放開書本之後,書本還會繼續移動。For example, when you push a book across a table, if you push hard enough the book will continue to move after you release it. WPFWPF 可讓您透過在使用者的手指放開物件之後引發操作事件,來模擬此行為。enables you to simulate this behavior by raising manipulation events after the user's fingers releases the object.

如需如何建立可讓使用者移動、調整大小和旋轉物件的應用程式的資訊,請參閱逐步解說:建立您的第一個觸控應用程式For information about how to create an application that enables the user to move, resize, and rotate an object, see Walkthrough: Creating Your First Touch Application.

UIElement 會定義下列操作事件。The UIElement defines the following manipulation events.

根據預設,UIElement 不會收到這些操作事件。By default, a UIElement does not receive these manipulation events. 若要在 UIElement上接收操作事件,請將 UIElement.IsManipulationEnabled 設定為 [true]。To receive manipulation events on a UIElement, set UIElement.IsManipulationEnabled to true.

操作事件的執行路徑The Execution Path of Manipulation Events

請考慮使用者「擲回」物件的情況。Consider a scenario where a user "throws" an object. 使用者將手指放在物件上,並將手指移過觸控式螢幕的一小段距離,然後在移動時移開手指。The user puts a finger on the object, moves the finger across the touchscreen for a short distance, and then lifts the finger while it is moving. 這樣的結果是會移動使用者手指下方的物件,並在使用者移開手指之後繼續移動。The result of this is that the object will move under the user's finger and continue to move after the user lifts the finger.

下圖顯示操作事件的執行路徑以及每個事件的重要資訊。The following illustration shows the execution path of manipulation events and important information about each event.

操作事件的順序。操作事件The sequence of manipulation events. Manipulation events

下列清單描述上圖中的事件順序。The following list describes the sequence of the events in the preceding illustration.

  1. 當使用者將手指放在物件上時,就會發生 ManipulationStarting 事件。The ManipulationStarting event occurs when the user places a finger on the object. 除此之外,此事件還可讓您設定 ManipulationContainer 屬性。Among other things, this event allows you to set the ManipulationContainer property. 在後續事件中,操作的位置會相對於 ManipulationContainerIn the subsequent events, the position of the manipulation will be relative to the ManipulationContainer. ManipulationStarting以外的事件中,這個屬性是唯讀的,因此 ManipulationStarting 的事件是您唯一可以設定這個屬性的時間。In events other than ManipulationStarting, this property is read-only, so the ManipulationStarting event is the only time that you can set this property.

  2. 下一步就會發生 ManipulationStarted 事件。The ManipulationStarted event occurs next. 此事件會報告操作的原點。This event reports the origin of the manipulation.

  3. 當使用者的手指在觸控螢幕上移動時,ManipulationDelta 事件會多次發生。The ManipulationDelta event occurs multiple times as a user's fingers move on a touchscreen. ManipulationDeltaEventArgs 類別的 DeltaManipulation 屬性會報告操作是否會被視為移動、展開或轉譯。The DeltaManipulation property of the ManipulationDeltaEventArgs class reports whether the manipulation is interpreted as movement, expansion, or translation. 這是您執行大部分物件操作工作的位置。This is where you perform most of the work of manipulating an object.

  4. 當使用者的手指失去與物件的聯繫時,就會發生 ManipulationInertiaStarting 事件。The ManipulationInertiaStarting event occurs when the user's fingers lose contact with the object. 此事件可讓您指定慣性期間的操作減速。This event enables you to specify the deceleration of the manipulations during inertia. 因此,如果選擇的話,您的物件可以模擬不同的實體空間或屬性。This is so your object can emulate different physical spaces or attributes if you choose. 例如,假設應用程式有兩個物件代表真實世界中的項目,而且其中一個比另一個重。For example, suppose your application has two objects that represent items in the physical world, and one is heavier than the other. 您可以將較重的物件減速,使其比較輕的物件更快。You can make the heavier object decelerate faster than the lighter object.

  5. 發生慣性時,ManipulationDelta 事件會多次發生。The ManipulationDelta event occurs multiple times as inertia occurs. 請注意,使用者手指移過觸控式螢幕時,以及 WPFWPF 模擬慣性時,都會發生此事件。Note that this event occurs when the user's fingers move across the touchscreen and when WPFWPF simulates inertia. 換句話說,ManipulationDelta 發生在 ManipulationInertiaStarting 事件之前和之後。In other words, ManipulationDelta occurs before and after the ManipulationInertiaStarting event. ManipulationDeltaEventArgs.IsInertial 屬性會報告是否在慣性期間發生 ManipulationDelta 事件,因此您可以檢查該屬性並執行不同的動作(視其值而定)。The ManipulationDeltaEventArgs.IsInertial property reports whether the ManipulationDelta event occurs during inertia, so you can check that property and perform different actions, depending on its value.

  6. 當操作和任何慣性結束時,就會發生 ManipulationCompleted 事件。The ManipulationCompleted event occurs when the manipulation and any inertia ends. 也就是說,在發生所有 ManipulationDelta 事件之後,就會發生 ManipulationCompleted 事件,以表示操作已完成。That is, after all the ManipulationDelta events occur, the ManipulationCompleted event occurs to signal that the manipulation is complete.

UIElement 也會定義 ManipulationBoundaryFeedback 事件。The UIElement also defines the ManipulationBoundaryFeedback event. ManipulationDelta 事件中呼叫 ReportBoundaryFeedback 方法時,就會發生這個事件。This event occurs when the ReportBoundaryFeedback method is called in the ManipulationDelta event. 當物件達到界限時,ManipulationBoundaryFeedback 事件可讓應用程式或元件提供視覺效果的意見反應。The ManipulationBoundaryFeedback event enables applications or components to provide visual feedback when an object hits a boundary. 例如,Window 類別會處理 ManipulationBoundaryFeedback 事件,以便在遇到邊緣時讓視窗稍微移動。For example, the Window class handles the ManipulationBoundaryFeedback event to cause the window to slightly move when its edge is encountered.

您可以在 ManipulationBoundaryFeedback 事件以外的任何操作事件中,對事件引數呼叫 Cancel 方法,以取消操作。You can cancel the manipulation by calling the Cancel method on the event arguments in any manipulation event except ManipulationBoundaryFeedback event. 當您呼叫 Cancel時,不會再引發操作事件,也不會發生滑鼠事件以供觸控。When you call Cancel, the manipulation events are no longer raised and mouse events occur for touch. 下表描述操作取消時間與所發生的滑鼠事件之間的關聯性。The following table describes the relationship between the time the manipulation is canceled and the mouse events that occur.

其中呼叫 Cancel 的事件The event that Cancel is called in 針對已發生之輸入所發生的滑鼠事件The mouse events that occur for input that already occurred
ManipulationStartingManipulationStartedManipulationStarting and ManipulationStarted 滑鼠向下事件。Mouse down events.
ManipulationDelta 滑鼠向下和滑鼠移動事件。Mouse down and mouse move events.
ManipulationInertiaStartingManipulationCompletedManipulationInertiaStarting and ManipulationCompleted 滑鼠向下、滑鼠移動和滑鼠向上事件。Mouse down, mouse move, and mouse up events.

請注意,如果您在操作處於慣性時呼叫 Cancel,此方法會傳回 false 而輸入不會引發滑鼠事件。Note that if you call Cancel when the manipulation is in inertia, the method returns false and the input does not raise mouse events.

觸控與操作事件之間的關聯性The Relationship Between Touch and Manipulation Events

UIElement 一律可以接收觸控事件。A UIElement can always receive touch events. 當 [IsManipulationEnabled] 屬性設定為 [true] 時,UIElement 可以同時接收觸控和操作事件。When the IsManipulationEnabled property is set to true, a UIElement can receive both touch and manipulation events. 如果未處理 TouchDown 事件(亦即,Handled 屬性為 false),則操作邏輯會將觸控捕捉至專案,並產生操作事件。If the TouchDown event is not handled (that is, the Handled property is false), the manipulation logic captures the touch to the element and generates the manipulation events. 如果 Handled 屬性在 TouchDown 事件中設定為 true,則操作邏輯不會產生操作事件。If the Handled property is set to true in the TouchDown event, the manipulation logic does not generate manipulation events. 下圖示範觸控事件與操作事件之間的關聯性。The following illustration shows the relationship between touch events and manipulation events.

![觸控與操作事件之間的關聯]性(./media/ndp-touchmanipulateevents.png "NDP_TouchManipulateEvents")觸控和操作事件Relationship between touch and manipulation events Touch and manipulation events

下列清單描述上圖中所顯示之觸控事件與操作事件間的關聯性。The following list describes the relationship between the touch and manipulation events that is shown in the preceding illustration.

焦點Focus

WPFWPF 中有兩個關於焦點的主要概念︰鍵盤焦點和邏輯焦點。There are two main concepts that pertain to focus in WPFWPF: keyboard focus and logical focus.

鍵盤焦點Keyboard Focus

鍵盤焦點是指接收鍵盤輸入的項目。Keyboard focus refers to the element that is receiving keyboard input. 整個桌面只能有一個項目有鍵盤焦點。There can be only one element on the whole desktop that has keyboard focus. WPFWPF中,具有鍵盤焦點的元素會 IsKeyboardFocused 設定為 [true]。In WPFWPF, the element that has keyboard focus will have IsKeyboardFocused set to true. 靜態 Keyboard 方法 FocusedElement 會傳回目前具有鍵盤焦點的元素。The static Keyboard method FocusedElement returns the element that currently has keyboard focus.

若要取得鍵盤焦點,請將 tab 鍵移至專案,或按一下特定元素上的滑鼠(例如 TextBox)。Keyboard focus can be obtained by tabbing to an element or by clicking the mouse on certain elements, such as a TextBox. 您也可以使用 Keyboard 類別上的 Focus 方法,以程式設計方式取得鍵盤焦點。Keyboard focus can also be obtained programmatically by using the Focus method on the Keyboard class. Focus 嘗試提供指定的元素鍵盤焦點。Focus attempts to give the specified element keyboard focus. Focus 傳回的元素是目前具有鍵盤焦點的專案。The element returned by Focus is the element that currently has keyboard focus.

為了讓元素取得鍵盤焦點,Focusable 屬性和 IsVisible 屬性必須設定為trueIn order for an element to obtain keyboard focus the Focusable property and the IsVisible properties must be set to true. 某些類別(例如 Panel)預設會 Focusable 設定為 false;因此,如果您想要讓該專案能夠取得焦點,您可能必須將此屬性設定為 [true]。Some classes, such as Panel, have Focusable set to false by default; therefore, you may have to set this property to true if you want that element to be able to obtain focus.

下列範例會使用 Focus,在 Button上設定鍵盤焦點。The following example uses Focus to set keyboard focus on a Button. 在應用程式中設定初始焦點的建議位置是在 Loaded 事件處理常式中。The recommended place to set initial focus in an application is in the Loaded event handler.

private void OnLoaded(object sender, RoutedEventArgs e)
{
    // Sets keyboard focus on the first Button in the sample.
    Keyboard.Focus(firstButton);
}
Private Sub OnLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Sets keyboard focus on the first Button in the sample.
    Keyboard.Focus(firstButton)
End Sub

如需鍵盤焦點的詳細資訊,請參閱焦點概觀For more information about keyboard focus, see Focus Overview.

邏輯焦點Logical Focus

邏輯焦點是指焦點範圍中的 FocusManager.FocusedElementLogical focus refers to the FocusManager.FocusedElement in a focus scope. 應用程式中可以有多個具有邏輯焦點的項目,但特定的焦點範圍中只能有一個有邏輯焦點的項目。There can be multiple elements that have logical focus in an application, but there may only be one element that has logical focus in a particular focus scope.

焦點範圍是一個容器元素,可追蹤其範圍內的 FocusedElementA focus scope is a container element that keeps track of the FocusedElement within its scope. 當焦點離開焦點範圍時,焦點項目就會失去鍵盤焦點,但卻仍然保有邏輯焦點。When focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. 當焦點回到焦點範圍時,焦點項目就會取得鍵盤焦點。When focus returns to the focus scope, the focused element will obtain keyboard focus. 這讓鍵盤焦點能在多個焦點範圍間變更,但確保焦點範圍內的焦點項目仍是焦點返回時的焦點項目。This allows for keyboard focus to be changed between multiple focus scopes but insures that the focused element within the focus scope remains the focused element when focus returns.

藉由使用 SetIsFocusScope 方法設定附加屬性,將 FocusManager 附加 IsFocusScope 屬性設定為 [true] 或 [程式碼],即可將專案轉換成 Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) 的焦點範圍。An element can be turned into a focus scope in Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) by setting the FocusManager attached property IsFocusScope to true, or in code by setting the attached property by using the SetIsFocusScope method.

下列範例會藉由設定 IsFocusScope 附加屬性,讓 StackPanel 進入焦點範圍。The following example makes a StackPanel into a focus scope by setting the IsFocusScope attached property.

<StackPanel Name="focusScope1" 
            FocusManager.IsFocusScope="True"
            Height="200" Width="200">
  <Button Name="button1" Height="50" Width="50"/>
  <Button Name="button2" Height="50" Width="50"/>
</StackPanel>
StackPanel focuseScope2 = new StackPanel();
FocusManager.SetIsFocusScope(focuseScope2, true);
Dim focuseScope2 As New StackPanel()
FocusManager.SetIsFocusScope(focuseScope2, True)

WPFWPF 中的類別預設為焦點範圍,WindowMenuToolBarContextMenuClasses in WPFWPF which are focus scopes by default are Window, Menu, ToolBar, and ContextMenu.

具有鍵盤焦點的元素也會有其所屬焦點範圍的邏輯焦點;因此,將焦點設定在 Keyboard 類別或基底專案類別上具有 Focus 方法的元素,將會嘗試提供元素鍵盤焦點和邏輯焦點。An element that has keyboard focus will also have logical focus for the focus scope it belongs to; therefore, setting focus on an element with the Focus method on the Keyboard class or the base element classes will attempt to give the element keyboard focus and logical focus.

若要判斷焦點範圍中的焦點元素,請使用 GetFocusedElementTo determine the focused element in a focus scope, use GetFocusedElement. 若要變更焦點範圍的聚焦元素,請使用 SetFocusedElementTo change the focused element for a focus scope, use SetFocusedElement.

如需邏輯焦點的詳細資訊,請參閱焦點概觀For more information about logical focus, see Focus Overview.

滑鼠位置Mouse Position

WPFWPF 輸入 API 會提供關於座標空間的實用資訊。The WPFWPF input API provides helpful information with regard to coordinate spaces. 例如,座標 (0,0) 是左上角的座標,但樹狀結構中的項目左上方為何?For example, coordinate (0,0) is the upper-left coordinate, but the upper-left of which element in the tree? 為輸入目標的項目?The element that is the input target? 附加事件處理常式的項目?The element you attached your event handler to? 或其他事項?Or something else? 為了避免混淆,當您使用透過滑鼠取得的座標時,WPFWPF 輸入 API 會要求您指定參考框架。To avoid confusion, the WPFWPF input API requires that you specify your frame of reference when you work with coordinates obtained through the mouse. GetPosition 方法會傳回相對於指定元素之滑鼠指標的座標。The GetPosition method returns the coordinate of the mouse pointer relative to the specified element.

滑鼠捕捉Mouse Capture

滑鼠裝置專門保留稱為滑鼠捕捉的強制回應特性。Mouse devices specifically hold a modal characteristic known as mouse capture. 滑鼠捕捉是用來維護啟動拖放作業後的轉換輸入狀態;因此,不一定會發生涉及滑鼠指標之額定螢幕位置的其他作業。Mouse capture is used to maintain a transitional input state when a drag-and-drop operation is started, so that other operations involving the nominal on-screen position of the mouse pointer do not necessarily occur. 拖曳期間,使用者按一下就會中止拖放,這樣會在拖曳原點持有滑鼠捕捉時,讓大部分的 mouseover 提示為不適當。During the drag, the user cannot click without aborting the drag-and-drop, which makes most mouseover cues inappropriate while the mouse capture is held by the drag origin. 輸入系統會公開可判斷滑鼠捕捉狀態的 Api,以及可以強制滑鼠捕捉至特定元素或清除滑鼠捕捉狀態的 Api。The input system exposes APIs that can determine mouse capture state, as well as APIs that can force mouse capture to a specific element, or clear mouse capture state. 如需拖放作業的詳細資訊,請參閱拖放概觀For more information on drag-and-drop operations, see Drag and Drop Overview.

命令Commands

命令比裝置輸入更接近語意層級的輸入處理。Commands enable input handling at a more semantic level than device input. 命令是簡單指示詞,例如 CutCopyPasteOpenCommands are simple directives, such as Cut, Copy, Paste, or Open. 命令適用於將命令邏輯集中。Commands are useful for centralizing your command logic. 您可以從 MenuToolBar或鍵盤快速鍵來存取相同的命令。The same command might be accessed from a Menu, on a ToolBar, or through a keyboard shortcut. 命令也提供一種機制,可在命令變成無法使用時停用控制項。Commands also provide a mechanism for disabling controls when the command becomes unavailable.

RoutedCommandICommandWPFWPF 執行。RoutedCommand is the WPFWPF implementation of ICommand. 執行 RoutedCommand 時,會在命令目標上引發 PreviewExecutedExecuted 事件,這會透過專案樹狀結構來進行通道和冒泡,如同其他輸入。When a RoutedCommand is executed, a PreviewExecuted and an Executed event are raised on the command target, which tunnel and bubble through the element tree like other input. 如果未設定命令目標,則具有鍵盤焦點的項目就是命令目標。If a command target is not set, the element with keyboard focus will be the command target. 執行命令的邏輯會附加至 CommandBindingThe logic that performs the command is attached to a CommandBinding. Executed 事件達到該特定命令的 CommandBinding 時,會呼叫 CommandBinding 上的 ExecutedRoutedEventHandlerWhen an Executed event reaches a CommandBinding for that specific command, the ExecutedRoutedEventHandler on the CommandBinding is called. 此處理常式會執行命令的動作。This handler performs the action of the command.

如需命令的詳細資訊,請參閱命令概觀For more information on commanding, see Commanding Overview.

WPFWPF 提供常用命令的程式庫,其中包含 ApplicationCommandsMediaCommandsComponentCommandsNavigationCommandsEditingCommands,您也可以自行定義。provides a library of common commands which consists of ApplicationCommands, MediaCommands, ComponentCommands, NavigationCommands, and EditingCommands, or you can define your own.

下列範例示範如何設定 MenuItem,以便在按一下它時,會叫用 TextBox上的 Paste 命令,假設 TextBox 具有鍵盤焦點。The following example shows how to set up a MenuItem so that when it is clicked it will invoke the Paste command on the TextBox, assuming the TextBox has keyboard focus.

<StackPanel>
  <Menu>
    <MenuItem Command="ApplicationCommands.Paste" />
  </Menu>
  <TextBox />
</StackPanel>
  // Creating the UI objects
  StackPanel mainStackPanel = new StackPanel();
  TextBox pasteTextBox = new TextBox();
  Menu stackPanelMenu = new Menu();
  MenuItem pasteMenuItem = new MenuItem();
 
  // Adding objects to the panel and the menu
  stackPanelMenu.Items.Add(pasteMenuItem);
  mainStackPanel.Children.Add(stackPanelMenu);
  mainStackPanel.Children.Add(pasteTextBox);

  // Setting the command to the Paste command
  pasteMenuItem.Command = ApplicationCommands.Paste;

  // Setting the command target to the TextBox
  pasteMenuItem.CommandTarget = pasteTextBox;
' Creating the UI objects
Dim mainStackPanel As New StackPanel()
Dim pasteTextBox As New TextBox()
Dim stackPanelMenu As New Menu()
Dim pasteMenuItem As New MenuItem()

' Adding objects to the panel and the menu
stackPanelMenu.Items.Add(pasteMenuItem)
mainStackPanel.Children.Add(stackPanelMenu)
mainStackPanel.Children.Add(pasteTextBox)

' Setting the command to the Paste command
pasteMenuItem.Command = ApplicationCommands.Paste

如需 WPFWPF 中命令的詳細資訊,請參閱命令概觀For more information about commands in WPFWPF, see Commanding Overview.

輸入系統和基底項目The Input System and Base Elements

輸入事件(例如 MouseKeyboardStylus 類別所定義的附加事件)會由輸入系統引發,並根據在執行時間測試視覺化樹狀結構的方式,插入物件模型中的特定位置。Input events such as the attached events defined by the Mouse, Keyboard, and Stylus classes are raised by the input system and injected into a particular position in the object model based on hit testing the visual tree at run time.

MouseKeyboardStylus 定義為附加事件的每個事件,也會由基底元素類別 UIElement 重新公開,並 ContentElement 為新的路由事件。Each of the events that Mouse, Keyboard, and Stylus define as an attached event is also re-exposed by the base element classes UIElement and ContentElement as a new routed event. 可處理原始附加事件並重複使用事件資料的類別會產生基底項目路由事件。The base element routed events are generated by classes handling the original attached event and reusing the event data.

輸入事件透過其基底項目輸入事件實作與特定來源項目建立關聯時,可以透過事件路由的其餘部分進行路由傳送,而其乃根據邏輯和視覺化樹狀結構物件的組合,並透過應用程式碼進行處理。When the input event becomes associated with a particular source element through its base element input event implementation, it can be routed through the remainder of an event route that is based on a combination of logical and visual tree objects, and be handled by application code. 一般來說,使用 UIElementContentElement上的路由事件處理這些裝置相關的輸入事件會比較方便,因為您可以在 XAMLXAML 和程式碼中使用更直覺的事件處理常式語法。Generally, it is more convenient to handle these device-related input events using the routed events on UIElement and ContentElement, because you can use more intuitive event handler syntax both in XAMLXAML and in code. 您可以選擇改為處理已初始程序的附加事件,但會遇到幾個問題︰基底項目類別處理可能會將附加事件標記為已處理,而且您需要使用存取子方法,而不是真正事件語法,才能附加所附加事件的處理常式。You could choose to handle the attached event that initiated the process instead, but you would face several issues: the attached event may be marked handled by the base element class handling, and you need to use accessor methods rather than true event syntax in order to attach handlers for attached events.

下一步What's Next

您現在有數種方式可處理 WPFWPF 中的輸入。You now have several techniques to handle input in WPFWPF. 您也應該進一步了解各種類型的輸入事件以及 WPFWPF 所使用的路由事件機制。You should also have an improved understanding of the various types of input events and the routed event mechanisms used by WPFWPF.

具有其他資源可詳細說明 WPFWPF 架構項目和事件路由。Additional resources are available that explain WPFWPF framework elements and event routing in more detail. 如需詳細資訊,請參閱下列概觀:命令概觀焦點概觀基底項目概觀WPF 中的樹狀結構路由事件概觀See the following overviews for more information, Commanding Overview, Focus Overview, Base Elements Overview, Trees in WPF, and Routed Events Overview.

另請參閱See also