Domanda

Sono nuovo in C# e sto lavorando su un'applicazione esistente.Ho un viewport DirectX che contiene componenti che desidero poter posizionare utilizzando i tasti freccia.

Attualmente sto sovrascrivendo ProcessCmdKey e acquisendo l'input della freccia e invio un evento OnKeyPress.Funziona, ma voglio essere in grado di utilizzare i modificatori(ALT+CTRL+SPOSTARE).Non appena tengo premuto un modificatore e premo una freccia, non viene attivato alcun evento che sto ascoltando.

Qualcuno ha qualche idea o suggerimento su dove dovrei andare con questo?

È stato utile?

Soluzione

All'interno del ProcessCmdKey sovrascritto, come determini quale tasto è stato premuto?

Il valore di keyData (il secondo parametro) cambierà in base al tasto premuto e agli eventuali tasti modificatori, quindi, ad esempio, premendo la freccia sinistra verrà restituito il codice 37, Maiusc-sinistra restituirà 65573, ctrl-sinistra 131109 e alt-sinistra 262181.

È possibile estrarre i modificatori e il tasto premuto tramite AND con i valori enum appropriati:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    bool shiftPressed = (keyData & Keys.Shift) != 0;
    Keys unmodifiedKey = (keyData & Keys.KeyCode);

    // rest of code goes here
}

Altri suggerimenti

Ho votato a favore La risposta di Tokabi, ma per confrontare le chiavi ci sono alcuni consigli aggiuntivi su StackOverflow.com qui.Ecco alcune funzioni che ho utilizzato per semplificare il tutto.

   public Keys UnmodifiedKey(Keys key)
    {
        return key & Keys.KeyCode;
    }

    public bool KeyPressed(Keys key, Keys test)
    {
        return UnmodifiedKey(key) == test;
    }

    public bool ModifierKeyPressed(Keys key, Keys test)
    {
        return (key & test) == test;
    }

    public bool ControlPressed(Keys key)
    {
        return ModifierKeyPressed(key, Keys.Control);
    }

    public bool AltPressed(Keys key)
    {
        return ModifierKeyPressed(key, Keys.Alt);
    }

    public bool ShiftPressed(Keys key)
    {
        return ModifierKeyPressed(key, Keys.Shift);
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (KeyPressed(keyData, Keys.Left) && AltPressed(keyData))
        {
            int n = code.Text.IndexOfPrev('<', code.SelectionStart);
            if (n < 0) return false;
            if (ShiftPressed(keyData))
            {
                code.ExpandSelectionLeftTo(n);
            }
            else
            {
                code.SelectionStart = n;
                code.SelectionLength = 0;
            }
            return true;
        }
        else if (KeyPressed(keyData, Keys.Right) && AltPressed(keyData))
        {
            if (ShiftPressed(keyData))
            {
                int n = code.Text.IndexOf('>', code.SelectionEnd() + 1);
                if (n < 0) return false;
                code.ExpandSelectionRightTo(n + 1);
            }
            else
            {
                int n = code.Text.IndexOf('<', code.SelectionStart + 1);
                if (n < 0) return false;
                code.SelectionStart = n;
                code.SelectionLength = 0;
            }
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top