Add desktop input mode switch
This commit is contained in:
@@ -2,6 +2,12 @@ namespace WirelessTextSyncer.Windows.Services;
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
public enum TextInjectionMode
|
||||
{
|
||||
ClipboardPaste,
|
||||
SendInputTyping
|
||||
}
|
||||
|
||||
public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
{
|
||||
private const uint InputKeyboard = 1;
|
||||
@@ -16,6 +22,8 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
private const ushort VirtualKeyV = 0x56;
|
||||
private static readonly IntPtr EnglishKeyboardLayout = LoadKeyboardLayout("00000409", KlfActivate);
|
||||
|
||||
public TextInjectionMode Mode { get; set; } = TextInjectionMode.ClipboardPaste;
|
||||
|
||||
public void InsertText(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
@@ -23,7 +31,13 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
return;
|
||||
}
|
||||
|
||||
AppLogger.Info($"Pasting text length: {text.Length}");
|
||||
AppLogger.Info($"Inserting text length: {text.Length}. Mode: {Mode}.");
|
||||
if (Mode == TextInjectionMode.SendInputTyping)
|
||||
{
|
||||
TypeTextWithSendInput(text);
|
||||
return;
|
||||
}
|
||||
|
||||
PasteTextViaClipboard(text);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,16 @@ namespace WirelessTextSyncer.Windows.Tray;
|
||||
|
||||
public sealed class TrayApplicationContext : ApplicationContext
|
||||
{
|
||||
private readonly KeyboardInjectionService keyboard;
|
||||
private readonly WebSocketServerService server;
|
||||
private readonly NotifyIcon notifyIcon;
|
||||
private ToolStripMenuItem? clipboardPasteMenuItem;
|
||||
private ToolStripMenuItem? sendInputTypingMenuItem;
|
||||
|
||||
public TrayApplicationContext()
|
||||
{
|
||||
AppLogger.Info("Tray application starting.");
|
||||
var keyboard = new KeyboardInjectionService();
|
||||
keyboard = new KeyboardInjectionService();
|
||||
var handler = new SyncMessageHandler(keyboard);
|
||||
|
||||
server = new WebSocketServerService(handler);
|
||||
@@ -54,10 +57,60 @@ public sealed class TrayApplicationContext : ApplicationContext
|
||||
{
|
||||
var menu = new ContextMenuStrip();
|
||||
menu.Items.Add("Copy connection address", null, (_, _) => CopyConnectionAddress());
|
||||
menu.Items.Add(BuildInputModeMenu());
|
||||
menu.Items.Add("Exit", null, (_, _) => ExitThread());
|
||||
return menu;
|
||||
}
|
||||
|
||||
private ToolStripMenuItem BuildInputModeMenu()
|
||||
{
|
||||
var inputModeMenu = new ToolStripMenuItem("Input mode");
|
||||
clipboardPasteMenuItem = new ToolStripMenuItem("Clipboard paste")
|
||||
{
|
||||
CheckOnClick = false
|
||||
};
|
||||
sendInputTypingMenuItem = new ToolStripMenuItem("SendInput typing")
|
||||
{
|
||||
CheckOnClick = false
|
||||
};
|
||||
|
||||
clipboardPasteMenuItem.Click += (_, _) => SetInputMode(TextInjectionMode.ClipboardPaste);
|
||||
sendInputTypingMenuItem.Click += (_, _) => SetInputMode(TextInjectionMode.SendInputTyping);
|
||||
inputModeMenu.DropDownItems.Add(clipboardPasteMenuItem);
|
||||
inputModeMenu.DropDownItems.Add(sendInputTypingMenuItem);
|
||||
UpdateInputModeMenuChecks();
|
||||
return inputModeMenu;
|
||||
}
|
||||
|
||||
private void SetInputMode(TextInjectionMode mode)
|
||||
{
|
||||
keyboard.Mode = mode;
|
||||
AppLogger.Info($"Text injection mode changed to {mode}.");
|
||||
UpdateInputModeMenuChecks();
|
||||
|
||||
notifyIcon.BalloonTipTitle = "WirelessTextSyncer";
|
||||
notifyIcon.BalloonTipText = $"Input mode: {GetInputModeLabel(mode)}";
|
||||
notifyIcon.ShowBalloonTip(1500);
|
||||
}
|
||||
|
||||
private void UpdateInputModeMenuChecks()
|
||||
{
|
||||
if (clipboardPasteMenuItem is not null)
|
||||
{
|
||||
clipboardPasteMenuItem.Checked = keyboard.Mode == TextInjectionMode.ClipboardPaste;
|
||||
}
|
||||
|
||||
if (sendInputTypingMenuItem is not null)
|
||||
{
|
||||
sendInputTypingMenuItem.Checked = keyboard.Mode == TextInjectionMode.SendInputTyping;
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetInputModeLabel(TextInjectionMode mode)
|
||||
{
|
||||
return mode == TextInjectionMode.ClipboardPaste ? "Clipboard paste" : "SendInput typing";
|
||||
}
|
||||
|
||||
private void OnTrayIconClicked(object? sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
|
||||
Reference in New Issue
Block a user