/
ClipboardMonitoring.cs
77 lines (71 loc) · 2.15 KB
/
ClipboardMonitoring.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
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using FTimer = System.Windows.Forms.Timer;
namespace ChiiTrans
{
class ClipboardMonitoring
{
public static bool Enabled
{
get
{
return Global.options.clipboardMonitoring;
}
set
{
Global.options.clipboardMonitoring = value;
UpdateEnabled();
}
}
private static string oldText = null;
private static FTimer timer;
static ClipboardMonitoring()
{
timer = new FTimer();
timer.Enabled = false;
timer.Interval = 50;
timer.Tick += MonitorProc;
}
public static void UpdateEnabled()
{
timer.Enabled = Enabled;
Form1.thisForm.UpdateClipboardMonitoringButton();
}
private static void MonitorProc(object sender, EventArgs e)
{
if (!Enabled)
{
UpdateEnabled();
return;
}
try
{
if (Clipboard.ContainsText())
{
string clip = Clipboard.GetText();
int maxLength = Global.options.maxSourceLength * 2;
if (clip.Length > maxLength)
clip = clip.Substring(0, maxLength);
if (clip != oldText)
{
if (!Global.options.clipboardMonitoringJapanese || Translation.hasJapanese(clip))
{
Translation.Translate(clip, null, true);
}
oldText = clip;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error in ClipboardMonitoring: " + ex.Message);
Enabled = false;
UpdateEnabled();
}
}
}
}