0

I created a mouse position program that can be used to save your mouse position {X, Y} I realised that this is not going to be effective unless I implement a method where for example pressing "5" will save that position The only way i can save the position is by pressing the button, although that does work, there is no way to save the position without clicking the btn. Can anyone help? I would be very grateful

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub XYbtn_Click(sender As Object, e As EventArgs) Handles XYbtn.Click
        Dim mousep As Point = MousePosition
        MouseXY.Text = mousep.ToString()
        TimeCo.Start()
    End Sub

    Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
        LabelX.Text = "X"
        LabelY.Text = "Y"
        X2.Text = "X2"
        Y2.Text = "Y2"
    End Sub

    Private Sub TimeCo_Tick(sender As Object, e As EventArgs) Handles TimeCo.Tick
        Dim mousep As Point = MousePosition
        MouseXY.Text = mousep.ToString()
    End Sub

    Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
        LabelX.Text = Cursor.Position.X
        LabelY.Text = Cursor.Position.Y


    End Sub

    Private Sub save2_Click(sender As Object, e As EventArgs) Handles save2.Click
        X2.Text = Cursor.Position.X
        Y2.Text = Cursor.Position.Y
    End Sub

    Private Sub startBtn_Click(sender As Object, e As EventArgs) Handles startBtn.Click

    End Sub
End Class
motrix
  • 29
  • 6
  • Are you intending for this keypress to occur while YOUR app has focus, or when ANOTHER app has focus? These are two very different things. – Idle_Mind Mar 31 '22 at 12:28
  • Ill have the form on top so it has focus and I can use it. I forgot to mention that – motrix Mar 31 '22 at 12:34

1 Answers1

0

If your form will have focus, you can set the AcceptButton property of the FORM to saveBtn. This will make it so that when you press ENTER on the keyboard while your form has focus then that button will be pressed.

If you'd rather use the key approach then set the KeyPreview property of the Form to True and handle the KeyPress event:

Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
    If e.KeyChar = "5" Then
        Console.WriteLine("5")
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40