Skip to content

Commit

Permalink
Fix exception when key is out of range
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
sabihoshi committed Apr 12, 2021
1 parent 3cb9182 commit 6f9beda
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions GenshinLyreMidiPlayer/Core/LyrePlayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using WindowsInput;
using WindowsInput.Native;
Expand Down Expand Up @@ -55,27 +56,29 @@ public static int TransposeNote(int noteId)

public static void PlayNote(int noteId, Layout selectedLayout)
{
var key = GetKey(noteId, selectedLayout);
Input.Keyboard.KeyPress(key);
InteractNote(noteId, selectedLayout, Input.Keyboard.KeyPress);
}

public static void NoteDown(int noteId, Layout selectedLayout)
{
var key = GetKey(noteId, selectedLayout);
Input.Keyboard.KeyDown(key);
InteractNote(noteId, selectedLayout, Input.Keyboard.KeyDown);
}

public static void NoteUp(int noteId, Layout selectedLayout)
{
var key = GetKey(noteId, selectedLayout);
Input.Keyboard.KeyUp(key);
InteractNote(noteId, selectedLayout, Input.Keyboard.KeyUp);
}

private static VirtualKeyCode GetKey(int noteId, Layout selectedLayout)
public static void InteractNote(int noteId, Layout selectedLayout,
Func<VirtualKeyCode, IKeyboardSimulator> action)
{
var layout = GetLayout(selectedLayout);
var keyIndex = LyreNotes.IndexOf(noteId);
var key = GetLayout(selectedLayout)[keyIndex];
return key;
if (keyIndex < 0 || keyIndex > layout.Count)
return;

var key = layout[keyIndex];
action.Invoke(key);
}
}
}
4 changes: 2 additions & 2 deletions GenshinLyreMidiPlayer/Views/SettingsPageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
<ui:ProgressRing IsActive="{Binding IsCheckingUpdate}" />
</ui:SimpleStackPanel>

<ui:ToggleSwitch Header="Automatically check updates" IsEnabled="{Binding AutoCheckUpdates}" />
<ui:ToggleSwitch Header="Include beta updates" IsEnabled="{Binding IncludeBetaUpdates}" />
<ui:ToggleSwitch Header="Automatically check updates" IsOn="{Binding AutoCheckUpdates}" />
<ui:ToggleSwitch Header="Include beta updates" IsOn="{Binding IncludeBetaUpdates}" />
</ui:SimpleStackPanel>
</GroupBox>
</ui:SimpleStackPanel>
Expand Down

0 comments on commit 6f9beda

Please sign in to comment.