Revision: 49613
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 26, 2011 21:43 by Currysuechtig
Initial Code
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);
}
}
}
Initial URL
Initial Description
Easily create Windows Forms Shortcuts with this class.
For example: A shortcut to catch pasting via Ctrl + V.
Shortcut sc = new Shortcut(this, new Action(() => MessageBox.Show("Data pasted!")));
sc.Keys.Add(Keys.ControlKey);
sc.Keys.Add(Keys.V);
Initial Title
Windows Forms Keyboard Shortcut on Control
Initial Tags
forms, windows
Initial Language
C#