Skip to content

Keyboard modifiers are properly handled; Shortcuts are not executed on key release; KeyState constructor clean #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Pixed.Application/ViewModels/PaintCanvasViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class PaintCanvasViewModel : ExtendedViewModel, IDisposable
private Frame _frame;
private Point _lastWindowSize;
private bool _disposedValue;
private KeyState _currentKeyState = new(Avalonia.Input.Key.None, false, false, false);
private KeyState _currentKeyState = new();
private string _projectSizeText;
private string _mouseCoordinatesText;
private int _toolSize = 1;
Expand Down
2 changes: 2 additions & 0 deletions Pixed.Application/Windows/MainView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private void View_KeyUp(object? sender, KeyEventArgs e)
Keyboard.ProcessReleased(e.Key);
Subjects.KeyState.OnNext(new KeyState(
e.Key,
false,
Keyboard.Modifiers.HasFlag(KeyModifiers.Shift),
Keyboard.Modifiers.HasFlag(KeyModifiers.Control),
Keyboard.Modifiers.HasFlag(KeyModifiers.Alt)));
Expand All @@ -33,6 +34,7 @@ private void View_KeyDown(object? sender, KeyEventArgs e)
Keyboard.ProcessPressed(e.Key);
Subjects.KeyState.OnNext(new KeyState(
e.Key,
true,
Keyboard.Modifiers.HasFlag(KeyModifiers.Shift),
Keyboard.Modifiers.HasFlag(KeyModifiers.Control),
Keyboard.Modifiers.HasFlag(KeyModifiers.Alt)));
Expand Down
2 changes: 2 additions & 0 deletions Pixed.Application/Windows/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private void Window_KeyUp(object? sender, KeyEventArgs e)
Keyboard.ProcessReleased(e.Key);
Subjects.KeyState.OnNext(new KeyState(
e.Key,
false,
Keyboard.Modifiers.HasFlag(KeyModifiers.Shift),
Keyboard.Modifiers.HasFlag(KeyModifiers.Control),
Keyboard.Modifiers.HasFlag(KeyModifiers.Alt)));
Expand All @@ -71,6 +72,7 @@ private void Window_KeyDown(object? sender, KeyEventArgs e)
Keyboard.ProcessPressed(e.Key);
Subjects.KeyState.OnNext(new KeyState(
e.Key,
true,
Keyboard.Modifiers.HasFlag(KeyModifiers.Shift),
Keyboard.Modifiers.HasFlag(KeyModifiers.Control),
Keyboard.Modifiers.HasFlag(KeyModifiers.Alt)));
Expand Down
26 changes: 24 additions & 2 deletions Pixed.Common/Services/Keyboard/KeyState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@

namespace Pixed.Common.Services.Keyboard;

public class KeyState(Key key, bool isShift, bool isCtrl, bool isAlt) : IEquatable<KeyState>
public class KeyState(Key key, bool pressed, bool isShift, bool isCtrl, bool isAlt) : IEquatable<KeyState>
{
public Key Key { get; } = key;
public bool Pressed { get; } = pressed;
public bool IsShift { get; } = isShift;
public bool IsCtrl { get; } = isCtrl;
public bool IsAlt { get; } = isAlt;

public KeyState() : this(Key.None, false, false, false, false)
{
}

public override bool Equals(object? obj)
{
if (obj is KeyState state)
Expand All @@ -26,6 +31,23 @@ public bool Equals(KeyState? other)
{
return false;
}
return other.Key == Key && other.IsShift == IsShift && other.IsCtrl == IsCtrl && other.IsAlt == IsAlt;
return other.Key == Key && other.IsShift == IsShift && other.IsCtrl == IsCtrl && other.IsAlt == IsAlt && other.Pressed == Pressed;
}

public override int GetHashCode()
{
HashCode hc = new();
hc.Add(Key);
hc.Add(IsShift);
hc.Add(IsCtrl);
hc.Add(IsAlt);
hc.Add(Pressed);

return hc.ToHashCode();
}

public static KeyState Control(Key key)
{
return new KeyState(key, true, false, true, false);
}
}
4 changes: 2 additions & 2 deletions Pixed.Common/Services/Keyboard/ShortcutService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public ShortcutService(ApplicationData applicationData)
}
});

Add(new KeyState(Key.Z, false, true, false), () =>
Add(KeyState.Control(Key.Z), () =>
{
_applicationData.CurrentModel.Undo();
Subjects.ProjectModified.OnNext(_applicationData.CurrentModel);
});
Add(new KeyState(Key.Y, false, true, false), () =>
Add(KeyState.Control(Key.Y), () =>
{
_applicationData.CurrentModel.Redo();
Subjects.ProjectModified.OnNext(_applicationData.CurrentModel);
Expand Down
10 changes: 5 additions & 5 deletions Pixed.Common/Tools/SelectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public SelectionManager(ApplicationData applicationData, ShortcutService shortcu
_clipboardHandle = clipboardHandle;
Subjects.SelectionCreating.Subscribe(OnSelectionCreated);
Subjects.SelectionDismissed.Subscribe(OnSelectionDismissed);
shortcutService.Add(new KeyState(Key.C, false, true, false), async () => await Copy());
shortcutService.Add(new KeyState(Key.X, false, true, false), async () => await Cut());
shortcutService.Add(new KeyState(Key.V, false, true, false), async () => await Paste());
shortcutService.Add(new KeyState(Key.A, false, true, false), SelectAll);
shortcutService.Add(new KeyState(Key.Delete, false, false, false), Erase);
shortcutService.Add(KeyState.Control(Key.C), async () => await Copy());
shortcutService.Add(KeyState.Control(Key.X), async () => await Cut());
shortcutService.Add(KeyState.Control(Key.V), async () => await Paste());
shortcutService.Add(KeyState.Control(Key.A), SelectAll);
shortcutService.Add(new KeyState(Key.Delete, true, false, false, false), Erase);
}

public void Clear()
Expand Down
Loading