-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipBoardUpdaterTextToText.cs
64 lines (48 loc) · 1.94 KB
/
ClipBoardUpdaterTextToText.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
using QwertyTranslator.Translator;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace QwertyTranslator
{
public class ClipBoardUpdaterTextToText
{
private readonly QwertyTranslatorMediator _translator;
public ClipBoardUpdaterTextToText(QwertyTranslatorMediator translator) => _translator = translator;
public void Update()
{
var clipboardText = GetClipboardText();
clipboardText = _translator.Translate(clipboardText, GetLanguange());
Clipboard.SetText(GetCorrectText(clipboardText));
}
/// <summary>
/// Clipboard.SetText(clipboardText) throw ArgumentNullExecption,
/// if clipboardText == string.Empty
/// </summary>
/// <param name="text">to contains to Clipboard</param>
/// <returns></returns>
private string GetCorrectText(string clipboardText)
{
return string.IsNullOrEmpty(clipboardText)
? " "
: clipboardText;
}
private string GetClipboardText()
{
var clipboardText = string.Empty;
if (Clipboard.ContainsText(TextDataFormat.UnicodeText))
{
clipboardText = Clipboard.GetText(TextDataFormat.UnicodeText);
}
return clipboardText;
}
private static Language GetLanguange() => (Language)GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero));
#region DllImport
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId([In] IntPtr hWnd, [Out, Optional] IntPtr lpdwProcessId);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern ushort GetKeyboardLayout([In] int idThread);
#endregion
}
}