Replace focused text from Android source
This commit is contained in:
@@ -7,7 +7,9 @@ public sealed class KeyboardInjectionService : IKeyboardInjectionService
|
||||
private const uint InputKeyboard = 1;
|
||||
private const uint KeyEventFKeyUp = 0x0002;
|
||||
private const uint KeyEventFUnicode = 0x0004;
|
||||
private const ushort VirtualKeyA = 0x41;
|
||||
private const ushort VirtualKeyBack = 0x08;
|
||||
private const ushort VirtualKeyControl = 0x11;
|
||||
private const ushort VirtualKeyReturn = 0x0D;
|
||||
|
||||
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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
var sent = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<INPUT>());
|
||||
@@ -108,8 +129,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 +159,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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user