Compare commits
8 Commits
d4b7a23e38
...
4f6e817a76
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f6e817a76 | ||
|
|
1b335d720d | ||
|
|
046a27ecf9 | ||
|
|
0c2cbbfccd | ||
|
|
ddd15f1067 | ||
|
|
6b3e8e02f2 | ||
|
|
1b4267a6ff | ||
|
|
a6602206c5 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -5,6 +5,6 @@ obj/
|
||||
*.user
|
||||
*.suo
|
||||
*.log
|
||||
*.lscache
|
||||
.env
|
||||
.env.*
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using WirelessTextSyncer.Windows.Models;
|
||||
using WirelessTextSyncer.Windows.Services;
|
||||
|
||||
namespace WirelessTextSyncer.Windows.Tests;
|
||||
|
||||
@@ -28,4 +29,58 @@ public sealed class SyncMessageTests
|
||||
Assert.IsNotNull(message);
|
||||
Assert.AreEqual(SyncAction.Backspace, message.Action);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeAndroidReplaceAllMessage()
|
||||
{
|
||||
var message = JsonSerializer.Deserialize<SyncMessage>(
|
||||
"""{"action":"replaceAll","text":"final text"}""",
|
||||
new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
||||
|
||||
Assert.IsNotNull(message);
|
||||
Assert.AreEqual(SyncAction.ReplaceAll, message.Action);
|
||||
Assert.AreEqual("final text", message.Text);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ReplaceAllAppendsSubmittedText()
|
||||
{
|
||||
var keyboard = new RecordingKeyboardInjectionService();
|
||||
var handler = new SyncMessageHandler(keyboard);
|
||||
|
||||
handler.Handle(new SyncMessage
|
||||
{
|
||||
Action = SyncAction.ReplaceAll,
|
||||
Text = "final text"
|
||||
});
|
||||
|
||||
CollectionAssert.AreEqual(
|
||||
new[] { "insert:final text" },
|
||||
keyboard.Calls);
|
||||
}
|
||||
|
||||
private sealed class RecordingKeyboardInjectionService : IKeyboardInjectionService
|
||||
{
|
||||
public List<string> Calls { get; } = [];
|
||||
|
||||
public void InsertText(string text)
|
||||
{
|
||||
Calls.Add($"insert:{text}");
|
||||
}
|
||||
|
||||
public void ReplaceFocusedText(string text)
|
||||
{
|
||||
Calls.Add($"replace:{text}");
|
||||
}
|
||||
|
||||
public void Backspace()
|
||||
{
|
||||
Calls.Add("backspace");
|
||||
}
|
||||
|
||||
public void Enter()
|
||||
{
|
||||
Calls.Add("enter");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Text.Json.Serialization;
|
||||
public enum SyncAction
|
||||
{
|
||||
InsertText,
|
||||
ReplaceAll,
|
||||
Backspace,
|
||||
Enter,
|
||||
Ping
|
||||
|
||||
@@ -15,6 +15,7 @@ public sealed class SyncActionJsonConverter : JsonConverter<SyncAction>
|
||||
return value switch
|
||||
{
|
||||
"insertText" => SyncAction.InsertText,
|
||||
"replaceAll" => SyncAction.ReplaceAll,
|
||||
"backspace" => SyncAction.Backspace,
|
||||
"enter" => SyncAction.Enter,
|
||||
"ping" => SyncAction.Ping,
|
||||
@@ -30,6 +31,7 @@ public sealed class SyncActionJsonConverter : JsonConverter<SyncAction>
|
||||
var action = value switch
|
||||
{
|
||||
SyncAction.InsertText => "insertText",
|
||||
SyncAction.ReplaceAll => "replaceAll",
|
||||
SyncAction.Backspace => "backspace",
|
||||
SyncAction.Enter => "enter",
|
||||
SyncAction.Ping => "ping",
|
||||
|
||||
@@ -4,6 +4,8 @@ public interface IKeyboardInjectionService
|
||||
{
|
||||
void InsertText(string text);
|
||||
|
||||
void ReplaceFocusedText(string text);
|
||||
|
||||
void Backspace();
|
||||
|
||||
void Enter();
|
||||
|
||||
@@ -2,13 +2,27 @@ namespace WirelessTextSyncer.Windows.Services;
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
public enum TextInjectionMode
|
||||
{
|
||||
ClipboardPaste,
|
||||
SendInputTyping
|
||||
}
|
||||
|
||||
public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
{
|
||||
private const uint InputKeyboard = 1;
|
||||
private const uint KeyEventFKeyUp = 0x0002;
|
||||
private const uint KeyEventFUnicode = 0x0004;
|
||||
private const uint KlfActivate = 0x00000001;
|
||||
private const uint WmInputLangChangeRequest = 0x0050;
|
||||
private const ushort VirtualKeyA = 0x41;
|
||||
private const ushort VirtualKeyBack = 0x08;
|
||||
private const ushort VirtualKeyControl = 0x11;
|
||||
private const ushort VirtualKeyReturn = 0x0D;
|
||||
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)
|
||||
{
|
||||
@@ -17,13 +31,99 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
return;
|
||||
}
|
||||
|
||||
AppLogger.Info($"Injecting text length: {text.Length}");
|
||||
AppLogger.Info($"Inserting text length: {text.Length}. Mode: {Mode}.");
|
||||
if (Mode == TextInjectionMode.SendInputTyping)
|
||||
{
|
||||
TypeTextWithSendInput(text);
|
||||
return;
|
||||
}
|
||||
|
||||
PasteTextViaClipboard(text);
|
||||
}
|
||||
|
||||
private static void TypeTextWithSendInput(string text)
|
||||
{
|
||||
AppLogger.Info($"Falling back to SendInput text length: {text.Length}");
|
||||
using var keyboardLayoutScope = KeyboardLayoutScope.SwitchForegroundWindowToEnglish();
|
||||
foreach (var character in text)
|
||||
{
|
||||
AppLogger.Info($"Injecting char U+{(int)character:X4}: {character}");
|
||||
SendUnicode(character);
|
||||
Thread.Sleep(2);
|
||||
}
|
||||
}
|
||||
|
||||
private static void PasteTextViaClipboard(string text)
|
||||
{
|
||||
Exception? failure = null;
|
||||
var thread = new Thread(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
PasteTextOnStaThread(text);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
failure = ex;
|
||||
}
|
||||
});
|
||||
|
||||
thread.SetApartmentState(ApartmentState.STA);
|
||||
thread.Start();
|
||||
thread.Join();
|
||||
|
||||
if (failure is not null)
|
||||
{
|
||||
AppLogger.Error("Clipboard paste failed.", failure);
|
||||
TypeTextWithSendInput(text);
|
||||
}
|
||||
}
|
||||
|
||||
private static void PasteTextOnStaThread(string text)
|
||||
{
|
||||
var hadText = Clipboard.ContainsText(TextDataFormat.UnicodeText);
|
||||
var previousText = hadText ? Clipboard.GetText(TextDataFormat.UnicodeText) : null;
|
||||
|
||||
try
|
||||
{
|
||||
Clipboard.SetText(text, TextDataFormat.UnicodeText);
|
||||
Thread.Sleep(30);
|
||||
SendModifiedKey(VirtualKeyControl, VirtualKeyV);
|
||||
Thread.Sleep(150);
|
||||
}
|
||||
finally
|
||||
{
|
||||
RestoreClipboardText(hadText, previousText);
|
||||
}
|
||||
}
|
||||
|
||||
private static void RestoreClipboardText(bool hadText, string? previousText)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (hadText && previousText is not null)
|
||||
{
|
||||
Clipboard.SetText(previousText, TextDataFormat.UnicodeText);
|
||||
return;
|
||||
}
|
||||
|
||||
Clipboard.Clear();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppLogger.Error("Failed to restore clipboard text.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void ReplaceFocusedText(string text)
|
||||
{
|
||||
AppLogger.Info($"Replacing focused text length: {text.Length}");
|
||||
SendModifiedKey(VirtualKeyControl, VirtualKeyA);
|
||||
Backspace();
|
||||
InsertText(text);
|
||||
AppLogger.Info("Finished replacing focused text.");
|
||||
}
|
||||
|
||||
public void Backspace()
|
||||
{
|
||||
AppLogger.Info("Injecting backspace.");
|
||||
@@ -52,6 +152,16 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
]);
|
||||
}
|
||||
|
||||
private static void SendModifiedKey(ushort modifierKey, ushort key)
|
||||
{
|
||||
Send([
|
||||
CreateVirtualKeyInput(modifierKey, 0),
|
||||
CreateVirtualKeyInput(key, 0),
|
||||
CreateVirtualKeyInput(key, KeyEventFKeyUp),
|
||||
CreateVirtualKeyInput(modifierKey, KeyEventFKeyUp)
|
||||
]);
|
||||
}
|
||||
|
||||
private static void Send(INPUT[] inputs)
|
||||
{
|
||||
var sent = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<INPUT>());
|
||||
@@ -98,6 +208,66 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetForegroundWindow();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern uint GetWindowThreadProcessId(IntPtr windowHandle, out uint processId);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetKeyboardLayout(uint threadId);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern IntPtr LoadKeyboardLayout(string keyboardLayoutId, uint flags);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool PostMessage(IntPtr windowHandle, uint message, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
private sealed class KeyboardLayoutScope : IDisposable
|
||||
{
|
||||
private readonly IntPtr foregroundWindow;
|
||||
private readonly IntPtr originalKeyboardLayout;
|
||||
|
||||
private KeyboardLayoutScope(IntPtr foregroundWindow, IntPtr originalKeyboardLayout)
|
||||
{
|
||||
this.foregroundWindow = foregroundWindow;
|
||||
this.originalKeyboardLayout = originalKeyboardLayout;
|
||||
}
|
||||
|
||||
public static KeyboardLayoutScope SwitchForegroundWindowToEnglish()
|
||||
{
|
||||
var foregroundWindow = GetForegroundWindow();
|
||||
if (foregroundWindow == IntPtr.Zero)
|
||||
{
|
||||
AppLogger.Error("Cannot switch keyboard layout because no foreground window was found.");
|
||||
return new KeyboardLayoutScope(IntPtr.Zero, IntPtr.Zero);
|
||||
}
|
||||
|
||||
var threadId = GetWindowThreadProcessId(foregroundWindow, out _);
|
||||
var originalKeyboardLayout = GetKeyboardLayout(threadId);
|
||||
AppLogger.Info($"Switching foreground keyboard layout from 0x{originalKeyboardLayout.ToInt64():X} to 0x{EnglishKeyboardLayout.ToInt64():X}.");
|
||||
|
||||
if (EnglishKeyboardLayout != IntPtr.Zero)
|
||||
{
|
||||
PostMessage(foregroundWindow, WmInputLangChangeRequest, IntPtr.Zero, EnglishKeyboardLayout);
|
||||
Thread.Sleep(50);
|
||||
}
|
||||
|
||||
return new KeyboardLayoutScope(foregroundWindow, originalKeyboardLayout);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (foregroundWindow == IntPtr.Zero || originalKeyboardLayout == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AppLogger.Info($"Restoring foreground keyboard layout to 0x{originalKeyboardLayout.ToInt64():X}.");
|
||||
PostMessage(foregroundWindow, WmInputLangChangeRequest, IntPtr.Zero, originalKeyboardLayout);
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct INPUT
|
||||
{
|
||||
@@ -108,8 +278,25 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
private struct InputUnion
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public MOUSEINPUT mi;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public KEYBDINPUT ki;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public HARDWAREINPUT hi;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct MOUSEINPUT
|
||||
{
|
||||
public int dx;
|
||||
public int dy;
|
||||
public uint mouseData;
|
||||
public uint dwFlags;
|
||||
public uint time;
|
||||
public UIntPtr dwExtraInfo;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
@@ -121,4 +308,12 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
public uint time;
|
||||
public UIntPtr dwExtraInfo;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct HARDWAREINPUT
|
||||
{
|
||||
public uint uMsg;
|
||||
public ushort wParamL;
|
||||
public ushort wParamH;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace WirelessTextSyncer.Windows.Services;
|
||||
public sealed class SyncMessageHandler
|
||||
{
|
||||
private readonly IKeyboardInjectionService keyboard;
|
||||
private string remoteText = string.Empty;
|
||||
|
||||
public SyncMessageHandler(IKeyboardInjectionService keyboard)
|
||||
{
|
||||
@@ -16,13 +17,24 @@ public sealed class SyncMessageHandler
|
||||
switch (message.Action)
|
||||
{
|
||||
case SyncAction.InsertText:
|
||||
keyboard.InsertText(message.Text ?? string.Empty);
|
||||
var insertedText = message.Text ?? string.Empty;
|
||||
keyboard.InsertText(insertedText);
|
||||
remoteText += insertedText;
|
||||
break;
|
||||
case SyncAction.ReplaceAll:
|
||||
AppendSubmittedText(message.Text ?? string.Empty);
|
||||
break;
|
||||
case SyncAction.Backspace:
|
||||
keyboard.Backspace();
|
||||
if (remoteText.Length > 0)
|
||||
{
|
||||
remoteText = remoteText[..^1];
|
||||
}
|
||||
|
||||
break;
|
||||
case SyncAction.Enter:
|
||||
keyboard.Enter();
|
||||
remoteText += Environment.NewLine;
|
||||
break;
|
||||
case SyncAction.Ping:
|
||||
break;
|
||||
@@ -30,4 +42,16 @@ public sealed class SyncMessageHandler
|
||||
throw new InvalidOperationException($"Unsupported sync action: {message.Action}");
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendSubmittedText(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AppLogger.Info($"Appending submitted text length: {text.Length}");
|
||||
keyboard.InsertText(text);
|
||||
remoteText += 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