Replace focused text from Android source

This commit is contained in:
Misaka
2026-05-15 22:09:31 +08:00
parent d4b7a23e38
commit a6602206c5
6 changed files with 133 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
using System.Text.Json; using System.Text.Json;
using WirelessTextSyncer.Windows.Models; using WirelessTextSyncer.Windows.Models;
using WirelessTextSyncer.Windows.Services;
namespace WirelessTextSyncer.Windows.Tests; namespace WirelessTextSyncer.Windows.Tests;
@@ -28,4 +29,58 @@ public sealed class SyncMessageTests
Assert.IsNotNull(message); Assert.IsNotNull(message);
Assert.AreEqual(SyncAction.Backspace, message.Action); 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 ReplaceAllUsesFocusedTextReplacement()
{
var keyboard = new RecordingKeyboardInjectionService();
var handler = new SyncMessageHandler(keyboard);
handler.Handle(new SyncMessage
{
Action = SyncAction.ReplaceAll,
Text = "final text"
});
CollectionAssert.AreEqual(
new[] { "replace: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");
}
}
} }

View File

@@ -6,6 +6,7 @@ using System.Text.Json.Serialization;
public enum SyncAction public enum SyncAction
{ {
InsertText, InsertText,
ReplaceAll,
Backspace, Backspace,
Enter, Enter,
Ping Ping

View File

@@ -15,6 +15,7 @@ public sealed class SyncActionJsonConverter : JsonConverter<SyncAction>
return value switch return value switch
{ {
"insertText" => SyncAction.InsertText, "insertText" => SyncAction.InsertText,
"replaceAll" => SyncAction.ReplaceAll,
"backspace" => SyncAction.Backspace, "backspace" => SyncAction.Backspace,
"enter" => SyncAction.Enter, "enter" => SyncAction.Enter,
"ping" => SyncAction.Ping, "ping" => SyncAction.Ping,
@@ -30,6 +31,7 @@ public sealed class SyncActionJsonConverter : JsonConverter<SyncAction>
var action = value switch var action = value switch
{ {
SyncAction.InsertText => "insertText", SyncAction.InsertText => "insertText",
SyncAction.ReplaceAll => "replaceAll",
SyncAction.Backspace => "backspace", SyncAction.Backspace => "backspace",
SyncAction.Enter => "enter", SyncAction.Enter => "enter",
SyncAction.Ping => "ping", SyncAction.Ping => "ping",

View File

@@ -4,6 +4,8 @@ public interface IKeyboardInjectionService
{ {
void InsertText(string text); void InsertText(string text);
void ReplaceFocusedText(string text);
void Backspace(); void Backspace();
void Enter(); void Enter();

View File

@@ -7,7 +7,9 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
private const uint InputKeyboard = 1; private const uint InputKeyboard = 1;
private const uint KeyEventFKeyUp = 0x0002; private const uint KeyEventFKeyUp = 0x0002;
private const uint KeyEventFUnicode = 0x0004; private const uint KeyEventFUnicode = 0x0004;
private const ushort VirtualKeyA = 0x41;
private const ushort VirtualKeyBack = 0x08; private const ushort VirtualKeyBack = 0x08;
private const ushort VirtualKeyControl = 0x11;
private const ushort VirtualKeyReturn = 0x0D; private const ushort VirtualKeyReturn = 0x0D;
public void InsertText(string text) public void InsertText(string text)
@@ -24,6 +26,15 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
} }
} }
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() public void Backspace()
{ {
AppLogger.Info("Injecting backspace."); AppLogger.Info("Injecting backspace.");
@@ -52,6 +63,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) private static void Send(INPUT[] inputs)
{ {
var sent = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<INPUT>()); var sent = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<INPUT>());
@@ -108,8 +129,25 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
[StructLayout(LayoutKind.Explicit)] [StructLayout(LayoutKind.Explicit)]
private struct InputUnion private struct InputUnion
{ {
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)] [FieldOffset(0)]
public KEYBDINPUT ki; 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)] [StructLayout(LayoutKind.Sequential)]
@@ -121,4 +159,12 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
public uint time; public uint time;
public UIntPtr dwExtraInfo; public UIntPtr dwExtraInfo;
} }
[StructLayout(LayoutKind.Sequential)]
private struct HARDWAREINPUT
{
public uint uMsg;
public ushort wParamL;
public ushort wParamH;
}
} }

View File

@@ -5,6 +5,7 @@ namespace WirelessTextSyncer.Windows.Services;
public sealed class SyncMessageHandler public sealed class SyncMessageHandler
{ {
private readonly IKeyboardInjectionService keyboard; private readonly IKeyboardInjectionService keyboard;
private string remoteText = string.Empty;
public SyncMessageHandler(IKeyboardInjectionService keyboard) public SyncMessageHandler(IKeyboardInjectionService keyboard)
{ {
@@ -16,13 +17,24 @@ public sealed class SyncMessageHandler
switch (message.Action) switch (message.Action)
{ {
case SyncAction.InsertText: case SyncAction.InsertText:
keyboard.InsertText(message.Text ?? string.Empty); var insertedText = message.Text ?? string.Empty;
keyboard.InsertText(insertedText);
remoteText += insertedText;
break;
case SyncAction.ReplaceAll:
ReplaceAll(message.Text ?? string.Empty);
break; break;
case SyncAction.Backspace: case SyncAction.Backspace:
keyboard.Backspace(); keyboard.Backspace();
if (remoteText.Length > 0)
{
remoteText = remoteText[..^1];
}
break; break;
case SyncAction.Enter: case SyncAction.Enter:
keyboard.Enter(); keyboard.Enter();
remoteText += Environment.NewLine;
break; break;
case SyncAction.Ping: case SyncAction.Ping:
break; break;
@@ -30,4 +42,18 @@ public sealed class SyncMessageHandler
throw new InvalidOperationException($"Unsupported sync action: {message.Action}"); throw new InvalidOperationException($"Unsupported sync action: {message.Action}");
} }
} }
private void ReplaceAll(string newText)
{
if (newText == remoteText)
{
return;
}
AppLogger.Info(
$"Hard replacing focused text. Old mirrored length: {remoteText.Length}, new length: {newText.Length}");
keyboard.ReplaceFocusedText(newText);
remoteText = newText;
}
} }