-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.vb
406 lines (353 loc) · 14.7 KB
/
Form1.vb
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Runtime.InteropServices
Imports System.Net
Public Class Form1
Private RunServer As Boolean = False
Private CTS As CancellationTokenSource
Private ConnectionAlive As Boolean = False
Private SendString As String = ""
Public Const MOD_ALT As Integer = &H1 'Alt key
Public Const MOD_WIN As Integer = &H8
Public Const WM_SetRedraw = &HB
Public GroupSetting As Integer = 0
Public ActiveColor As Color = Color.FromArgb(255, 255, 128, 0)
Public PassiveColor As Color = Color.FromArgb(255, 65, 65, 65)
Public Const WM_HOTKEY As Integer = &H312
Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
Private Sub ChangeGroupSetting(NewSelection As Integer)
If GroupSetting <> NewSelection Then
Dim MyButton As Button = Controls("Group" & GroupSetting)
MyButton.BackColor = PassiveColor
GroupSetting = NewSelection
MyButton = Controls("Group" & GroupSetting)
MyButton.BackColor = ActiveColor
End If
End Sub
Private Sub SendNumPress(NumPressed As Integer)
If SendString = "" And ConnectionAlive = True Then
SendString = GroupSetting & "_" & NumPressed
End If
End Sub
'Private Sub Button1_Click(sender As Object, e As EventArgs)
' If RunServer = False Then
' RunServer = True
' Button1.Text = "Stop"
' Dim RunMyServer As Task = RuntcpServer()
' Else
' RunServer = False
' End If
'End Sub
Declare Function SetDrawing Lib "user32" Alias "SendMessageA" (ByVal hwndLock As IntPtr, ByVal Msg As Integer, ByVal wParam As Boolean, ByVal lParam As IntPtr) As Integer
Private Sub ConsoleText(NewText As String)
'Updates the console text-box and handles the drawing state to avoid flicker (May or may not work as intended)
If Visible = True Then SetDrawing(TextBox1.Handle, WM_SetRedraw, False, 0)
TextBox1.Text = NewText
If Visible = True Then
SetDrawing(TextBox1.Handle, WM_SetRedraw, True, 0)
TextBox1.Refresh()
Application.DoEvents()
End If
End Sub
Private Sub ConsoleAppend(AppendText As String)
'Appends the console text-box and handles the drawing state to avoid flicker (May or may not work as intended)
If Visible = True Then SetDrawing(TextBox1.Handle, WM_SetRedraw, False, 0)
TextBox1.AppendText(AppendText)
TextBox1.SelectionStart = Len(TextBox1.Text)
TextBox1.ScrollToCaret()
If Visible = True Then
SetDrawing(TextBox1.Handle, WM_SetRedraw, True, 0)
TextBox1.Refresh()
Application.DoEvents()
End If
End Sub
Private Sub ConsoleBackspace()
'Removes the last character from the console text-box and handles the drawing state to avoid flicker
If Visible = True Then SetDrawing(TextBox1.Handle, WM_SetRedraw, False, 0)
TextBox1.SelectionStart = Len(TextBox1.Text) - 1
TextBox1.SelectionLength = 1
TextBox1.SelectedText = ""
If Visible = True Then
SetDrawing(TextBox1.Handle, WM_SetRedraw, True, 0)
TextBox1.Refresh()
Application.DoEvents()
End If
End Sub
Private Sub ConsoleClearLine()
'Removes the last line from the console text-box and handles the drawing state to avoid flicker
If Visible = True Then SetDrawing(TextBox1.Handle, WM_SetRedraw, False, 0)
Dim SelectionStart As Long = InStrRev(TextBox1.Text, Chr(10))
If SelectionStart <> 0 Then
Dim SelectionLength As Long = Len(TextBox1.Text)
If SelectionLength <> SelectionStart Then
SelectionLength = SelectionLength - SelectionStart
TextBox1.SelectionStart = SelectionStart
TextBox1.SelectionLength = SelectionLength
TextBox1.SelectedText = ""
Application.DoEvents()
End If
End If
If Visible = True Then
SetDrawing(TextBox1.Handle, WM_SetRedraw, True, 0)
TextBox1.Refresh()
Application.DoEvents()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CTS = New CancellationTokenSource
'kbHook = New KeyboardHook
RegisterHotKey(Me.Handle, 100, MOD_ALT, &H60)
RegisterHotKey(Me.Handle, 101, MOD_ALT, &H61)
RegisterHotKey(Me.Handle, 102, MOD_ALT, &H62)
RegisterHotKey(Me.Handle, 103, MOD_ALT, &H63)
RegisterHotKey(Me.Handle, 104, MOD_ALT, &H64)
RegisterHotKey(Me.Handle, 105, MOD_ALT, &H65)
RegisterHotKey(Me.Handle, 106, MOD_ALT, &H66)
RegisterHotKey(Me.Handle, 107, MOD_ALT, &H67)
RegisterHotKey(Me.Handle, 108, MOD_ALT, &H68)
RegisterHotKey(Me.Handle, 109, MOD_ALT, &H69)
RegisterHotKey(Me.Handle, 200, MOD_WIN, &H60)
RegisterHotKey(Me.Handle, 201, MOD_WIN, &H61)
RegisterHotKey(Me.Handle, 202, MOD_WIN, &H62)
RegisterHotKey(Me.Handle, 203, MOD_WIN, &H63)
RegisterHotKey(Me.Handle, 204, MOD_WIN, &H64)
RegisterHotKey(Me.Handle, 205, MOD_WIN, &H65)
RegisterHotKey(Me.Handle, 206, MOD_WIN, &H66)
RegisterHotKey(Me.Handle, 207, MOD_WIN, &H67)
RegisterHotKey(Me.Handle, 208, MOD_WIN, &H68)
RegisterHotKey(Me.Handle, 209, MOD_WIN, &H69)
'RegisterHotKey(Me.Handle, 300, 0, Keys.T)
End Sub
<DllImport("User32.dll")> Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr,
ByVal id As Integer, ByVal fsModifiers As Integer,
ByVal vk As Integer) As Integer
End Function
<DllImport("User32.dll")> Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr,
ByVal id As Integer) As Integer
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim id As IntPtr = m.WParam
Select Case (id.ToString)
Case "100"
SendNumPress(0)
Case "101"
SendNumPress(1)
Case "102"
SendNumPress(2)
Case "103"
SendNumPress(3)
Case "104"
SendNumPress(4)
Case "105"
SendNumPress(5)
Case "106"
SendNumPress(6)
Case "107"
SendNumPress(7)
Case "108"
SendNumPress(8)
Case "109"
SendNumPress(9)
Case "200"
ChangeGroupSetting(0)
Case "201"
ChangeGroupSetting(1)
Case "202"
ChangeGroupSetting(2)
Case "203"
ChangeGroupSetting(3)
Case "204"
ChangeGroupSetting(4)
Case "205"
ChangeGroupSetting(5)
Case "206"
ChangeGroupSetting(6)
Case "207"
ChangeGroupSetting(7)
Case "208"
ChangeGroupSetting(8)
Case "209"
ChangeGroupSetting(9)
'Case "300"
'TextBox1.Text = TextBox1.Text & vbCrLf & "Rock And Stone" & vbCrLf
'MessageBox.Show("W9")
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
UnregisterHotKey(Me.Handle, 100)
UnregisterHotKey(Me.Handle, 101)
UnregisterHotKey(Me.Handle, 102)
UnregisterHotKey(Me.Handle, 103)
UnregisterHotKey(Me.Handle, 104)
UnregisterHotKey(Me.Handle, 105)
UnregisterHotKey(Me.Handle, 106)
UnregisterHotKey(Me.Handle, 107)
UnregisterHotKey(Me.Handle, 108)
UnregisterHotKey(Me.Handle, 109)
UnregisterHotKey(Me.Handle, 200)
UnregisterHotKey(Me.Handle, 201)
UnregisterHotKey(Me.Handle, 202)
UnregisterHotKey(Me.Handle, 203)
UnregisterHotKey(Me.Handle, 204)
UnregisterHotKey(Me.Handle, 205)
UnregisterHotKey(Me.Handle, 206)
UnregisterHotKey(Me.Handle, 207)
UnregisterHotKey(Me.Handle, 208)
UnregisterHotKey(Me.Handle, 209)
If RunServer = True Then
RunServer = False
e.Cancel = True
End If
'UnregisterHotKey(Me.Handle, 300)
End Sub
Public Async Function RuntcpServer() As Task
Dim PortNumber As Integer = 8888
Dim serverSocket As New TcpListener(Net.IPAddress.Any, PortNumber)
Dim hostname As String = Dns.GetHostName()
Dim iphe As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(hostname)
Dim STRipaddress As String = ""
For Each ipheal As System.Net.IPAddress In iphe.AddressList
If ipheal.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
STRipaddress = ipheal.ToString()
End If
Next
Dim clientSocket As TcpClient
Dim MynetworkStream As NetworkStream
Dim bytesFrom As Byte
Dim dataFromClient As String
Dim SendBytes() As Byte
serverSocket.Start()
ConsoleAppend("-Server Started: " & STRipaddress & ":" & PortNumber & vbCrLf)
Application.DoEvents()
ReconnectClient:
Do Until serverSocket.Pending = True
If RunServer = False Then GoTo StopServer
Await Task.Delay(20)
Loop
clientSocket = Await serverSocket.AcceptTcpClientAsync()
ConsoleAppend("-Connected to: " & clientSocket.Client.RemoteEndPoint.ToString & vbCrLf)
Application.DoEvents()
Try
MynetworkStream = clientSocket.GetStream()
ConnectionAlive = True
Catch ex As Exception
MsgBox("-Connection fail: " & ex.ToString)
GoTo didntwork
End Try
While RunServer And MynetworkStream.CanRead And serverSocket.Pending = False 'And serverSocket.Pending = False And MynetworkStream.CanRead
Try
If MynetworkStream.DataAvailable = True Then
bytesFrom = MynetworkStream.ReadByte
dataFromClient = Chr(bytesFrom)
dataFromClient = Replace(dataFromClient, Chr(13), vbCrLf)
ConsoleAppend(dataFromClient)
Application.DoEvents()
Else
If SendString <> "" Then
SendBytes = Encoding.ASCII.GetBytes(SendString & vbCrLf)
MynetworkStream.Write(SendBytes, 0, SendBytes.Length)
'MsgBox(SendBytes.Length)
ConsoleAppend("-Sent: " & SendString & vbCrLf)
SendString = ""
Application.DoEvents()
End If
Await Task.Delay(20)
End If
Catch ex As Exception
MsgBox("socket fail: " & ex.ToString)
GoTo didntwork
End Try
End While
If RunServer = True Then
ConnectionAlive = False
clientSocket.Close()
ConsoleAppend("-Disconnected" & vbCrLf)
Application.DoEvents()
GoTo ReconnectClient
End If
didntwork:
ConnectionAlive = False
clientSocket.Close()
ConsoleAppend("-Disconnected" & vbCrLf)
Application.DoEvents()
StopServer:
serverSocket.Stop()
ConsoleAppend("-Server Stopped")
Await Task.Delay(100)
Close()
'Button1.Text = "Start"
End Function
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
RunServer = True
Dim RunMyServer As Task = RuntcpServer()
End Sub
Private Sub Group0_Click(sender As Object, e As EventArgs) Handles Group0.Click
ChangeGroupSetting(0)
End Sub
Private Sub Group1_Click(sender As Object, e As EventArgs) Handles Group1.Click
ChangeGroupSetting(1)
End Sub
Private Sub Group2_Click(sender As Object, e As EventArgs) Handles Group2.Click
ChangeGroupSetting(2)
End Sub
Private Sub Group3_Click(sender As Object, e As EventArgs) Handles Group3.Click
ChangeGroupSetting(3)
End Sub
Private Sub Group4_Click(sender As Object, e As EventArgs) Handles Group4.Click
ChangeGroupSetting(4)
End Sub
Private Sub Group5_Click(sender As Object, e As EventArgs) Handles Group5.Click
ChangeGroupSetting(5)
End Sub
Private Sub Group6_Click(sender As Object, e As EventArgs) Handles Group6.Click
ChangeGroupSetting(6)
End Sub
Private Sub Group7_Click(sender As Object, e As EventArgs) Handles Group7.Click
ChangeGroupSetting(7)
End Sub
Private Sub Group8_Click(sender As Object, e As EventArgs) Handles Group8.Click
ChangeGroupSetting(8)
End Sub
Private Sub Group9_Click(sender As Object, e As EventArgs) Handles Group9.Click
ChangeGroupSetting(9)
End Sub
Private Sub numpad0_Click(sender As Object, e As EventArgs) Handles numpad0.Click
SendNumPress(0)
End Sub
Private Sub numpad1_Click(sender As Object, e As EventArgs) Handles numpad1.Click
SendNumPress(1)
End Sub
Private Sub numpad2_Click(sender As Object, e As EventArgs) Handles numpad2.Click
SendNumPress(2)
End Sub
Private Sub numpad3_Click(sender As Object, e As EventArgs) Handles numpad3.Click
SendNumPress(3)
End Sub
Private Sub numpad4_Click(sender As Object, e As EventArgs) Handles numpad4.Click
SendNumPress(4)
End Sub
Private Sub numpad5_Click(sender As Object, e As EventArgs) Handles numpad5.Click
SendNumPress(5)
End Sub
Private Sub numpad6_Click(sender As Object, e As EventArgs) Handles numpad6.Click
SendNumPress(6)
End Sub
Private Sub numpad7_Click(sender As Object, e As EventArgs) Handles numpad7.Click
SendNumPress(7)
End Sub
Private Sub numpad8_Click(sender As Object, e As EventArgs) Handles numpad8.Click
SendNumPress(8)
End Sub
Private Sub numpad9_Click(sender As Object, e As EventArgs) Handles numpad9.Click
SendNumPress(9)
End Sub
End Class