So I am creating an application that allows users to send basic keystrokes to end certain processes. But I am running into some issues. I know the code I have will work the problem is I do not know where to put the following code to execute the commands:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Keys.ControlKey + Keys.I Then
            Try
                KillProcess("chrome.exe")
            Catch ex As Exception
                MessageBox.Show("Process May Not Be Running", "Error Closing Chrome.exe", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

Also I am wondering how to let the user add commands themselves and end them using the keystrokes that they choose. Thanks and help would be greatly appreciated!

My Entire Coding Class:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Keys.ControlKey + Keys.I Then
            Try
                KillProcess("chrome.exe")
            Catch ex As Exception
                MessageBox.Show("Process May Not Be Running", "Error Closing Chrome.exe", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Public Sub KillProcess(ByVal processName As String)

        On Error GoTo ErrHandler

        Dim oWMI
        Dim ret
        Dim sService
        Dim oWMIServices
        Dim oWMIService
        Dim oServices
        Dim oService
        Dim servicename

        oWMI = GetObject("winmgmts:")
        oServices = oWMI.InstancesOf("win32_process")

        For Each oService In oServices

            servicename = LCase(Trim(CStr(oService.Name) & ""))

            If InStr(1, servicename, LCase(processName), vbTextCompare) > 0 Then
                ret = oService.Terminate
            End If

        Next

        oServices = Nothing
        oWMI = Nothing

ErrHandler:
        Err.Clear()
    End Sub
End Class

Also, here is a screenshot of the application so you can see what I am working with:
[IMG]http://screensnapr.com/e/S0oLwA.png[/IMG]

Thanks again!

-Chris

Thanks for the reference but this did not help me. I am trying to make it where certain keystrokes end the process. I already have the code to end the process, I just don't know where to add it. Thanks anyways.

Ok so now I am running into a new issue. I fixed the issue of the Keystrokes but I am still getting into some problems.

I added the following code to TextBox1_KeyPress and I also added TextBox1.Focus() to form load and it will execute the first command perfectly but any command I add after that will not work:

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Keys.ControlKey + Keys.I Then
            Try
                KillProcess("chrome.exe")
            Catch ex As Exception
                MessageBox.Show("Process May Not Be Running", "Error Closing Chrome.exe", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        ElseIf Keys.ControlKey + Keys.L Then
            Try
                KillProcess("Skype.exe")
            Catch ex As Exception
                MessageBox.Show("Process May Not Be Running", "Error Closing Skype.exe", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub
End Class

Can you guys help me out? Thanks.

-Chris

UPDATE:

I have removed the TextBox and just have the ListView on the Form1 now. So, when I click either one of the commands, CTRL + I or CTRL + L, it closes both Chrome and Skype almost like it isn't checking for the Keystrokes that are being pressed it is just ending all the processes listed like I am clicking all of them. This is becoming very frustrating and help would be great right now. Thanks again and I will keep everyone updated as I continue to test.

-Chris

q.q.(quick question)
Do you only need to kill.processes if your app. is active or can it run in the background and kill.processes if certain keys are pressed?

you can use Context Menu Strip Control. First Create all menus and add shortcut key to each menu item also code behind each menu item.

Now set the textbox1.contextmenustrip=contextmenustrip1

Now u can use shortcuts easily .

Ok I have added a context menu strip and added the code to those buttons but when I use the shortcut I set it does not close the processes. Any help?

BTW thanks to everyone for helping out thus far.

Found out how to do this a much more simple way. Thanks for all the help even though most was reference :) Again, thanks!

-Chris

Care to share Chris, or is it a confidential type of "my coder's bag of goodies" item?

No it isn't confidential. What I did was add a Timer to each Hot Key I wanted to create so I made them all enabled on the Form_Load. Then on the Timer_Tick Event I added the following code:

Dim skey As Boolean
        skey = GetAsyncKeyState(Keys.PageUp)
        If skey = True Then
            KillProcess("chrome.exe")
        End If

Of course, I added the following code above my class:

<System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function GetAsyncKeyState(ByVal vkey As System.Windows.Forms.Keys) As Short
        'Nothing
    End Function

I know this is a quick way but this is just a small app for my friends and I because we are currently in college and need something that allows you to click a Hot Key and end certain processes. Also, this is kind of off topic, but are you yourself a Computer Programmer/Computer Software Engineer?

commented: thanks for provided solution.:) +10

Off topic? Send a p.m. and thank Dani for this handy p.m.'ing option to keep threads a little more on topic, especially for an IT forum. Then send me a p.m. If needed; otherwise, just a Hobbyist Programmer here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.