-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (82 loc) · 3.28 KB
/
Program.cs
File metadata and controls
117 lines (82 loc) · 3.28 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;
using TextCopy;
using translatorc_;
using WindowsInput;
using WindowsInput.Native;
class Program
{
private const string APIKEY = "AQVN3g6-anTBkvd4qSzfc02Dbzrr_CxS48OivNsT";
private static bool _isChanged = false;
[STAThread]
static async Task Main(string[] args)
{
var simulator = new InputSimulator();
while (true)
{
var lang = ChangeLanguage(_isChanged);
if (simulator.InputDeviceState.IsHardwareKeyDown(VirtualKeyCode.TAB) &&
simulator.InputDeviceState.IsHardwareKeyDown(VirtualKeyCode.VK_C))
{
_isChanged = !_isChanged;
lang = ChangeLanguage(_isChanged);
Console.WriteLine($"Язык сменен,Текущий язык:{lang}");
await Task.Delay(400);
}
if (simulator.InputDeviceState.IsHardwareKeyDown(VirtualKeyCode.CONTROL) &&
simulator.InputDeviceState.IsHardwareKeyDown(VirtualKeyCode.VK_T))
{
Console.WriteLine("Переводим...");
simulator.Keyboard
.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
await Task.Delay(400);
var stringForTranslate = ClipboardService.GetText();
Translate(stringForTranslate, lang);
await Task.Delay(3000);
simulator.Keyboard
.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V);
//Console.WriteLine(ClipboardService.GetText());
await Task.Delay(400);
}
}
static async void Translate(string textToTranslate,string targetLanguage)
{
string apiUrl = "https://translate.api.cloud.yandex.net/translate/v2/translate";
using (var client = new HttpClient()) {
client.DefaultRequestHeaders.Add("Authorization", $"Api-Key {APIKEY}");
var requestBody = new
{
targetLanguageCode = targetLanguage,
texts = new[] { textToTranslate }
};
string jsonBody = JsonConvert.SerializeObject(requestBody);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await client.PostAsync(apiUrl, content);
if (response.IsSuccessStatusCode)
{
string responseJson = await response.Content.ReadAsStringAsync();
ClipboardService.SetText( JsonParser.Convert(responseJson));
}
else
{
Console.WriteLine($"Ошибка: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}");
}
}
}
}
private static string ChangeLanguage(bool isChainged)
{
if (isChainged)
{
return "ru";
}
else
{
return "en";
}
}
}