Skip to content

Commit 036ad8a

Browse files
feat: allowing clearing of keystroke with ESC
I would have preferred to clear with right-click but the documentation isn't forthcoming with how to override the right-click behavior on a button. Hopefully no one wants to use ESC. Also, added some boilerplate text when there is nothing bound. Also, added the loading icon with "press any key" text while waiting for input.
1 parent 05839ac commit 036ad8a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Nullinside.TwitchStreamingTools/Controls/Keybind.axaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:viewModels="clr-namespace:Nullinside.TwitchStreamingTools.Controls.ViewModels"
6+
xmlns:controls="clr-namespace:Nullinside.TwitchStreamingTools.Controls"
67
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
78
x:Class="Nullinside.TwitchStreamingTools.Controls.Keybind"
89
x:DataType="viewModels:KeybindViewModel">
910
<StackPanel>
10-
<Button Content="{Binding Keybind}" Command="{Binding ListenForKeystroke}" />
11+
<Button IsVisible="{Binding !Listening}"
12+
Content="{Binding Keybind, TargetNullValue='[None]'}"
13+
Command="{Binding ListenForKeystroke}"
14+
ToolTip.Tip="ESC to unset" />
15+
<StackPanel IsVisible="{Binding Listening}"
16+
Orientation="Horizontal">
17+
<controls:Loading Width="25"
18+
Height="25" />
19+
<Label>Press a key...</Label>
20+
</StackPanel>
1121
</StackPanel>
1222
</UserControl>

src/Nullinside.TwitchStreamingTools/Controls/ViewModels/KeybindViewModel.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Reactive;
22

3+
using Nullinside.TwitchStreamingTools.Models;
34
using Nullinside.TwitchStreamingTools.Services;
45
using Nullinside.TwitchStreamingTools.ViewModels;
56

@@ -21,6 +22,11 @@ public class KeybindViewModel : ViewModelBase {
2122
/// </summary>
2223
private Keybind? _keybind;
2324

25+
/// <summary>
26+
/// True if listening for keystrokes, false otherwise.
27+
/// </summary>
28+
private bool _listening;
29+
2430
/// <summary>
2531
/// Initializes a new instance of the <see cref="KeybindViewModel" /> class.
2632
/// </summary>
@@ -38,6 +44,14 @@ public Keybind? Keybind {
3844
set => this.RaiseAndSetIfChanged(ref _keybind, value);
3945
}
4046

47+
/// <summary>
48+
/// True if listening for keystrokes, false otherwise.
49+
/// </summary>
50+
public bool Listening {
51+
get => _listening;
52+
set => this.RaiseAndSetIfChanged(ref _listening, value);
53+
}
54+
4155
/// <summary>
4256
/// Listens for keystrokes.
4357
/// </summary>
@@ -47,6 +61,7 @@ public Keybind? Keybind {
4761
/// Starts listening for keystrokes.
4862
/// </summary>
4963
private void StartListenKeystroke() {
64+
Listening = true;
5065
_service.OnKeystroke -= OnKeystroke;
5166
_service.OnKeystroke += OnKeystroke;
5267
}
@@ -60,7 +75,8 @@ private void OnKeystroke(Keybind keybind) {
6075
return;
6176
}
6277

63-
Keybind = keybind;
78+
Keybind = keybind.Key == Keys.Escape ? null : keybind;
6479
_service.OnKeystroke -= OnKeystroke;
80+
Listening = false;
6581
}
6682
}

0 commit comments

Comments
 (0)