1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
namespace Shortcut
{
/// <summary>
/// Erstellt einen neuen Shortcut
/// </summary>
public class Shortcut
{
#region variables
private Window mWindow;
private List<Key> mKeys = new List<Key>();
private Action mAction;
private List<Key> pressedKeys = new List<Key>();
#endregion
#region properties
/// <summary>
/// Fenster, auf dem Shortcut gedrückt werden soll.
/// </summary>
public Window Window
{
get { return mWindow; }
set { mWindow = value; }
}
/// <summary>
/// Tasten, die gedrückt werden müssen, damit Aktion ausgeführt wird.
/// </summary>
public List<Key> Keys
{
get { return mKeys; }
set { mKeys = value; }
}
/// <summary>
/// Aktion, die bei Drücken der Shortcut-Tasten ausgeführt werden soll.
/// </summary>
public Action Action
{
get { return mAction; }
set { mAction = value; }
}
#endregion
#region ctor
/// <summary>
/// Initialisiert ein neues Shortcut.
/// </summary>
/// <param name="keys">Shortcuttasten, die Aktion auslösen.</param>
/// <param name="action">Aktion, die ausgelöst werden soll.</param>
public Shortcut(Window window, List<Key> keys, Action action)
{
this.Window = window;
this.Window.KeyDown += new KeyEventHandler(Window_KeyDown);
this.Window.KeyUp += new KeyEventHandler(Window_KeyUp);
this.Keys = keys;
this.Action = action;
}
#endregion
#region events
/// <summary>
/// Wird ausgelöst, wenn auf dem Zielfenster eine Taste gedrückt wird.
/// </summary>
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (!this.pressedKeys.Contains(e.Key))
this.pressedKeys.Add(e.Key);
int counter = 0;
foreach (Key key in this.Keys)
{
if (this.pressedKeys.Contains(key))
counter++;
}
if (this.Keys.Count > 0 && counter == this.Keys.Count)
{
this.Action();
this.pressedKeys.Clear();
}
}
/// <summary>
/// Wird ausgelöst, wenn auf dem Zielfenster eine Taste losgelassen wird.
/// </summary>
private void Window_KeyUp(object sender, KeyEventArgs e)
{
this.pressedKeys.Remove(e.Key);
}
#endregion
}
}
|