Hi, can anyone advise how to make a button run code only while it is pressed? Basically I have found this code on the Net for changing the system volume, but you have to keep clicking on the buttons to change the volume. I want to be able to hold the UP/Down button down to continually increase/de-crease the volume.
Option Explicit On
Imports System.Runtime.InteropServices
Public Class Form1
Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
Private Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
Private Const APPCOMMAND_VOLUME_Down As Integer = &H90000
Private Const WM_APPCOMMAND As Integer = &H319
Private bRunning As Boolean
<DllImport("user32.dll")> Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal iParam As Integer) As IntPtr
End Function
'Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
'End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_Down))
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
End Sub
Private Sub Button1_MouseDown(Button As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
bRunning = True
Call doLoopedStuff()
End Sub
Private Sub Button1_MouseUp(Button As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
bRunning = False
Call doLoopedStuff()
End Sub
Private Sub doLoopedStuff()
Do While (bRunning = True)
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
Loop
End Sub
End Class
The problem I have with this code is that once I have pressed the UP button it starts the loop but ignores the MouseUP part. Infact I have to stop debugging to break out of it.