How do you move the mouse outside the form and to move diagonally?
I'm currently doing a project on an EOG mouse (eyes controlling the movement of the mouse) for handicapped people. I need the software to work the moment I plug in the hardware, therefore I need the mouse to be able to work outside the form. I'm also having trouble with the movements of the mouse. So far, I am only able to move it left right up down. I need to program it to move diagonally. The movements are pretty jerky. Here are my codes:
Option Explicit On
Imports System
Imports System.Math
Imports System.Text
Imports System.Drawing
Imports System.IO
Imports System.IO.Ports
Imports System.Windows.Forms
Public Class Form1
Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Integer
Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Dim chCode1() As Char = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}
Dim chCode2() As Char = {ChrW(0), ChrW(1), ChrW(2), ChrW(3), ChrW(4), ChrW(5), ChrW(6), ChrW(7), ChrW(8), ChrW(9), ChrW(10), ChrW(11), ChrW(12), ChrW(13), ChrW(14), ChrW(15), ChrW(16), ChrW(17), ChrW(18), ChrW(19), ChrW(20), ChrW(21), ChrW(22), ChrW(23), ChrW(24), ChrW(25), ChrW(26), ChrW(27), ChrW(28), ChrW(29), ChrW(30), ChrW(31), ChrW(32), ChrW(33), ChrW(34), ChrW(35), ChrW(36), ChrW(37), ChrW(38), ChrW(39), ChrW(40), ChrW(41), ChrW(42), ChrW(43), ChrW(44), ChrW(45), ChrW(46), ChrW(47), ChrW(48), ChrW(49), ChrW(50), ChrW(51), ChrW(52), ChrW(53), ChrW(54), ChrW(55), ChrW(56), ChrW(57), ChrW(58), ChrW(59), ChrW(60), ChrW(61), ChrW(62), ChrW(63), ChrW(64), ChrW(65), ChrW(66), ChrW(67), ChrW(68), ChrW(69), ChrW(70), ChrW(71), ChrW(72), ChrW(73), ChrW(74), ChrW(75), ChrW(76), ChrW(77), ChrW(78), ChrW(79), ChrW(80), ChrW(81), ChrW(82), ChrW(83), ChrW(84), ChrW(85), ChrW(86), ChrW(87), ChrW(88), ChrW(89), ChrW(90), ChrW(91), ChrW(92), ChrW(93), ChrW(94), ChrW(95), ChrW(96), ChrW(97), ChrW(98), ChrW(99), ChrW(100)}
'Data received declaration
Dim ctemp As Char
Dim stemp As String = ""
Dim chCodeCount1 As Integer = 0
Dim chCodeCount2 As Integer = 0
Dim datVal1 As Integer, datVal2 As Integer, datVal3 As Integer
Dim detectMovement As Boolean = True
Dim detectBlink As Boolean = False
Dim sampleCount As Integer = 0
Dim l As Boolean = False
Dim r As Boolean = False
Dim u As Boolean = False
Dim d As Boolean = False
Dim lu As Boolean = False
Dim ru As Boolean = False
Dim ld As Boolean = False
Dim rd As Boolean = False
Dim op As Boolean = False
Dim OpenPort As Object
Dim s As String = ""
Dim objWriter
Dim objReader
Dim dataS As String = ""
Dim sample As Integer = 0
#Region " SerialPortCommunication..."
'********************Serial Port Communication********************************
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
If SerialPort1.IsOpen Then
conversion()
End If
End Sub
Private Sub conversion()
chCodeCount1 = 0
chCodeCount2 = 0
'reads 2 bytes to form a 16-bit signed integer
stemp = ""
Try
While Chr(SerialPort1.ReadChar) <> "s"
stemp = ""
End While
sample += 1
'reading data
ctemp = Chr(SerialPort1.ReadChar)
While ctemp <> chCode1(chCodeCount1)
chCodeCount1 += 1
End While
ctemp = Chr(SerialPort1.ReadChar)
While ctemp <> chCode2(chCodeCount2 + 1)
chCodeCount2 += 1
End While
datVal1 = (chCodeCount1 * 100) + chCodeCount2
chCodeCount1 = 0
chCodeCount2 = 0
ctemp = Chr(SerialPort1.ReadChar)
While ctemp <> chCode1(chCodeCount1)
chCodeCount1 += 1
End While
ctemp = Chr(SerialPort1.ReadChar)
While ctemp <> chCode2(chCodeCount2 + 1)
chCodeCount2 += 1
End While
datVal2 = (chCodeCount1 * 100) + chCodeCount2
'************Channel 1 'To move cursor up and down.******************
If detectMovement = True Then
End If
If datVal1 > 820 Then
If u = True Then
u = False
Else
d = True
l = False
r = False
lu = False
ru = False
ld = False
rd = False
End If
detectBlink = True
'MsgBox("DOWN detected")
End If
If datVal1 < 20 Then
If d = True Then
d = False
Else
u = True
l = False
r = False
lu = False
ru = False
ld = False
rd = False
End If
'MsgBox("UP detected")
End If
'**********Channel 2 to move cursor right and left***********
If datVal2 > 800 Then
If r = True Then
r = False
Else
l = True
u = False
d = False
lu = False
ru = False
ld = False
rd = False
End If
'MsgBox("LEFT detected")
End If
'************Channel 3 'To move cursor diagonally.******************
If datVal3 > 410 Then
End If
If lu = True Then
lu = False
Else
ld = True
l = False
u = False
d = False
r = False
ru = False
rd = False
End If
detectBlink = True
'MsgBox("LEFT Down detected")
If datVal3 > 10 Then
End If
If ld = True Then
ld = False
Else
lu = True
l = False
u = False
d = False
r = False
ru = False
rd = False
End If
detectBlink = True
'MsgBox("LEFT UP detected")
If datVal3 > 410 Then
If ru = True Then
ru = False
Else
rd = True
l = False
u = False
d = False
r = False
lu = False
ld = False
End If
detectBlink = True
'MsgBox("Right Down detected")
If datVal3 > 10 Then
If rd = True Then
rd = False
Else
ru = True
l = False
u = False
d = False
r = False
lu = False
ld = False
End If
detectBlink = True
'MsgBox("Right UP detected")
End If
If u = True Or d = True Or l = True Or r = True Then
detectMovement = Not detectMovement
End If
Else
sampleCount += 1
'******* Left click*********
If detectBlink = True Then
If sampleCount = 52 Then
If d = True Then
If datVal1 < 820 Then
'start of mouse click
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
'end of mouse click
detectBlink = False
'MsgBox("blink detected")
Else
'MsgBox("mouse moving")
mouseMovement()
detectBlink = False
End If
Else
'MsgBox("mouse moving")
mouseMovement()
detectBlink = False
End If
End If
Else
'MsgBox("counting sample - sleep mode")
If sampleCount = 700 Then
detectMovement = Not detectMovement
sampleCount = 0
End If
End If
End If
s = s + Str$(datVal1) + " " + Str$(datVal2) + vbNewLine
If sample = 200 Then
'MsgBox("logging data 200")
End If
If sample = 500 Then
'MsgBox("logging data 500")
End If
If sample = 1000 Then
'MsgBox("logging data 1000")
End If
If sample = 2000 Then
'MsgBox("logging data 2000")
End If
If sample = 3500 Then
'MsgBox("logging data 3500")
End If
If sample = 4500 Then
'MsgBox("logging data 4500")
End If
If sample = 5500 Then
'MsgBox("logging data 5500")
End If
If sample = 6500 Then
'MsgBox("logging data 6500")
End If
If sample = 7500 Then
'MsgBox("logging data 6500")
End If
If sample = 8500 Then
'MsgBox("logging data 6500")
End If
If sample = 9500 Then
'MsgBox("logging data 6500")
End If
If sample = 10000 Then
objWriter.WriteLine(s)
objWriter.Close()
MsgBox("Text Written To File")
End If
Catch ex As Exception
MsgBox("Data received error." & ex.Message)
End Try
End Sub
#End Region
Private Sub mouseMovement()
Dim tempPos As Point
Dim tP As Long = GetCursorPos(tempPos)
If l = True Then
If tempPos.X > 1 Then
SetCursorPos(tempPos.X - 1, tempPos.Y)
End If
End If
If r = True Then
If tempPos.X < 799 Then
SetCursorPos(tempPos.X + 1, tempPos.Y)
End If
End If
If u = True Then
If tempPos.Y > 1 Then
SetCursorPos(tempPos.X, tempPos.Y - 1)
End If
End If
If d = True Then
If tempPos.Y < 599 Then
SetCursorPos(tempPos.X, tempPos.Y + 1)
End If
End If
If lu = True Then
If tempPos.X > 1 Then
SetCursorPos(tempPos.X - 1, tempPos.Y - 1)
End If
End If
If ru = True Then
If tempPos.X > 799 Then
SetCursorPos(tempPos.X + 1, tempPos.Y - 1)
End If
End If
If ld = True Then
If tempPos.Y > 1 Then
SetCursorPos(tempPos.X - 1, tempPos.Y + 1)
End If
End If
If ru = True Then
If tempPos.Y < 599 Then
SetCursorPos(tempPos.X + 1, tempPos.Y + 1)
End If
End If
End Sub
Private Sub OpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenPort.Click
objWriter = New System.IO.StreamWriter("D:\project1\final1.txt", False)
Try
SerialPort1.Open()
OpenPort.Text() = "Close Port"
Catch ex As Exception
MsgBox("open port error : " + ex.ToString)
End Try
End Sub
Private Sub btnMoveClick_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _btnMoveClick.Click
' Convert the target to absolute screen coordinates.
Dim pt As Point = Me.PointToScreen(m_Target)
Dim MOUSEEVENTF_ABSOLUTE As Object
Dim MOUSEEVENTF_MOVE As Object
' mouse_event moves in a coordinate system where
' (0, 0) is in the upper left corner and
' (65535,65535) is in the lower right corner.
' Convert the coordinates.
Dim screen_bounds As Rectangle = Screen.GetBounds(pt)
Dim x As Integer = CInt(pt.X * 65535 / _
screen_bounds.Width)
Dim y As Integer = CInt(pt.Y * 65535 / _
screen_bounds.Height)
' Move the mouse.
mouse_event( _
MOUSEEVENTF_ABSOLUTE + _
MOUSEEVENTF_MOVE, _
x, y, 0, 0)
' Click there.
mouse_event( _
MOUSEEVENTF_ABSOLUTE Or _
MOUSEEVENTF_MOVE Or _
MOUSEEVENTF_LEFTDOWN Or _
MOUSEEVENTF_LEFTUP, _
x, y, 0, 0)
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim a As Boolean = MessageBox.Show("Do You Wish To Exit The Application?", "Confirmation", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No
If a = True Then
If SerialPort1.IsOpen Then
objWriter.Close()
SerialPort1.Close()
End If
e.Cancel = a
SerialPort1.Close()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
mouseMovement()
End Sub
End Class
|