3

I'm trying to make a simple mouse macro program but i cannot figure out how to get the coordinates of the mouse outside of the form itself. I have tried with MousePosition and Cursor.Position but this only seems to take the coordinates inside the form.

I have looked around on the internet but most examples are to capture inside the form/application and the only similar question i found on StackOverflow was unanswered. VB.net -- Getting mouse coordinates outside the form

So, is the a simple way to capture the mouse coordinates outside of its own program? If not a simple way, could someone explain the complicated way a bit? I would really appreciate it!

Community
  • 1
  • 1
Vahx
  • 626
  • 10
  • 23

1 Answers1

4

Assuming WinForms, you can try using just a timer:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  Me.Text = MousePosition.ToString
End Sub
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • This works indeed for tracing the coordinates, but i am also trying to add the coordinates to a list when i click, so that can be later to replay the actions. But when i click outside of the form, the click does not register because the form is not focused anymore, the event is ofcourse the form_mouseclick event, is there any event in VB that will work when clicking outside of the form or will i need to create my own event for it? or is there another way i can obtain this result? – Vahx Mar 18 '14 at 21:14
  • @Vahx You can replace `MousePosition` in my code with `MouseButtons`, which would tell you which mouse button is pressed at the time of the tick event. – LarsTech Mar 18 '14 at 21:55
  • thx but its more of where the button was clicked instead of which button. – Vahx Mar 19 '14 at 05:31
  • 1
    @Vahx Sorry, I misunderstood your comment. You are basically talking about a global hook, not events from your form. See [Processing Global Mouse and Keyboard Hooks in C#](http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C) – LarsTech Mar 19 '14 at 12:07
  • ok thankx, looks a bit complicated but i found some VB variants on the net so i think i'll figure it out. :) answer accepted – Vahx Mar 19 '14 at 20:38
  • This gave me the results I needed, however rather then set up a timer I just used system.threading.thread.sleep(2000) followed by Me.Text = MousePosition.ToString – Rob Feb 14 '22 at 17:26