locked
Cursor movement using keyboard to draw

    Question

  • Hi

    I need to move the cursor using keyboard keys to draw graphics on the form.

    Here's so far i manage to move the cursor using the keyboard arrows.

     

    Code Snippet

    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

    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20

    Public Const MOUSEEVENTF_MIDDLEUP = &H40

    Public Const MOUSEEVENTF_RIGHTDOWN = &H8

    Public Const MOUSEEVENTF_RIGHTUP = &H10

     

    Public x As Integer

    Public y As Integer

     

    Public Sub New()

    ' This call is required by the Windows Form Designer.

    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    x = 500

    y = 300

    SetCursorPos(x, y)

    End Sub

     

    Private Sub Form_Load()

    End Sub

     

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    Dim tempPos As Point

    Dim R As Long = GetCursorPos(tempPos)

    x = tempPos.X

    y = tempPos.Y

    Select Case e.KeyCode

    Case Keys.Up

    y = y - 10

    e.Handled = True

    Case Keys.Down

    y = y + 10

    e.Handled = True

    Case Keys.Left

    x = x - 10

    e.Handled = True

    Case Keys.Right

    x = x + 10

    e.Handled = True

    Case Keys.Enter

    LeftClick()

    e.Handled = True

    End Select

    SetCursorPos(x, y)

    End Sub

     

    Public Sub LeftClick()

    LeftDown()

    LeftUp()

    End Sub

     

    Public Sub LeftDown()

    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)

    End Sub

     

    Public Sub LeftUp()

    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

    End Sub

     

    Public Sub RightClick()

    RightDown()

    RightUp()

    End Sub

     

    Public Sub RightDown()

    mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)

    End Sub

     

    Public Sub RightUp()

    mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

    End Sub

     

     

    I had my drawing form done with button controls.

     

     

    Any advice what i should do next? Im stuck. I need to integrate my cursor movement into my graphics.

    Wednesday, August 06, 2008 7:43 AM

Answers

  • You don't need to use those API functions to do this, it can be done with .Net functions.

     

    It's not really clear what you want to do next but have a look at the following in a new project which may give you some ideas.  It lets you move the cursor with the arrow keys and draw a straight line by holding down the Control key.

     

    Public Class Form1

    Dim X As Integer = 150

    Dim Y As Integer = 150

    Dim StartPoint As Point

    Dim EndPoitn As Point

    Dim Drawing As Boolean

    Dim Bmp As Bitmap

    Dim G As Graphics

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    If e.KeyCode = Keys.ControlKey Then

    StartPoint = New Point(X, Y)

    Drawing = True

    Exit Sub

    End If

    Select Case e.KeyCode

    Case Keys.Down

    Y += 5

    Cursor.Position = PointToScreen(New Point(X, Y))

    Invalidate()

    Case Keys.Up

    Y -= 5

    Cursor.Position = PointToScreen(New Point(X, Y))

    Invalidate()

    Case Keys.Left

    X -= 5

    Cursor.Position = PointToScreen(New Point(X, Y))

    Invalidate()

    Case Keys.Right

    X += 5

    Cursor.Position = PointToScreen(New Point(X, Y))

    Invalidate()

    End Select

    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

    If e.KeyCode = Keys.ControlKey Then

    G.DrawLine(Pens.Black, StartPoint, New Point(X, Y))

    Drawing = False

    Invalidate()

    End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    KeyPreview = True

    Cursor.Position = PointToScreen(New Point(X, Y))

    Bmp = New Bitmap(ClientRectangle.Width, ClientRectangle.Height)

    G = Graphics.FromImage(Bmp)

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    If Drawing Then

    e.Graphics.DrawLine(Pens.Black, StartPoint, New Point(X, Y))

    End If

    e.Graphics.DrawImage(Bmp, New Point(0, 0))

    End Sub

    End Class

    Wednesday, August 06, 2008 12:34 PM

All replies

  • You don't need to use those API functions to do this, it can be done with .Net functions.

     

    It's not really clear what you want to do next but have a look at the following in a new project which may give you some ideas.  It lets you move the cursor with the arrow keys and draw a straight line by holding down the Control key.

     

    Public Class Form1

    Dim X As Integer = 150

    Dim Y As Integer = 150

    Dim StartPoint As Point

    Dim EndPoitn As Point

    Dim Drawing As Boolean

    Dim Bmp As Bitmap

    Dim G As Graphics

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    If e.KeyCode = Keys.ControlKey Then

    StartPoint = New Point(X, Y)

    Drawing = True

    Exit Sub

    End If

    Select Case e.KeyCode

    Case Keys.Down

    Y += 5

    Cursor.Position = PointToScreen(New Point(X, Y))

    Invalidate()

    Case Keys.Up

    Y -= 5

    Cursor.Position = PointToScreen(New Point(X, Y))

    Invalidate()

    Case Keys.Left

    X -= 5

    Cursor.Position = PointToScreen(New Point(X, Y))

    Invalidate()

    Case Keys.Right

    X += 5

    Cursor.Position = PointToScreen(New Point(X, Y))

    Invalidate()

    End Select

    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

    If e.KeyCode = Keys.ControlKey Then

    G.DrawLine(Pens.Black, StartPoint, New Point(X, Y))

    Drawing = False

    Invalidate()

    End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    KeyPreview = True

    Cursor.Position = PointToScreen(New Point(X, Y))

    Bmp = New Bitmap(ClientRectangle.Width, ClientRectangle.Height)

    G = Graphics.FromImage(Bmp)

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    If Drawing Then

    e.Graphics.DrawLine(Pens.Black, StartPoint, New Point(X, Y))

    End If

    e.Graphics.DrawImage(Bmp, New Point(0, 0))

    End Sub

    End Class

    Wednesday, August 06, 2008 12:34 PM
  • Thanks Dave!

    That was sooo cool!

     

    Anyway my project is to have a control panel, then select and draw lines and shapes by using keyboard. I googled and cant find any examples. And also forgot to mention, the lines and shapes must be rubber band. I can do the rubber band thingy.

     

    Here's how my project looks like:

    http://i260.photobucket.com/albums/ii36/ashura517/untitled.jpg

     

     

     

    Any link where i can study from? Drawing program using keyboard keys?
    Wednesday, August 06, 2008 5:21 PM