-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
142 lines (107 loc) · 4.09 KB
/
MainWindow.xaml.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1_WatchingKeys;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string previousSequence = string.Empty;
bool HasModifier = false;
bool hasLetter = false;
private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
MainTextBlock.Text = string.Empty;
IEnumerable<Key> downKeys = GetDownKeys();
bool containsWin = downKeys.Contains(Key.LWin) || downKeys.Contains(Key.RWin);
bool containsShift = downKeys.Contains(Key.LeftShift) || downKeys.Contains(Key.RightShift);
bool containsCtrl = downKeys.Contains(Key.LeftCtrl) || downKeys.Contains(Key.RightCtrl);
bool containsAlt = downKeys.Contains(Key.LeftAlt) || downKeys.Contains(Key.RightAlt);
IEnumerable<Key> justLetterKeys = RemoveModifierKeys(downKeys);
hasLetter = justLetterKeys.Any();
HasModifier = containsWin || containsShift || containsCtrl || containsAlt;
if (!(hasLetter && HasModifier))
return;
List<string> keyStrings = [];
if (containsWin)
keyStrings.Add("Win");
if (containsShift)
keyStrings.Add("Shift");
if (containsCtrl)
keyStrings.Add("Ctrl");
if (containsAlt)
keyStrings.Add("Alt");
foreach (Key key in justLetterKeys)
keyStrings.Add(key.ToString());
string currentSequence = string.Join('+', keyStrings);
if (string.IsNullOrEmpty(currentSequence))
{
MainTextBlock.Text += Environment.NewLine;
MainTextBlock.Text += "< No Keys Pressed >";
}
if (currentSequence.Equals(previousSequence))
return;
MainTextBlock.Text += Environment.NewLine;
MainTextBlock.Text += currentSequence;
previousSequence = currentSequence;
Scroller.ScrollToBottom();
}
private static IEnumerable<Key> RemoveModifierKeys(IEnumerable<Key> downKeys)
{
HashSet<Key> filteredKeys = new(downKeys);
filteredKeys.Remove(Key.LWin);
filteredKeys.Remove(Key.RWin);
filteredKeys.Remove(Key.LeftShift);
filteredKeys.Remove(Key.RightShift);
filteredKeys.Remove(Key.LeftCtrl);
filteredKeys.Remove(Key.RightCtrl);
filteredKeys.Remove(Key.LeftAlt);
filteredKeys.Remove(Key.RightAlt);
return filteredKeys;
}
private static readonly byte[] DistinctVirtualKeys = Enumerable
.Range(0, 256)
.Select(KeyInterop.KeyFromVirtualKey)
.Where(item => item != Key.None)
.Distinct()
.Select(item => (byte)KeyInterop.VirtualKeyFromKey(item))
.ToArray();
public static IEnumerable<Key> GetDownKeys()
{
var keyboardState = new byte[256];
GetKeyboardState(keyboardState);
List<Key> downKeys = [];
for (var index = 0; index < DistinctVirtualKeys.Length; index++)
{
var virtualKey = DistinctVirtualKeys[index];
if ((keyboardState[virtualKey] & 0x80) != 0)
downKeys.Add(KeyInterop.KeyFromVirtualKey(virtualKey));
}
return downKeys;
}
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool GetKeyboardState(byte[] keyState);
private void Window_Deactivated(object sender, EventArgs e)
{
MainTextBlock.Text = string.Empty;
MainTextBlock.Text += Environment.NewLine;
MainTextBlock.Text += "Lost focus?";
MainTextBlock.Text += Environment.NewLine;
MainTextBlock.Text += "Maybe that shortcut is taken";
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (hasLetter && HasModifier)
return;
MainTextBlock.Text = string.Empty;
MainTextBlock.Text += Environment.NewLine;
MainTextBlock.Text += "Not valid command. Need to include a modifier and a letter";
}
}