C #: Come i tasti corretti pressato da KEYDATA KeyEventArgs'
-
21-08-2019 - |
Domanda
sto intrappolando un evento KeyDown
e ho bisogno di essere in grado di verificare se i tasti premuti attuali sono: Ctrl + Maiusc + M ?
Lo so che ho bisogno di usare il e.KeyData
dal KeyEventArgs
, il Keys
enum e qualcosa con Enum Bandiere e bit, ma non sono sicuro su come verificare per la combinazione.
Soluzione
È necessario utilizzare le modificatori proprietà della classe KeyEventArgs.
Qualcosa di simile:
//asumming e is of type KeyEventArgs (such as it is
// on a KeyDown event handler
// ..
bool ctrlShiftM; //will be true if the combination Ctrl + Shift + M is pressed, false otherwise
ctrlShiftM = ((e.KeyCode == Keys.M) && // test for M pressed
((e.Modifiers & Keys.Shift) != 0) && // test for Shift modifier
((e.Modifiers & Keys.Control) != 0)); // test for Ctrl modifier
if (ctrlShiftM == true)
{
Console.WriteLine("[Ctrl] + [Shift] + M was pressed");
}
Altri suggerimenti
Credo che la sua più facile da usare in questo modo:
if(e.KeyData == (Keys.Control | Keys.G))
È possibile controllare utilizzando una tecnica simile al seguente:
if(Control.ModifierKeys == Keys.Control && Control.ModifierKeys == Keys.Shift)
Questo, in combinazione con i normali controlli chiave vi darà la risposta che cercate.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow