DragEventArgs.KeyState Propiedad

Definición

Obtiene el estado actual de las teclas MAYÚS, CTRL y ALT, así como el estado de los botones del mouse.

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

Valor de propiedad

Estado actual de las teclas MAYÚS, CTRL y ALT y de los botones del mouse.

Ejemplos

En el ejemplo siguiente se muestra una operación de arrastrar y colocar entre dos ListBox controles. En el ejemplo se llama al DoDragDrop método cuando se inicia la acción de arrastrar. La acción de arrastrar se inicia si el mouse se ha movido más que SystemInformation.DragSize desde la ubicación del mouse durante el MouseDown evento. El IndexFromPoint método se usa para determinar el índice del elemento que se va a arrastrar durante el MouseDown evento.

En el ejemplo también se muestra el uso de cursores personalizados para la operación de arrastrar y colocar. En el ejemplo se supone que existen dos archivos de cursor y 3dwarro.cur3dwno.cur, en el directorio de la aplicación, para los cursores personalizados de arrastrar y sin colocar, respectivamente. Los cursores personalizados se usarán si UseCustomCursorsCheckCheckBox está activado. Los cursores personalizados se establecen en el GiveFeedback controlador de eventos.

El estado del teclado se evalúa en el DragOver controlador de eventos de la derecha ListBoxpara determinar cuál será la operación de arrastre según el estado de las teclas MAYÚS, CTRL, ALT o CTRL+ALT. La ubicación en la ListBox que se produciría la colocación también se determina durante el DragOver evento. Si los datos que se van a quitar no son , Stringse establece DragDropEffects.Noneen DragEventArgs.Effect . Por último, el estado de la colocación se muestra en DropLocationLabelLabel.

Los datos que se van a quitar de la derecha ListBox se determinan en el DragDrop controlador de eventos y el String valor se agrega en el lugar adecuado de ListBox. Si la operación de arrastrar se mueve fuera de los límites del formulario, la operación de arrastrar y colocar se cancela en el QueryContinueDrag controlador de eventos.

Este fragmento de código muestra el uso de la DragEventArgs clase . Consulte el método para obtener el DoDragDrop ejemplo de código completo.

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

Comentarios

Puede hacer que el efecto de una operación de arrastrar y colocar dependa del estado de una clave determinada. Por ejemplo, puede decidir copiar o mover datos en función de si se presionan las teclas CTRL o MAYÚS durante la operación de arrastrar y colocar.

Los bits que se establecen en la KeyState propiedad identifican las teclas o botones del mouse que se presionaron durante la operación. Por ejemplo, si se presiona el botón izquierdo del mouse, se establece el primer bit de la KeyState propiedad . Puede usar el operador AND bit a bit para probar un estado de clave determinado.

En la tabla siguiente se enumeran los valores que se usan para un evento especificado.

Valor Clave
1 (bit 0) Botón primario del mouse.
2 (bit 1) Botón secundario del mouse.
4 (bit 2) Tecla MAYÚS.
8 (bit 3) Tecla CTRL.
16 (bit 4) Botón central del mouse.
32 (bit 5) Tecla ALT.

Se aplica a