-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
186 lines (165 loc) · 5.16 KB
/
Main.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
// This code is distributed under MIT license.
// Copyright (c) 2015 George Mamaladze
// See license.txt or https://mit-license.org/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
namespace Demo
{
public partial class Main : Form
{
private IKeyboardMouseEvents m_Events;
private bool b6key = true;
public Main()
{
InitializeComponent();
radioGlobal.Checked = true;
SubscribeGlobal();
FormClosing += Main_Closing;
}
private void Main_Closing(object sender, CancelEventArgs e)
{
Unsubscribe();
}
private void SubscribeApplication()
{
Unsubscribe();
Subscribe(Hook.AppEvents());
}
private void SubscribeGlobal()
{
Unsubscribe();
Subscribe(Hook.GlobalEvents());
}
private void Subscribe(IKeyboardMouseEvents events)
{
m_Events = events;
m_Events.KeyDown += OnKeyDown;
m_Events.KeyUp += OnKeyUp;
m_Events.KeyPress += HookManager_KeyPress;
}
private void Unsubscribe()
{
if (m_Events == null) return;
m_Events.KeyDown -= OnKeyDown;
m_Events.KeyUp -= OnKeyUp;
m_Events.KeyPress -= HookManager_KeyPress;
m_Events.Dispose();
m_Events = null;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
Log(string.Format("KeyDown \t\t {0}\n", e.KeyCode));
// q代表键盘1方向,e代表键盘3方向
if (b6key)
{
if (e.KeyCode == Keys.Q)
{
SendKeys.SendWait("(as)");
}
else if (e.KeyCode == Keys.E)
{
SendKeys.SendWait("(ds)");
}
}
}
private void OnKeyUp(object sender, KeyEventArgs e)
{
Log(string.Format("KeyUp \t\t\t {0}\n", e.KeyCode));
// 处理热键开关,当发现弹起→按键的时候,切换生效状态。
if (e.KeyCode == Keys.Right)
{
if (b6key)
{
Log("关闭6键");
radioGlobal.Checked = false;
radioNone.Checked = true;
b6key = false;
}
else
{
Log("开启6键");
radioGlobal.Checked = true;
radioNone.Checked = false;
b6key = true;
}
}
// pause热键改为显示隐藏程序
if (e.KeyCode == Keys.Pause)
{
if (this.Visible == true)
{
this.Visible = false;
}
else
{
this.Visible = true;
}
}
}
private void HookManager_KeyPress(object sender, KeyPressEventArgs e)
{
Log(string.Format("KeyPress \t\t\t {0}\n", e.KeyChar));
// 长按连击,只处理UI按键,并且开启了长按连击,因为模拟按键也有键盘事件,会有死循环。后面想办法处理
/*
*
if(bAutoFire)
{
Thread.Sleep(10);
if (e.KeyChar == 'u')
{
SendKeys.SendWait("{u}");
}else if (e.KeyChar == 'i')
{
SendKeys.SendWait("{i}");
}
}
*/
}
private void Log(string text)
{
if (IsDisposed) return;
textBoxLog.AppendText(text);
textBoxLog.AppendText(Environment.NewLine);
textBoxLog.ScrollToCaret();
}
private void radioGlobal_CheckedChanged(object sender, EventArgs e)
{
//if (((RadioButton)sender).Checked) SubscribeGlobal();
if (((RadioButton)sender).Checked) b6key = true;
}
private void radioNone_CheckedChanged(object sender, EventArgs e)
{
// if (((RadioButton)sender).Checked) Unsubscribe();
if (((RadioButton)sender).Checked) b6key = false;
}
private void clearLog_Click(object sender, EventArgs e)
{
textBoxLog.Clear();
}
/// <summary>
/// 加双击托盘图标的处理程序。
/// 双击的时候,切换显示隐藏。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.Visible == true)
{
this.Visible = false;
}
else
{
this.Visible = true;
}
}
private void Main_Load(object sender, EventArgs e)
{
SubscribeGlobal();
}
}
}