Controlling Mouse With KeyBoard

Posted on 2005-05-13
Last Modified: 2012-05-05
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.

Question by:Gupi
    LVL 12

    Accepted Solution

    Private Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Long, ByVal y As Long) As Long

    SetCursorPos 200,200
    LVL 12

    Expert Comment

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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:

    Alternatively, you can use the mouse_event() API and specify relative coordinates.  You can also make mouse clicks using this API:

    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
    LVL 6

    Expert Comment

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

    microsoft already made keyboard control of the mouse.. dont reinvent the wheel!
    LVL 6

    Expert Comment

    (it's for handicapped people)
    LVL 6

    Expert Comment

    (or people like me who broke their mouse accidentally last week)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now