QueryContinueDragEventArgs.KeyState 属性

定义

获取 Shift、Ctrl 和 Alt 键的当前状态。Gets the current state of the SHIFT, CTRL, and ALT keys.

public:
 property int KeyState { int get(); };
public int KeyState { get; }
member this.KeyState : int
Public ReadOnly Property KeyState As Integer

属性值

Int32

Shift、Ctrl 和 Alt 键的当前状态。The current state of the SHIFT, CTRL, and ALT keys.

示例

下面的代码示例演示如何使用此成员。The following code example demonstrates the use of this member. 在此示例中,事件处理程序报告事件的发生 Control.QueryContinueDragIn the example, an event handler reports on the occurrence of the Control.QueryContinueDrag event. 此报表可帮助你了解何时发生事件,并可帮助你进行调试。This report helps you to learn when the event occurs and can assist you in debugging. 若要报告多个事件或频繁发生的事件,请考虑 MessageBox.Show Console.WriteLine 将消息替换为多行或将其追加到多行 TextBoxTo report on multiple events or on events that occur frequently, consider replacing MessageBox.Show with Console.WriteLine or appending the message to a multiline TextBox.

若要运行示例代码,请将其粘贴到一个项目中,该项目包含从继承的类型的实例 Control ,如 ButtonComboBoxTo run the example code, paste it into a project that contains an instance of a type that inherits from Control, such as a Button or ComboBox. 然后命名该实例 Control1 ,并确保事件处理程序与事件相关联 Control.QueryContinueDragThen name the instance Control1 and ensure that the event handler is associated with the Control.QueryContinueDrag event.

private void Control1_QueryContinueDrag(Object sender, QueryContinueDragEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "KeyState", e.KeyState );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "EscapePressed", e.EscapePressed );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Action", e.Action );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "QueryContinueDrag Event" );
}
Private Sub Control1_QueryContinueDrag(sender as Object, e as QueryContinueDragEventArgs) _ 
     Handles Control1.QueryContinueDrag

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "KeyState", e.KeyState)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "EscapePressed", e.EscapePressed)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Action", e.Action)
    messageBoxVB.AppendLine()
    MessageBox.Show(messageBoxVB.ToString(),"QueryContinueDrag Event")

End Sub

注解

属性中设置的位标识在 KeyState 操作过程中按下的键或鼠标按钮。The bits that are set in the KeyState property identify the keys or mouse buttons that were pressed during the operation. 例如,如果按下鼠标左键,则会设置属性中的第一位 KeyStateFor example, if the left mouse button is pressed, the first bit in the KeyState property is set. 您可以使用按位 AND 运算符来测试给定的键状态。You can use the bitwise AND operator to test for a given key state.

下表列出了为指定事件设置的位。The following table lists the bits that are set for a specified event.

bitBit Key
11 鼠标左按钮。The left mouse button.
22 鼠标右按钮。The right mouse button.
44 Shift 键。The SHIFT key.
88 CTL 密钥。The CTL key.
1616 鼠标中键。The middle mouse button.
3232 Alt 键。The ALT key.

默认情况下, QueryContinueDrag 事件将设置 Action 为( DragAction.Drop 如果 KeyState 指示按下了鼠标左键、中键或鼠标右键)。By default, the QueryContinueDrag event sets Action to DragAction.Drop if KeyState indicates that the left, middle, or right mouse button is pressed.

适用于

另请参阅