Visual Basic Hotkey

Posted on 2011-04-25
Last Modified: 2012-05-11
When running the program I want to be able to press Alt+P to pause it and then Alt+P again to start it. Can someone help me impliment this into the following code.  

Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Drawing
Imports System
Imports System.Threading
Imports System.Diagnostics
Public Class Form1

    Private slowmode As Boolean = False
    Dim MyProcess As Process
    'Load Ccleaner
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        MoveMouseTo(Panel1.PointToScreen(New Point(895, 288)))

        Do While True
            Dim HandleColor As String = "#31AB4E"

            Dim pt As New Point(Panel1.PointToScreen(New Point(136, 174)))
            Dim clr As Color = System.Drawing.ColorTranslator.FromHtml(HandleColor)

            If IsColorAt(pt, clr) = True Then
                Label3.Text = "Scan Complete"
                Label3.Text = "System Scanning"
            End If

    End Sub
    'Grab the window to panel1
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim FHandle As IntPtr
        FHandle = FindWindow("msseces_class", Nothing)
        SetParent(FHandle, Panel1.Handle)

    End Sub

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=False)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, _
                                          ByVal lParam As IntPtr) As IntPtr
    End Function

    <System.Runtime.InteropServices.DllImport("USER32.DLL", CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    End Function

    Private Sub MoveMouseTo(ByVal targetPT As Point)
        Dim startPT As Point = Cursor.Position
        Dim distance As Single = PointToPointDist(startPT.X, startPT.Y, targetPT.X, targetPT.Y)
        Dim duration As Single = distance / 400 * 1000
        Dim startDT As DateTime = DateTime.Now
        Dim targetDT As DateTime = DateTime.Now.AddMilliseconds(duration)
        Dim percent As Single
        While DateTime.Now <= targetDT
            percent = DateTime.Now.Subtract(startDT).TotalMilliseconds / duration
            Cursor.Position = New Point(startPT.X + (targetPT.X - startPT.X) * percent, startPT.Y + (targetPT.Y - startPT.Y) * percent)
        End While
        Cursor.Position = targetPT
    End Sub

    Private Function PointToPointDist(ByVal Ax As Single, ByVal Ay As Single, ByVal Bx As Single, ByVal By As Single) As Single
        ' PointToPointDist = SquareRoot((Bx - Ax)^2 + (By - Ay)^2)
        Return Math.Sqrt((Bx - Ax) * (Bx - Ax) + (By - Ay) * (By - Ay))
    End Function

    Private Sub Delay(ByVal DelayInSeconds As Integer)
        Dim targetDT As DateTime = DateTime.Now.Add(TimeSpan.FromSeconds(DelayInSeconds))
        While targetDT > DateTime.Now
            System.Threading.Thread.Sleep(50) ' <-- very SMALL delay
            Application.DoEvents() ' <-- keep UI responsive
        End While
    End Sub

    Private Function IsColorAt(ByVal pt As Point, ByVal clr As Color) As Boolean
        Dim bmp As New Bitmap(1, 1)
        Using G As Graphics = Graphics.FromImage(bmp)
            G.CopyFromScreen(pt, New Point(0, 0), bmp.Size)
        End Using
        Return bmp.GetPixel(0, 0).Equals(clr)
    End Function

    Public Class Clicked
        Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
        Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down 
        Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up 
        Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down 
        Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up 

        Public Shared Sub LeftDown()
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        End Sub

        Public Shared Sub LeftUp()
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        End Sub

        Public Shared Sub RightDown()
            mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        End Sub

        Public Shared Sub RightUp()
            mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
        End Sub
    End Class

End Class

Open in new window

Question by:Jack_Jones
    LVL 83

    Expert Comment

    When the program has the focus or even if it hasn't?
    LVL 85

    Accepted Solution

    See the two solutions here:

    You can either register your combo as a hotkey, or detect it with a low level keyboard hook.
    LVL 1

    Author Closing Comment

    Thanks again Idle that example worked perfectly.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now