This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
138 lines (120 loc) · 4.69 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace RSBMixer
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private LowLevelKeyboardHook KeyboardHook = new LowLevelKeyboardHook();
private Keys StartStopKey = Keys.A;
private Keys MainLaserKey = Keys.D1;
private Keys RSBKey = Keys.D2;
private bool Active = false;
public Form1()
{
InitializeComponent();
if (!File.Exists("settings.xml"))
{
using (var fs = File.Create("settings.xml"))
{
var xml = new XElement("Settings",
new XElement("StartStopKey", Keys.A.ToString()),
new XElement("MainLaserKey", Keys.D1.ToString()),
new XElement("RSBKey", Keys.D2.ToString())
);
var info = new UTF8Encoding(true).GetBytes(xml.ToString());
fs.Write(info, 0, info.Length);
}
}
else
{
var xml = XDocument.Load("settings.xml");
StartStopKey = (Keys)Enum.Parse(typeof(Keys), xml.Descendants().SingleOrDefault(x => x.Name.LocalName == "StartStopKey").Value, true);
MainLaserKey = (Keys)Enum.Parse(typeof(Keys), xml.Descendants().SingleOrDefault(x => x.Name.LocalName == "MainLaserKey").Value, true);
RSBKey = (Keys)Enum.Parse(typeof(Keys), xml.Descendants().SingleOrDefault(x => x.Name.LocalName == "RSBKey").Value, true);
button2.Text = StartStopKey.ToString();
button3.Text = MainLaserKey.ToString();
button4.Text = RSBKey.ToString();
}
Task.Factory.StartNew(Tick);
KeyboardHook.OnKeyPressed += KeydownEventHandler;
}
private void KeydownEventHandler(object sender, Keys key)
{
if (key == StartStopKey && !button2.Focused && !button3.Focused && !button4.Focused)
button1.PerformClick();
var settingsChanged = false;
if (button3.Focused && key != RSBKey && key != StartStopKey)
{
button3.Text = key.ToString();
MainLaserKey = key;
settingsChanged = true;
}
else if (button4.Focused && key != MainLaserKey && key != StartStopKey)
{
button4.Text = key.ToString();
RSBKey = key;
settingsChanged = true;
}
else if (button2.Focused && key != RSBKey && key != MainLaserKey)
{
button2.Text = key.ToString();
StartStopKey = key;
settingsChanged = true;
}
if (settingsChanged)
{
var xml = new XElement("Settings",
new XElement("StartStopKey", StartStopKey),
new XElement("MainLaserKey", MainLaserKey),
new XElement("RSBKey", RSBKey)
);
xml.Save("settings.xml");
}
}
private void Button1_Click(object sender, EventArgs e)
{
PressKey(MainLaserKey);
Active = Active ? false : true;
button1.Text = Active ? "Stop" : "Start";
label4.Text = Active ? "Active" : "Passive";
button2.Enabled = Active ? false : true;
button3.Enabled = Active ? false : true;
button4.Enabled = Active ? false : true;
label1.Enabled = Active ? false : true;
label2.Enabled = Active ? false : true;
label3.Enabled = Active ? false : true;
}
private const int KEYEVENTF_EXTENDEDKEY = 1;
private const int KEYEVENTF_KEYUP = 2;
private void PressKey(Keys vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
private async void Tick()
{
while (true)
{
if (Active)
{
PressKey(RSBKey);
await Task.Delay(150);
PressKey(MainLaserKey);
await Task.Delay(3000);
}
}
}
}
}