-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
189 lines (151 loc) · 5.58 KB
/
Form1.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using System.Data.SQLite;
using static System.Net.Mime.MediaTypeNames;
namespace popup
{
public partial class Form1 : Form
{
private IKeyboardMouseEvents _globalHook;
private List<string> clipboardHistory = new List<string>();
private Timer clipboardTimer;
private string connectionString = "Data Source=clipboardData.db;Version=3;";
public Form1()
{
this.TopMost = true;
InitializeComponent();
CreateTable();
ShowData();
new ClipHistoryCustomizer(ClipHistory, this);
// Start the global hook
_globalHook = Hook.GlobalEvents();
_globalHook.KeyDown += OnGlobalKeyDown;
// Fetch data form keyboard
clipboardTimer = new Timer();
clipboardTimer.Interval = 500; // Check every 0.5 sec or 500ms
clipboardTimer.Tick += ClipboardTimer_Tick;
clipboardTimer.Start();
}
//-----------database/////////.................
// CreateTable: Creates the table if it doesn't exist
private void CreateTable()
{
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
string sql = "CREATE TABLE IF NOT EXISTS ClipDataTable (clipData TEXT)";
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
// InsertData: Inserts a record into the Users table
private async Task InsertDataAsync(string clipData)
{
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
await conn.OpenAsync();
string sql = "INSERT INTO ClipDataTable (clipData) VALUES (@clipData)";
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
cmd.Parameters.AddWithValue("@clipData", clipData);
await cmd.ExecuteNonQueryAsync();
}
}
// ShowData: Retrieves all records and displays them in MessageBoxes
private void ShowData()
{
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
string sql = "SELECT clipData FROM ClipDataTable ORDER BY ROWID DESC LIMIT 1";
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
var result = cmd.ExecuteScalar();
if (result != null)
{
string clipData = result.ToString();
if (!ClipHistory.Items.Contains(clipData))
{
ClipHistory.Items.Add(clipData);
clipboardHistory.Add(clipData);
}
}
}
}
//------database end--------------------------
// Event handler for global key down || press win + O
private void OnGlobalKeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.O && e.Modifiers == Keys.LWin)
{
ShowPopUp();
}
}
// Add items to the ClipboardHistory list
private async void ClipboardTimer_Tick(object sender, EventArgs e)
{
string clipboardText = Clipboard.GetText();
if (!string.IsNullOrEmpty(clipboardText) && (clipboardHistory.Count == 0 || !clipboardHistory.Contains(clipboardText)))
{
await InsertDataAsync(clipboardText);
ShowData();
}
}
// Show the popUp on the screen when press : win + O
private void ShowPopUp()
{
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.TopMost = true;
this.Show();
}
// Copy to the clipboard
private async void ClipHistory_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ClipHistory.SelectedIndex;
if(selectedIndex >= 0)
{
Clipboard.SetText(clipboardHistory[selectedIndex]);
// Show the "Copied!" status
Status.Text = "Copied!";
Status.ForeColor = Color.Green;
Status.Visible = true;
// Start the timer to hide the status after 3 seconds
await (Task.Delay(3000));
Status.Visible = false;
}
}
// -----------------------------------------------------------
protected override void OnFormClosed(FormClosedEventArgs e)
{
try
{
if (_globalHook != null)
{
_globalHook.KeyDown -= OnGlobalKeyDown;
_globalHook.Dispose();
}
}
finally
{
base.OnFormClosed(e);
}
}
// -----------------------------------------------------------
private void Status_Click(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void ClipHistory_MouseHover(object sender, EventArgs e)
{
}
}
}