-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverter.cs
71 lines (58 loc) · 1.57 KB
/
Converter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace Iksokodo;
using System;
using System.Runtime.InteropServices;
using WindowsInput;
using WindowsInput.Native;
internal class Converter
{
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(Keys ArrowKeys);
private static readonly Keys[] _possibleKeys = Enum.GetValues<Keys>();
private readonly InputSimulator _inputSimulator = new();
private Key? _cached = null;
public bool Paused { get; set; }
public ManualResetEvent Handle { get; private set; } = new(false);
public void Convert()
{
if (Paused) return;
Key? received = GetKey();
if (received is Key receivedKey)
{
if (receivedKey.IsStart || receivedKey.IsApostrophe)
{
_cached = received;
}
else
{
if (_cached is Key cachedKey && receivedKey.IsX)
{
if (cachedKey.IsApostrophe) // c'x => cx
{
_inputSimulator.Keyboard.KeyPress(VirtualKeyCode.BACK);
_inputSimulator.Keyboard.KeyPress(VirtualKeyCode.BACK);
_inputSimulator.Keyboard.KeyPress(VirtualKeyCode.VK_X);
}
else // cx => ĉ
{
_inputSimulator.Keyboard.KeyPress(VirtualKeyCode.BACK);
_inputSimulator.Keyboard.KeyPress(VirtualKeyCode.BACK);
_inputSimulator.Keyboard.TextEntry((char)cachedKey);
}
}
_cached = null;
}
}
}
private static Key? GetKey()
{
bool shift = GetAsyncKeyState(Keys.ShiftKey) != 0;
foreach (Keys key in _possibleKeys)
{
if (GetAsyncKeyState(key) == -32767) // -32767 == 0b10000000_00000001 => key is down and was pressed since last query
{
return new Key(key, shift);
}
}
return null;
}
}