[EDIT 3] Ho risolto il problema "utilizzando" la versione "strana". Almeno per le chiavi più importanti. È sufficiente per il mio caso, in cui voglio verificare che ALT e ALT + A non siano uguali (assicurandoti quindi che A non venga premuto). Non perfetto, ma già molto tempo per un problema così piccolo. Grazie per tutte le risposte comunque ... [EDIT 3]Rileva se viene premuto un tasto qualsiasi in C# (non A, B, ma nessuno)
[EDIT 4] risolto molto più pulito grazie alla 280Z28 [/ EDIT 4]
so come verificare la presenza di tasti modificatori e come testare per una singola chiave. Il problema è che voglio controllare se viene premuto un tasto. Il seguente approccio sembra "strano" :-)
WPF applicazione scritta in C#
if (Keyboard.IsKeyDown(Key.A)) return true;
if (Keyboard.IsKeyDown(Key.B)) return true;
if (Keyboard.IsKeyDown(Key.C)) return true;
So che è un enum, così ho pensato di un ciclo, ma qual è il "maggior numero" usare. E questo è possibile? btw, è un caso molto speciale, normalmente userei un evento, ma in questo caso devo farlo in questo modo. Sfortunatamente, non esiste una tastiera "elenco" Keyboard.CurrentlyDown. Almeno non l'ho visto.
Grazie, Chris
EDIT: Ok, perché sembra essere un affare più grande, qui la ragione di questo: Ho definito un "keyset" che funge da DictionaryKey per funzioni personalizzate. Se qualcuno fa clic su un elemento, il wrapper scorre il dizionario e controlla se uno qualsiasi dei "Keyset" predefiniti è attivo.
Ciò consente di definire semplici trigger, come ad es. Esegui questa funzione se ALT + A + B viene premuto. Un'altra opzione è ad es. Esegui questa funzione se ALT + STRG + A viene premuto (durante un clic del mouse su un elemento WPF).
L'unico "problema" con l'implementazione corrente, se definisco un Keyset che NON contiene alcun tasto REAL, come eseguire se ALT viene premuto, viene attivato anche se si preme ALT + A. Oh, mentre scrivo questo, mi rendo conto che c'è un altro problema. Al momento ALT + A + B si attiva anche se si preme ALT + A + B + C.
Forse il mio approccio è sbagliato, e dovrei creare un "rilevatore di tasti statici" e confrontare il keyset con i suoi valori (acquisiti tramite eventi). Farò un tentativo.
EDIT 2 Questo non funziona, almeno non in modo semplice. Ho bisogno di un FrameworkElement da collegare a KeyDown, ma non ce l'ho in un costruttore statico. E io non sono interessato a KeyDownEvents di un certo elemento, ma "globalmente" ... Penso che spingo a rimandare questo problema, non è così importante. Eppure, se qualcuno sa di una migliore approccio diverso ...
Così a lungo, per chi se ne frega, qui qualche codice:
public class KeyModifierSet
{
internal readonly HashSet<Key> Keys = new HashSet<Key>();
internal readonly HashSet<ModifierKeys> MKeys = new HashSet<ModifierKeys>();
public override int GetHashCode()
{
int hash = Keys.Count + MKeys.Count;
foreach (var t in Keys)
{
hash *= 17;
hash = hash + t.GetHashCode();
}
foreach (var t in MKeys)
{
hash *= 19;
hash = hash + t.GetHashCode();
}
return hash;
}
public override bool Equals(object obj)
{
return Equals(obj as KeyModifierSet);
}
public bool Equals(KeyModifierSet other)
{
// Check for null
if (ReferenceEquals(other, null))
return false;
// Check for same reference
if (ReferenceEquals(this, other))
return true;
// Check for same Id and same Values
return Keys.SetEquals(other.Keys) && MKeys.SetEquals(other.MKeys);
}
public bool IsActive()
{
foreach (var k in Keys)
if (Keyboard.IsKeyUp(k)) return false;
if ((Keys.Count == 0) && !Keyboard.IsKeyDown(Key.None)) return false;
foreach (var k in MKeys)
if ((Keyboard.Modifiers & k) == 0) return false;
if ((MKeys.Count == 0) && Keyboard.Modifiers > 0) return false;
return true;
}
public KeyModifierSet(ModifierKeys mKey)
{
MKeys.Add(mKey);
}
public KeyModifierSet()
{
}
public KeyModifierSet(Key key)
{
Keys.Add(key);
}
public KeyModifierSet(Key key, ModifierKeys mKey)
{
Keys.Add(key);
MKeys.Add(mKey);
}
public KeyModifierSet Add(Key key)
{
Keys.Add(key);
return this;
}
public KeyModifierSet Add(ModifierKeys key)
{
MKeys.Add(key);
return this;
}
}
Qual è la vostra applicazione? Console? Windows Forms? WPF? Sito web? – jrista
Vedere titolo ... .NET C# –
Ciò non aiuta. –