- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- namespace Tools
- {
- /// <summary>
- /// Tasten-Shortcut auf einem Control.
- /// </summary>
- public class Shortcut
- {
- /// <summary>
- /// Aktuell gedrückte Tasten.
- /// </summary>
- private List<Keys> _CurrentlyPressedKeys = new List<Keys>();
- private Control _Control;
- private List<Keys> _Keys = new List<Keys>();
- private Action _Action;
- /// <summary>
- /// Control, auf dem die Tastendrücke überwacht werden sollen.
- /// </summary>
- public Control Control
- {
- get { return _Control; }
- set
- {
- this.OnControlChanging();
- _Control = value;
- this.OnControlChanged();
- }
- }
- /// <summary>
- /// Keys, die gedrückt werden müssen, um die Aktion auszul?sen.
- /// </summary>
- public List<Keys> Keys
- {
- get { return _Keys; }
- }
- /// <summary>
- /// Aktion, die ausgel?st werden soll.
- /// </summary>
- public Action Action
- {
- get { return _Action; }
- set { _Action = value; }
- }
- /// <summary>
- /// Initialisiert einen einen Tasten-Shortcut.
- /// </summary>
- public Shortcut()
- {
- }
- /// <summary>
- /// Initialisiert einen einen Tasten-Shortcut.
- /// </summary>
- /// <param name="control">Control, auf dem die Tastendrücke überwacht werden sollen.</param>
- public Shortcut(Control control)
- {
- this.Control = control;
- }
- /// <summary>
- /// Initialisiert einen einen Tasten-Shortcut.
- /// </summary>
- /// <param name="control">Control, auf dem die Tastendrücke überwacht werden sollen.</param>
- /// <param name="action">Aktion, die ausgel?st werden soll.</param>
- public Shortcut(Control control, Action action)
- {
- this.Control = control;
- this.Action = action;
- }
- /// <summary>
- /// Wird aufgerufen, wenn sich die Control-Eigenschaft gerade ?ndert.
- /// </summary>
- protected virtual void OnControlChanging()
- {
- // Abonnements der alten Key-Events entfernen
- if (this.Control != null)
- {
- this.Control.KeyDown -= new KeyEventHandler(Control_KeyDown);
- this.Control.KeyUp -= new KeyEventHandler(Control_KeyUp);
- }
- }
- /// <summary>
- /// Wird aufgerufen, wenn die Control-Eigenschaft ge?ndert wurde.
- /// </summary>
- protected virtual void OnControlChanged()
- {
- // Neue Key-Events abonnieren
- if (this.Control != null)
- {
- this.Control.KeyDown += new KeyEventHandler(Control_KeyDown);
- this.Control.KeyUp += new KeyEventHandler(Control_KeyUp);
- }
- }
- /// <summary>
- /// überprüft, ob die aktuell gedrückten Tasten dem Shortcut entsprechen.
- /// </summary>
- protected void CheckKeys()
- {
- bool flag = true;
- foreach (Keys key in this.Keys)
- {
- if (!_CurrentlyPressedKeys.Contains(key))
- flag = false;
- }
- if (flag)
- this.Action();
- }
- /// <summary>
- /// Wird aufgerufen, wenn eine Taste auf dem Control gedrückt wurde.
- /// </summary>
- private void Control_KeyDown(object sender, KeyEventArgs e)
- {
- // Aktuell gedrückte Taste speichern
- _CurrentlyPressedKeys.Add(e.KeyCode);
- // Auf Shortcut überprüfen
- this.CheckKeys();
- }
- /// <summary>
- /// Wird aufgerufen, wenn eine Taste auf dem Control losgelassen wurde.
- /// </summary>
- private void Control_KeyUp(object sender, KeyEventArgs e)
- {
- // Losgelassene Taste entfernen
- _CurrentlyPressedKeys.Remove(e.KeyCode);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/221120137429.html
来源: http://www.codesnippet.cn/detail/221120137429.html