0

Im making a stop watch to measure how long it takes for certain weapons to empty their clip in CS:GO. I have the stopwatch programmed, but it only seems to work if im clicking within the bounds of the window. If its possible, does anyone know how to make it able to start/stop even while in another program? Ill post the code below.

Public Class Form1
Dim WithEvents timer As New Timer
Dim milliseconds As Integer

Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
    Timer1.Start()
End Sub

Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
    Timer1.Stop()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    totalTime.Text = 0.0
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    totalTime.Text = totalTime.Text + 0.01
End Sub
End Class
stuartd
  • 70,509
  • 14
  • 132
  • 163
zacsloth
  • 31
  • 1
  • 7
  • Why not use the [stopwatch class](https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx)? – stuartd Feb 03 '15 at 01:13
  • Other programs like to use the mouse as well. You can see the MouseUp by setting the form's Capture property to True. But MouseDown, no, that's for whatever the user clicks on. Use a bulls-eye image to make it obvious. – Hans Passant Feb 03 '15 at 01:19
  • Are you wanting to time how long the mouse is held down in another application? http://stackoverflow.com/questions/23626869/how-to-capture-mouse-windows-messages-out-of-the-application – Derek Tomes Feb 03 '15 at 01:22
  • 1
    Check this answer out, it uses a C# library (you can reference this and write your VB) but it allows you to get the MouseUp and MouseDown events outside the bounds of your form which is what you're asking: http://stackoverflow.com/questions/20449658/how-to-handle-user-clicking-mouse-outside-a-form – b.pell Feb 03 '15 at 01:47
  • I have a Razor Dark Widow keyboard which comes with 5 macro keys. Could I simulate a mouse click (starting the timer, firing the gun, stopping the timer) with one of the macro keys? – zacsloth Feb 03 '15 at 04:08
  • I think the link p.bells provided above is probably what you're after. – Derek Tomes Feb 03 '15 at 19:46

1 Answers1

2

The MouseDown and MouseUp events only happen when the mouse clicks within your application so you can't use them as events to track the time the mouse is held down in another application.

You may be able to do what you want using a global mouse hook or by looking at windows messages such as the Mouse Down and Mouse Up messages

Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
  • could I, for example, use key down/ key up with the 'a' key and bind the weapon to shoot with the 'a' key in game? Or would that still require a hook? – zacsloth Feb 03 '15 at 13:04
  • If the game you are trying to time things for isn't part of your application then your vb.net won't be receiving the events that you need to time things. This is a fundamental part of running many applications on windows. To over simplify it slightly - it doesn't matter if it's a key press or a mouse click; windows figures out which application the event belongs to and only sends the notification to that application. – Derek Tomes Feb 03 '15 at 19:41