Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Controlling Mouse With KeyBoard

hi. Please tell me  in VB how do i move mouse pointer using keyboard ?
or how do i change the position of mouse pointer when i click or a command button or press enter when the focus is on it.
Very Urgent.
Quick Response is REQUESTED PLEASE.
Thanks.

Gupi
0
Gupi
Asked:
Gupi
  • 3
  • 2
1 Solution
 
str_kaniCommented:
Private Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Long, ByVal y As Long) As Long

SetCursorPos 200,200
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want to move it relative to the current position then use the GetCursorPos() API to the current location and then you add/subtract to those values and then use SetCursorPos() as str_kani has suggested:
http://www.mentalis.org/apilist/GetCursorPos.shtml

Alternatively, you can use the mouse_event() API and specify relative coordinates.  You can also make mouse clicks using this API:
http://www.mentalis.org/apilist/mouse_event.shtml

Option Explicit

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_MOVE = &H1
Private Const MOUSEEVENTF_ABSOLUTE = &H8000

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            mouse_event MOUSEEVENTF_MOVE, 0, -1, 0, 0
           
        Case vbKeyRight
            mouse_event MOUSEEVENTF_MOVE, 1, 0, 0, 0
           
        Case vbKeyDown
            mouse_event MOUSEEVENTF_MOVE, 0, 1, 0, 0
           
        Case vbKeyLeft
            mouse_event MOUSEEVENTF_MOVE, -1, 0, 0, 0
           
        Case vbKeyReturn
            mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
            mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
           
    End Select
End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cjardCommented:
in control panel, in accessibility settings, turn on MOUSE KEYS..

microsoft already made keyboard control of the mouse.. dont reinvent the wheel!
0
 
cjardCommented:
(it's for handicapped people)
0
 
cjardCommented:
(or people like me who broke their mouse accidentally last week)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now