webcrab/webmouse/main.vb

176 lines
6.6 KiB
VB.net
Raw Normal View History

2020-06-15 04:26:36 -07:00
Imports System.Threading
Imports System.Net
Public Class main
Dim weblistener As HttpListener
Dim lastpos As New Point(0, 0)
Dim startpos As New Point(0, 0)
Dim leftclicking As Boolean = False
2020-06-15 04:26:36 -07:00
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles StartButton.Click
If Not weblistener Is Nothing Then
weblistener.Abort()
weblistener = Nothing
log("Stopped")
StartButton.BackColor = Color.Lime
StartButton.Text = "Start"
Else
weblistener = New HttpListener
weblistener.Prefixes.Add("http://*:8080/")
2020-06-15 04:26:36 -07:00
weblistener.Start()
Dim listenthread As New Thread(AddressOf listenforconnection)
listenthread.Start()
log("Started")
StartButton.BackColor = Color.Red
StartButton.Text = "Stop"
End If
End Sub
Public Sub listenforconnection()
Dim context As HttpListenerContext = Nothing
2020-06-15 04:26:36 -07:00
Dim resp As HttpListenerResponse
Dim respstr As String = String.Empty
2020-06-15 04:26:36 -07:00
Dim lastpacket As Long = -1
Dim lastpack As Long
Dim leftclicking As Boolean = False
2020-06-15 04:26:36 -07:00
While weblistener.IsListening
Try
context = weblistener.GetContext()
Catch ex As Exception
If weblistener Is Nothing OrElse weblistener.IsListening = False Then
Exit Sub
End If
End Try
resp = context.Response
Dim path As String = context.Request.Url.AbsolutePath
log(context.Request.RemoteEndPoint.ToString & " " & context.Request.RawUrl)
2020-06-15 04:26:36 -07:00
' check for valid command packets
If path = "/" Then
respstr = FileIO.FileSystem.ReadAllText("page.html")
2020-06-15 04:26:36 -07:00
lastpacket = 0
Else
Dim pack As Long = context.Request.QueryString("seq")
If pack > lastpack Then
respstr = handleinput(context.Request)
lastpack = pack
2020-06-15 04:26:36 -07:00
End If
End If
If String.IsNullOrEmpty(respstr) Then
respstr = "bad"
End If
Dim respbytes As Byte() = System.Text.Encoding.UTF8.GetBytes(respstr)
resp.ContentLength64 = respbytes.Length
resp.OutputStream.Write(respbytes, 0, respbytes.Length)
resp.OutputStream.Close()
resp.Close()
'perftimer.Reset()
2020-06-15 04:26:36 -07:00
End While
End Sub
Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not weblistener Is Nothing Then
weblistener.Abort()
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
Button1_Click(sender, e)
End Sub
Private Sub log(str As String)
Dim oldtext As String = LogText.Text
Dim oldsplit() As String = oldtext.Split(vbNewLine)
Dim newtext As String
newtext = Now.ToString("[HH:mm:ss.fff] ") & str & vbNewLine
For i = 0 To Math.Min(oldsplit.Length - 1, 5)
newtext &= oldsplit(i) & vbNewLine
Next
LogText.Text = newtext
2020-06-15 04:26:36 -07:00
End Sub
Private Function handleinput(req As HttpListenerRequest) As String
Dim split() As String = req.Url.AbsolutePath.Split({"/"}, StringSplitOptions.RemoveEmptyEntries)
If split.Length < 1 Then
Return "bad"
End If
Dim opt As String = split(0)
Select Case opt
Case "start" 'start of trackpad touch
lastpos.X = req.QueryString("x")
lastpos.Y = req.QueryString("y")
startpos.X = req.QueryString("x")
startpos.Y = req.QueryString("y")
Case "move" 'trackpad touch move
mouse_event(MouseEventFlags.MOUSEEVENTF_MOVE, req.QueryString("x") - lastpos.X, req.QueryString("y") - lastpos.Y, 0, 0)
lastpos.X = req.QueryString("x")
lastpos.Y = req.QueryString("y")
Case "end"
Dim tol As Integer = TapInput.Value
If (Math.Abs(req.QueryString("x") - startpos.X) < tol) And (Math.Abs(req.QueryString("y") - startpos.Y) < tol) Then
mouse_event(MouseEventFlags.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
System.Threading.Thread.Sleep(10)
mouse_event(MouseEventFlags.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End If
Case "left"
If leftclicking Then
mouse_event(MouseEventFlags.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
leftclicking = False
Else
mouse_event(MouseEventFlags.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
leftclicking = True
End If
Case "middle"
mouse_event(MouseEventFlags.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
System.Threading.Thread.Sleep(10)
mouse_event(MouseEventFlags.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
Case "right"
mouse_event(MouseEventFlags.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
System.Threading.Thread.Sleep(10)
mouse_event(MouseEventFlags.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
Case "wheel"
mouse_event(MouseEventFlags.MOUSEEVENTF_WHEEL, 0, 0, WHEEL_DELTA * req.QueryString("d"), 0)
Case "volume"
If req.QueryString("d") = -1 Then
keybd_press(VK.VOLUME_DOWN)
ElseIf req.QueryString("d") = 0 Then
keybd_press(VK.VOLUME_MUTE)
ElseIf req.QueryString("d") = 1 Then
keybd_press(VK.VOLUME_UP)
End If
Case "media"
Select Case req.QueryString("act")
Case "stop"
keybd_press(VK.MEDIA_STOP)
Case "prev"
keybd_press(VK.MEDIA_PREV_TRACK)
Case "play"
keybd_press(VK.MEDIA_PLAY_PAUSE)
Case "next"
keybd_press(VK.MEDIA_NEXT_TRACK)
End Select
Case "text"
Case Else
Return "bad"
End Select
Return "ok"
End Function
2020-06-15 04:26:36 -07:00
End Class