DragEventArgs.KeyState Eigenschaft

Definition

Ruft den aktuellen Zustand von UMSCHALTTASTE, STRG, ALT sowie den Zustand der Maustasten ab.

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

Eigenschaftswert

Der aktuelle Zustand von UMSCHALTTASTE, STRG, ALT sowie der Maustasten.

Beispiele

Im folgenden Beispiel wird ein Drag-and-Drop-Vorgang zwischen zwei ListBox Steuerelementen veranschaulicht. Im Beispiel wird die DoDragDrop -Methode aufgerufen, wenn die Ziehaktion gestartet wird. Die Ziehaktion wird gestartet, wenn sich die Maus während MouseDown des Ereignisses mehr als SystemInformation.DragSize von der Mausposition bewegt hat. Die IndexFromPoint -Methode wird verwendet, um den Index des Elements zu bestimmen, das während des Ereignisses MouseDown gezogen werden soll.

Das Beispiel veranschaulicht auch die Verwendung benutzerdefinierter Cursor für den Drag-and-Drop-Vorgang. Im Beispiel wird davon ausgegangen, dass zwei Cursordateien und 3dwarro.cur3dwno.curim Anwendungsverzeichnis für die benutzerdefinierten Cursor drag und no drop vorhanden sind. Die benutzerdefinierten Cursor werden verwendet, wenn die UseCustomCursorsCheckCheckBox aktiviert ist. Die benutzerdefinierten Cursor werden im GiveFeedback Ereignishandler festgelegt.

Der Tastaturzustand wird im DragOver Ereignishandler für die rechte ListBoxausgewertet, um zu bestimmen, welcher Ziehvorgang auf der Grundlage des Zustands der UMSCHALT-, STRG-, ALT- oder STRG+ALT-TASTEN erfolgt. Die Position in der, an der ListBox der Abbruch erfolgen würde, wird auch während des Ereignisses DragOver bestimmt. Wenn die zu löschenden Daten keine Stringsind, wird auf DragEventArgs.EffectDragDropEffects.Nonefestgelegt. Schließlich wird die status des Drops im DropLocationLabelLabelangezeigt.

Die für die rechte ListBox Seite zu löschenden Daten werden im DragDrop -Ereignishandler bestimmt, und der String Wert wird an der entsprechenden Stelle im ListBoxhinzugefügt. Wenn der Ziehvorgang außerhalb der Grenzen des Formulars verschoben wird, wird der Drag-and-Drop-Vorgang im QueryContinueDrag Ereignishandler abgebrochen.

Dieser Codeauszug veranschaulicht die Verwendung der DragEventArgs -Klasse. DoDragDrop Das vollständige Codebeispiel finden Sie in der -Methode.

void ListDragTarget_DragOver( Object^ /*sender*/, System::Windows::Forms::DragEventArgs^ e )
{
   // Determine whether string data exists in the drop data. If not, then
   // the drop effect reflects that the drop cannot occur.
   if (  !e->Data->GetDataPresent( System::String::typeid ) )
   {
      e->Effect = DragDropEffects::None;
      DropLocationLabel->Text = "None - no string data.";
      return;
   }

   // Set the effect based upon the KeyState.
   if ( (e->KeyState & (8 + 32)) == (8 + 32) && ((e->AllowedEffect & DragDropEffects::Link) == DragDropEffects::Link) )
   {
      // KeyState 8 + 32 = CTRL + ALT
      // Link drag-and-drop effect.
      e->Effect = DragDropEffects::Link;
   }
   else
   if ( (e->KeyState & 32) == 32 && ((e->AllowedEffect & DragDropEffects::Link) == DragDropEffects::Link) )
   {
      // ALT KeyState for link.
      e->Effect = DragDropEffects::Link;
   }
   else
   if ( (e->KeyState & 4) == 4 && ((e->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move) )
   {
      // SHIFT KeyState for move.
      e->Effect = DragDropEffects::Move;
   }
   else
   if ( (e->KeyState & 8) == 8 && ((e->AllowedEffect & DragDropEffects::Copy) == DragDropEffects::Copy) )
   {
      // CTRL KeyState for copy.
      e->Effect = DragDropEffects::Copy;
   }
   else
   if ( (e->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move )
   {
      // By default, the drop action should be move, if allowed.
      e->Effect = DragDropEffects::Move;
   }
   else
            e->Effect = DragDropEffects::None;





   
   // Get the index of the item the mouse is below.
   // The mouse locations are relative to the screen, so they must be
   // converted to client coordinates.
   indexOfItemUnderMouseToDrop = ListDragTarget->IndexFromPoint( ListDragTarget->PointToClient( Point(e->X,e->Y) ) );
   
   // Updates the label text.
   if ( indexOfItemUnderMouseToDrop != ListBox::NoMatches )
   {
      DropLocationLabel->Text = String::Concat( "Drops before item # ", (indexOfItemUnderMouseToDrop + 1) );
   }
   else
            DropLocationLabel->Text = "Drops at the end.";
}
private void ListDragTarget_DragOver(object sender, DragEventArgs e)
{
    // Determine whether string data exists in the drop data. If not, then
    // the drop effect reflects that the drop cannot occur.
    if (!e.Data.GetDataPresent(typeof(System.String)))
    {
        e.Effect = DragDropEffects.None;
        DropLocationLabel.Text = "None - no string data.";
        return;
    }

    // Set the effect based upon the KeyState.
    if ((e.KeyState & (8 + 32)) == (8 + 32) &&
        (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
    {
        // KeyState 8 + 32 = CTRL + ALT

        // Link drag-and-drop effect.
        e.Effect = DragDropEffects.Link;
    }
    else if ((e.KeyState & 32) == 32 &&
        (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
    {
        // ALT KeyState for link.
        e.Effect = DragDropEffects.Link;
    }
    else if ((e.KeyState & 4) == 4 &&
        (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        // SHIFT KeyState for move.
        e.Effect = DragDropEffects.Move;
    }
    else if ((e.KeyState & 8) == 8 &&
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
    {
        // CTRL KeyState for copy.
        e.Effect = DragDropEffects.Copy;
    }
    else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        // By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move;
    }
    else
    {
        e.Effect = DragDropEffects.None;
    }

    // Get the index of the item the mouse is below. 

    // The mouse locations are relative to the screen, so they must be 
    // converted to client coordinates.

    indexOfItemUnderMouseToDrop =
        ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(new Point(e.X, e.Y)));

    // Updates the label text.
    if (indexOfItemUnderMouseToDrop != ListBox.NoMatches)
    {
        DropLocationLabel.Text = "Drops before item #" + (indexOfItemUnderMouseToDrop + 1);
    }
    else
    {
        DropLocationLabel.Text = "Drops at the end.";
    }
}
Private Sub ListDragTarget_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListDragTarget.DragOver
    ' Determine whether string data exists in the drop data. If not, then
    ' the drop effect reflects that the drop cannot occur.
    If Not (e.Data.GetDataPresent(GetType(System.String))) Then

        e.Effect = DragDropEffects.None
        DropLocationLabel.Text = "None - no string data."
        Return
    End If

    ' Set the effect based upon the KeyState.
    If ((e.KeyState And (8 + 32)) = (8 + 32) And
        (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then
        ' KeyState 8 + 32 = CTRL + ALT

        ' Link drag-and-drop effect.
        e.Effect = DragDropEffects.Link

    ElseIf ((e.KeyState And 32) = 32 And
        (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then

        ' ALT KeyState for link.
        e.Effect = DragDropEffects.Link

    ElseIf ((e.KeyState And 4) = 4 And
        (e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

        ' SHIFT KeyState for move.
        e.Effect = DragDropEffects.Move

    ElseIf ((e.KeyState And 8) = 8 And
        (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy) Then

        ' CTRL KeyState for copy.
        e.Effect = DragDropEffects.Copy

    ElseIf ((e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

        ' By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move

    Else
        e.Effect = DragDropEffects.None
    End If

    ' Gets the index of the item the mouse is below. 

    ' The mouse locations are relative to the screen, so they must be 
    ' converted to client coordinates.

    indexOfItemUnderMouseToDrop =
        ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(New Point(e.X, e.Y)))

    ' Updates the label text.
    If (indexOfItemUnderMouseToDrop <> ListBox.NoMatches) Then
        DropLocationLabel.Text = "Drops before item #" & (indexOfItemUnderMouseToDrop + 1)
    Else
        DropLocationLabel.Text = "Drops at the end."
    End If

End Sub

Hinweise

Sie können die Auswirkung eines Drag-and-Drop-Vorgangs vom Zustand eines bestimmten Schlüssels abhängig machen. Beispielsweise können Sie sich entscheiden, Daten zu kopieren oder zu verschieben, je nachdem, ob die STRG- oder UMSCHALT-TASTE während des Drag-and-Drop-Vorgangs gedrückt wird.

Die in der KeyState -Eigenschaft festgelegten Bits identifizieren die Tasten oder Maustasten, die während des Vorgangs gedrückt wurden. Wenn beispielsweise die linke Maustaste gedrückt wird, wird das erste Bit in der KeyState -Eigenschaft festgelegt. Sie können den bitweisen AND-Operator verwenden, um einen bestimmten Schlüsselzustand zu testen.

In der folgenden Tabelle sind die Werte aufgeführt, die für ein angegebenes Ereignis verwendet werden.

Wert Key
1 (Bit 0) Die linke Maustaste.
2 (Bit 1) Die rechte Maustaste.
4 (Bit 2) Die UMSCHALTTASTE.
8 (Bit 3) Die CTRL-TASTE.
16 (Bit 4) Die mittlere Maustaste.
32 (Bit 5) Die ALT-TASTE.

Gilt für: