Skip to content

Commit 7a4176a

Browse files
committed
Listened for "delete" and "ctrl+s" key presses to delete and space respectively when treeview has focus
1 parent 4b47dd4 commit 7a4176a

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

GHelper/GHelper/View/MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
<SplitView.Resources>
2323

2424
<DataTemplate x:Key="ApplicationTemplate" x:DataType="viewModel:ApplicationViewModel">
25-
<TreeViewItem ItemsSource="{x:Bind Path=Profiles, Mode=OneWay}">
25+
<TreeViewItem ItemsSource="{x:Bind Path=Profiles, Mode=OneWay}" PreviewKeyDown="{x:Bind Path=HandleKeyboardInput}">
2626
<selector:ApplicationSelectorView Application="{x:Bind Mode=OneWay}"/>
2727
</TreeViewItem>
2828
</DataTemplate>
2929

3030
<DataTemplate x:Key="ProfileTemplate" x:DataType="viewModel:ProfileViewModel">
31-
<TreeViewItem>
31+
<TreeViewItem PreviewKeyDown="{x:Bind Path=HandleKeyboardInput}">
3232
<selector:ProfileSelectorView Profile="{x:Bind Mode=OneWay}" />
3333
</TreeViewItem>
3434
</DataTemplate>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Windows.System;
2+
using Windows.UI.Core;
3+
4+
namespace GHelper.View.Utility
5+
{
6+
public static class KeyboardState
7+
{
8+
public static CoreVirtualKeyStates ControlKeyState
9+
{
10+
get
11+
{
12+
CoreVirtualKeyStates controlKeyState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Control);
13+
14+
if ((controlKeyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down)
15+
{
16+
return CoreVirtualKeyStates.Down;
17+
}
18+
else
19+
{
20+
return CoreVirtualKeyStates.None;
21+
}
22+
}
23+
}
24+
}
25+
}

GHelper/GHelper/ViewModel/GHubRecordViewModel.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System.ComponentModel;
22
using System.Reflection;
33
using System.Runtime.CompilerServices;
4+
using Windows.System;
5+
using Windows.UI.Core;
46
using GHelper.Event;
7+
using GHelper.View.Utility;
58
using GHelperLogic.Model;
9+
using Microsoft.UI.Xaml.Input;
610

711
namespace GHelper.ViewModel
812
{
@@ -64,6 +68,23 @@ public void FireDeletedEvent()
6468
{
6569
UserDeletedRecord?.Invoke(this);
6670
}
71+
72+
public void HandleKeyboardInput(object sender, KeyRoutedEventArgs keyboardEventInfo)
73+
{
74+
switch (keyboardEventInfo.Key)
75+
{
76+
case VirtualKey.S:
77+
if (KeyboardState.ControlKeyState == CoreVirtualKeyStates.Down)
78+
{
79+
FireSaveEvent();
80+
}
81+
break;
82+
83+
case VirtualKey.Delete or VirtualKey.Back:
84+
FireDeletedEvent();
85+
break;
86+
}
87+
}
6788

6889
protected abstract void OnPropertyChanged([CallerMemberName] string? propertyName = null);
6990
}

0 commit comments

Comments
 (0)