Files
iDunnoDev a2ad5443d5 Added some faux mouse polling since the movements were creating way too many events (this wasn't an issue in the past with low DPI mice)
Added a check to make sure the user cannot use the same Key twice for actions
Added a count down timer update for when a user is setting the start position so they know how long they need to wait
Added an offset to the final click in the action so the mouse re-clicks the app window into focus
Removed some unused code and Cleaned up some methods
2022-09-29 00:06:54 +01:00

87 lines
5.0 KiB
VB.net

Public Class FrmMain
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer) 'Grab a reference to the mouse even from the user32 lib in windows so that we can get the position
Public mouseActionList As New Dictionary(Of String, List(Of Tuple(Of Integer, Integer)))() 'A public dictionary to hold the mouse action/keypress details
Public mouseActionStartPosition As Tuple(Of Integer, Integer) 'The start x and y location on the screen for the mouse action to move from
Private _mouseStartPositionSet As Boolean 'Bool to check if the user has set the start mouse position yet or not
'Ints to store a couple of tick values we can use to compare
Private _posSetStartTick As Integer
Private _posSetCurrentTick As Integer
Private Sub CmdExit_Click(sender As Object, e As EventArgs) Handles CmdExit.Click 'Quit the app
Me.Close()
End Sub
Private Sub CmdRecAct_Click(sender As Object, e As EventArgs) Handles CmdRecAct.Click
Dim capFrm As New FrmCapture
capFrm.Location = New Point(Me.Location.X - capFrm.Width, Me.Location.Y)
capFrm.ShowDialog()
End Sub
Private Sub CmdRemSel_Click(sender As Object, e As EventArgs) Handles CmdRemSel.Click 'Method for removing mouse actions from the action list1
Dim sel_text As String() = ChkActions.SelectedItem.ToString.Split(" | ")
ChkActions.Items.RemoveAt(ChkActions.SelectedIndex)
Dim char_key As Char = sel_text.Last()
mouseActionList.Remove(char_key)
End Sub
Private Sub FrmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown 'Send the mouse action to the mouse lib to execute the command
If _mouseStartPositionSet = True Then 'Check if the start position has been set so we know where to start the action
If mouseActionList.ContainsKey(Chr(e.KeyValue)) Then 'Check if the keypress is actually in the actions list
LblLastInput.Text = Chr(e.KeyValue)
Cursor.Position = New Point(mouseActionStartPosition.Item1, mouseActionStartPosition.Item2) 'set the mouse position to the start
mouse_event(&H2&, 0, 0, 0, 0) 'Send the left click command
'loop through each point in the action array and set the mouse position
For Each lst_item In mouseActionList.Item(Chr(e.KeyValue))
Cursor.Position = New Point(mouseActionStartPosition.Item1 + lst_item.Item1, mouseActionStartPosition.Item2 + lst_item.Item2)
Threading.Thread.Sleep(1)
Next
mouse_event(&H4&, 0, 0, 0, 0) 'Release the left click
'Set the mouse location back to the app and click to try get focus, click and release the button incase it stuck for some reason, force focus and bring to the front
Cursor.Position = New Point(Me.Location.X + 10, Me.Location.Y + 10) 'Needs offset!
mouse_event(&H2&, 0, 0, 0, 0)
mouse_event(&H4&, 0, 0, 0, 0)
Me.BringToFront()
Me.Focus()
End If
End If
End Sub
Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.TopMost = True
Me.KeyPreview = True
_mouseStartPositionSet = False
End Sub
Private Sub CmdPos_Click(sender As Object, e As EventArgs) Handles CmdPos.Click
_posSetStartTick = Environment.TickCount 'Get the enviroment time we can use to get the amount of milliseconds between 2 points in the app
_posSetCurrentTick = Environment.TickCount
PosSel.Start() 'Start the timer to listen for the start moust position method
PosCountDown.Start()
CmdPos.BackColor = Color.Green
End Sub
Private Sub PosSel_Tick(sender As Object, e As EventArgs) Handles PosSel.Tick 'Method that waits for the timer to tick, once it has we get the mouse's current position and sets the start location variable
mouseActionStartPosition = Tuple.Create(Cursor.Position.X, Cursor.Position.Y)
lblMousePos.Text = "Mouse Pos: " & Cursor.Position.X & ":" & Cursor.Position.Y
CmdPos.BackColor = Color.Red
_mouseStartPositionSet = True
PosCountDown.Stop()
PosSel.Stop()
End Sub
Private Sub PosCountDown_Tick(sender As Object, e As EventArgs) Handles PosCountDown.Tick 'Method to update the pos label with a timer to show the user when the position has been taken
If PosSel.Enabled Then 'Check if the pos timer is currently running
_posSetCurrentTick = Environment.TickCount 'Get the current enviroment time to check how many millisecs have passed
Dim currentTimeElapsed As Integer = (PosSel.Interval - (_posSetCurrentTick - _posSetStartTick)) / 1000 'Get the time passed in seconds and count down from the inteval
lblMousePos.Text = "Pos Capture in: " & currentTimeElapsed.ToString()
End If
End Sub
End Class