Add keyboard input support for plugins with key event handling

This commit is contained in:
Stone_Red
2026-06-30 20:03:30 +02:00
parent f4ac2c65f3
commit 2e219e66c9
5 changed files with 262 additions and 4 deletions
+1 -1
View File
@@ -26,7 +26,7 @@
<RectangleGeometry x:Name="rectangleGeometry" RadiusX="{Binding ElementName=border, Path=CornerRadius.TopLeft}" RadiusY="{Binding ElementName=border, Path=CornerRadius.TopLeft}" /> <RectangleGeometry x:Name="rectangleGeometry" RadiusX="{Binding ElementName=border, Path=CornerRadius.TopLeft}" RadiusY="{Binding ElementName=border, Path=CornerRadius.TopLeft}" />
</Viewbox.Clip> </Viewbox.Clip>
<Border x:Name="imageBorder" BorderBrush="#BAFFEF"> <Border x:Name="imageBorder" BorderBrush="#BAFFEF">
<Image x:Name="image" RenderOptions.EdgeMode="Aliased" Margin="0,0,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Stretch="Uniform" MouseDown="Window_MouseDown" MouseMove="Window_MouseMove" MouseWheel="Window_MouseWheel" /> <Image x:Name="image" RenderOptions.EdgeMode="Aliased" Margin="0,0,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Stretch="Uniform" Focusable="True" MouseDown="Image_MouseDown" MouseMove="Image_MouseMove" MouseWheel="Image_MouseWheel" KeyDown="Image_KeyDown" KeyUp="Image_KeyUp" />
</Border> </Border>
</Viewbox> </Viewbox>
</Grid> </Grid>
+39 -3
View File
@@ -939,8 +939,10 @@ public partial class PluginWindow : Window, IPluginWindow
tileBar.CaptionHeight = ActualHeight - 10; tileBar.CaptionHeight = ActualHeight - 10;
} }
private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) private void Image_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{ {
_ = image.Focus();
ImageSource imageSource = image.Source; ImageSource imageSource = image.Source;
BitmapSource bitmapImage = (BitmapSource)imageSource; BitmapSource bitmapImage = (BitmapSource)imageSource;
double pixelMousePositionX = e.GetPosition(image).X * bitmapImage.PixelWidth / image.ActualHeight; double pixelMousePositionX = e.GetPosition(image).X * bitmapImage.PixelWidth / image.ActualHeight;
@@ -970,7 +972,7 @@ public partial class PluginWindow : Window, IPluginWindow
pluginClassInstance?.OnMouseClick(point, mouseButton); pluginClassInstance?.OnMouseClick(point, mouseButton);
} }
private void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) private void Image_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{ {
ImageSource imageSource = image.Source; ImageSource imageSource = image.Source;
BitmapSource bitmapImage = (BitmapSource)imageSource; BitmapSource bitmapImage = (BitmapSource)imageSource;
@@ -981,7 +983,7 @@ public partial class PluginWindow : Window, IPluginWindow
pluginClassInstance?.OnMouseMove(point); pluginClassInstance?.OnMouseMove(point);
} }
private void Window_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) private void Image_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{ {
ImageSource imageSource = image.Source; ImageSource imageSource = image.Source;
BitmapSource bitmapImage = (BitmapSource)imageSource; BitmapSource bitmapImage = (BitmapSource)imageSource;
@@ -992,5 +994,39 @@ public partial class PluginWindow : Window, IPluginWindow
pluginClassInstance?.OnMouseWheel(point, e.Delta); pluginClassInstance?.OnMouseWheel(point, e.Delta);
} }
private void Image_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (pluginClassInstance is null)
{
return;
}
Keys key = (Keys)e.Key;
System.Windows.Input.ModifierKeys modifiers = e.KeyboardDevice.Modifiers;
bool alt = modifiers.HasFlag(System.Windows.Input.ModifierKeys.Alt);
bool control = modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control);
bool shift = modifiers.HasFlag(System.Windows.Input.ModifierKeys.Shift);
bool windows = modifiers.HasFlag(System.Windows.Input.ModifierKeys.Windows);
pluginClassInstance.OnKeyDown(new KeyEventArgs(key, alt, control, shift, windows));
}
private void Image_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (pluginClassInstance is null)
{
return;
}
Keys key = (Keys)e.Key;
System.Windows.Input.ModifierKeys modifiers = e.KeyboardDevice.Modifiers;
bool alt = modifiers.HasFlag(System.Windows.Input.ModifierKeys.Alt);
bool control = modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control);
bool shift = modifiers.HasFlag(System.Windows.Input.ModifierKeys.Shift);
bool windows = modifiers.HasFlag(System.Windows.Input.ModifierKeys.Windows);
pluginClassInstance.OnKeyUp(new KeyEventArgs(key, alt, control, shift, windows));
}
#endregion Window Events #endregion Window Events
} }
+46
View File
@@ -0,0 +1,46 @@
using System;
namespace DesktopMagic.Api;
/// <summary>
/// Provides data for keyboard events.
/// </summary>
public class KeyEventArgs : EventArgs
{
/// <summary>
/// The key that was pressed or released.
/// </summary>
public Keys Key { get; }
/// <summary>
/// Whether the Alt modifier was pressed.
/// </summary>
public bool Alt { get; }
/// <summary>
/// Whether the Control modifier was pressed.
/// </summary>
public bool Control { get; }
/// <summary>
/// Whether the Shift modifier was pressed.
/// </summary>
public bool Shift { get; }
/// <summary>
/// Whether the Windows logo key was pressed.
/// </summary>
public bool Windows { get; }
/// <summary>
/// Initializes a new instance of the <see cref="KeyEventArgs"/> class.
/// </summary>
public KeyEventArgs(Keys key, bool alt, bool control, bool shift, bool windows)
{
Key = key;
Alt = alt;
Control = control;
Shift = shift;
Windows = windows;
}
}
+160
View File
@@ -0,0 +1,160 @@
#pragma warning disable CS1591
namespace DesktopMagic.Api;
/// <summary>
/// Defines keyboard keys available for plugin input handling.
/// Values match <c>System.Windows.Input.Key</c> for direct casting.
/// </summary>
public enum Keys
{
None = 0,
Cancel = 1,
Back = 2,
Tab = 3,
LineFeed = 4,
Clear = 5,
Return = 6,
Enter = 6,
ShiftKey = 16,
ControlKey = 17,
AltKey = 18,
Pause = 19,
CapsLock = 20,
Escape = 27,
Space = 32,
PageUp = 33,
PageDown = 34,
End = 35,
Home = 36,
Left = 37,
Up = 38,
Right = 39,
Down = 40,
Select = 41,
Print = 42,
Execute = 43,
PrintScreen = 44,
Insert = 45,
Delete = 46,
Help = 47,
D0 = 48,
D1 = 49,
D2 = 50,
D3 = 51,
D4 = 52,
D5 = 53,
D6 = 54,
D7 = 55,
D8 = 56,
D9 = 57,
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
LWin = 91,
RWin = 92,
Apps = 93,
Sleep = 95,
NumPad0 = 96,
NumPad1 = 97,
NumPad2 = 98,
NumPad3 = 99,
NumPad4 = 100,
NumPad5 = 101,
NumPad6 = 102,
NumPad7 = 103,
NumPad8 = 104,
NumPad9 = 105,
Multiply = 106,
Add = 107,
Separator = 108,
Subtract = 109,
Decimal = 110,
Divide = 111,
F1 = 112,
F2 = 113,
F3 = 114,
F4 = 115,
F5 = 116,
F6 = 117,
F7 = 118,
F8 = 119,
F9 = 120,
F10 = 121,
F11 = 122,
F12 = 123,
F13 = 124,
F14 = 125,
F15 = 126,
F16 = 127,
F17 = 128,
F18 = 129,
F19 = 130,
F20 = 131,
F21 = 132,
F22 = 133,
F23 = 134,
F24 = 135,
NumLock = 144,
Scroll = 145,
LeftShift = 160,
RightShift = 161,
LeftCtrl = 162,
RightCtrl = 163,
LeftAlt = 164,
RightAlt = 165,
BrowserBack = 166,
BrowserForward = 167,
BrowserRefresh = 168,
BrowserStop = 169,
BrowserSearch = 170,
BrowserFavorites = 171,
BrowserHome = 172,
VolumeMute = 173,
VolumeDown = 174,
VolumeUp = 175,
MediaNextTrack = 176,
MediaPreviousTrack = 177,
MediaStop = 178,
MediaPlayPause = 179,
LaunchMail = 180,
SelectMedia = 181,
LaunchApplication1 = 182,
LaunchApplication2 = 183,
OemSemicolon = 186,
OemPlus = 187,
OemComma = 188,
OemMinus = 189,
OemPeriod = 190,
OemQuestion = 191,
OemTilde = 192,
OemOpenBrackets = 219,
OemPipe = 220,
OemCloseBrackets = 221,
OemQuotes = 222,
Oem8 = 223,
OemBackslash = 226,
}
+16
View File
@@ -99,6 +99,22 @@ public abstract class Plugin
{ {
} }
/// <summary>
/// Occurs when a key is pressed while the plugin window has keyboard focus.
/// </summary>
/// <param name="e">The key event data.</param>
public virtual void OnKeyDown(KeyEventArgs e)
{
}
/// <summary>
/// Occurs when a key is released while the plugin window has keyboard focus.
/// </summary>
/// <param name="e">The key event data.</param>
public virtual void OnKeyUp(KeyEventArgs e)
{
}
/// <summary> /// <summary>
/// Occurs when the application's theme has changed. /// Occurs when the application's theme has changed.
/// </summary> /// </summary>