Good Keyboard Hook RRS feed

  • Question

  • Hi,

    i wanted to ask weather anyone of you has a good code for a Keyboard Hook. I'am looking for a code in wich it differs between "key press", "key down" and "key up" .....  Anyone got a code?

    thx for helping ; )
    Sunday, January 18, 2009 9:15 PM

All replies

  • look at setwindowshookex, and wh_keyboard_ll.  There are a bunch of code samples on the net for what you want to do.

    Compensating what I don't know yet, with what I do know now.
    Sunday, January 18, 2009 10:50 PM
  • hi,

    i had a look at this: http://www.codeproject.com/KB/system/globalsystemhook.aspx

    and translated it to net...just for looking weather i could use this code but i had a problem with a declaration

    so here is the code:


    #Region "using ..."
    Imports System
    Imports System.Drawing
    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Windows.Forms
    #End Region

    Namespace Kennedy.ManagedHooks.SampleHookingApp
        Public Class MainForm
            Inherits System.Windows.Forms.Form
    #Region "Member Variables"

            Private buttonInstall As System.Windows.Forms.Button
            Private buttonUninstall As System.Windows.Forms.Button
            Private components As System.ComponentModel.Container = Nothing
            Private statusBar1 As System.Windows.Forms.StatusBar
            Private textBoxMessages As System.Windows.Forms.TextBox
            Private buttonAbout As System.Windows.Forms.Button

    #End Region

            Private mouseHook As Kennedy.ManagedHooks.MouseHook = Nothing
            Private keyboardHook As Kennedy.ManagedHooks.KeyboardHook = Nothing

            Public Sub New()
                ' Required for Windows Form Designer support

                ' Add any constructor code after InitializeComponent call

                ' EXAMPLE CODE SECTION

                mouseHook = New Kennedy.ManagedHooks.MouseHook()
                AddHandler mouseHook.MouseEvent, AddressOf mouseHook_MouseEvent

                keyboardHook = New Kennedy.ManagedHooks.KeyboardHook()
                AddHandler keyboardHook.KeyboardEvent, AddressOf keyboardHook_KeyboardEvent
            End Sub

            Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                If disposing Then
                    ' EXAMPLE CODE SECTION
                    If mouseHook IsNot Nothing Then
                        mouseHook = Nothing
                    End If
                    If keyboardHook IsNot Nothing Then
                        keyboardHook = Nothing
                    End If

                    If components IsNot Nothing Then
                    End If
                End If
            End Sub

    #Region "Windows Form Designer generated code"
            ''' <summary>
            ''' Required method for Designer support - do not modify
            ''' the contents of this method with the code editor.
            ''' </summary>
            Private Sub InitializeComponent()
                Me.buttonInstall = New System.Windows.Forms.Button()
                Me.buttonUninstall = New System.Windows.Forms.Button()
                Me.textBoxMessages = New System.Windows.Forms.TextBox()
                Me.statusBar1 = New System.Windows.Forms.StatusBar()
                Me.buttonAbout = New System.Windows.Forms.Button()
                ' buttonInstall
                Me.buttonInstall.Anchor = DirectCast((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right)), System.Windows.Forms.AnchorStyles)
                Me.buttonInstall.FlatStyle = System.Windows.Forms.FlatStyle.System
                Me.buttonInstall.Location = New System.Drawing.Point(8, 8)
                Me.buttonInstall.Name = "buttonInstall"
                Me.buttonInstall.Size = New System.Drawing.Size(268, 32)
                Me.buttonInstall.TabIndex = 0
                Me.buttonInstall.Text = "&Install Mouse and Keyboard Hooks"
                AddHandler Me.buttonInstall.Click, AddressOf Me.buttonInstall_Click
                ' buttonUninstall
                Me.buttonUninstall.Anchor = DirectCast((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right)), System.Windows.Forms.AnchorStyles)
                Me.buttonUninstall.FlatStyle = System.Windows.Forms.FlatStyle.System
                Me.buttonUninstall.Location = New System.Drawing.Point(8, 44)
                Me.buttonUninstall.Name = "buttonUninstall"
                Me.buttonUninstall.Size = New System.Drawing.Size(268, 32)
                Me.buttonUninstall.TabIndex = 1
                Me.buttonUninstall.Text = "&Uninstall Mouse and Keyboard Hooks"
                AddHandler Me.buttonUninstall.Click, AddressOf Me.buttonUninstall_Click
                ' textBoxMessages
                Me.textBoxMessages.Anchor = DirectCast(((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right)), System.Windows.Forms.AnchorStyles)
                Me.textBoxMessages.BackColor = System.Drawing.Color.White
                Me.textBoxMessages.Location = New System.Drawing.Point(0, 84)
                Me.textBoxMessages.Multiline = True
                Me.textBoxMessages.Name = "textBoxMessages"
                Me.textBoxMessages.[ReadOnly] = True
                Me.textBoxMessages.ScrollBars = System.Windows.Forms.ScrollBars.Both
                Me.textBoxMessages.Size = New System.Drawing.Size(348, 320)
                Me.textBoxMessages.TabIndex = 2
                Me.textBoxMessages.TabStop = False
                Me.textBoxMessages.Text = ""
                ' statusBar1
                Me.statusBar1.Location = New System.Drawing.Point(0, 404)
                Me.statusBar1.Name = "statusBar1"
                Me.statusBar1.Size = New System.Drawing.Size(348, 22)
                Me.statusBar1.TabIndex = 3
                ' buttonAbout
                Me.buttonAbout.Anchor = DirectCast(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)), System.Windows.Forms.AnchorStyles)
                Me.buttonAbout.FlatStyle = System.Windows.Forms.FlatStyle.System
                Me.buttonAbout.Location = New System.Drawing.Point(284, 24)
                Me.buttonAbout.Name = "buttonAbout"
                Me.buttonAbout.Size = New System.Drawing.Size(56, 36)
                Me.buttonAbout.TabIndex = 4
                Me.buttonAbout.Text = "&About"
                ' MainForm
                Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
                Me.ClientSize = New System.Drawing.Size(348, 426)
                Me.Name = "MainForm"
                Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
                Me.Text = "System Hook Test Application"

            End Sub
    #End Region

    #Region "Main Method"
            ''' <summary>
            ''' The main entry point for the application.
            ''' </summary>
            <STAThread()> _
           Private Shared Sub Main()
                ' Give our app an XP theme look.

                Application.Run(New MainForm())
            End Sub

    #End Region

            Private Sub buttonInstall_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                ' EXAMPLE CODE SECTION

                AddText("Adding mouse hook.")

                AddText("Adding keyboard hook.")

                buttonInstall.Enabled = False
                buttonUninstall.Enabled = True
            End Sub

            Private Sub buttonUninstall_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                ' EXAMPLE CODE SECTION

                AddText("Mouse hook removed.")

                AddText("Keyboard hook removed.")

                buttonInstall.Enabled = True
                buttonUninstall.Enabled = False
            End Sub

            Private Sub mouseHook_MouseEvent(ByVal mEvent As Kennedy.ManagedHooks.MouseEvents, ByVal point As Point)
                Dim msg As String = String.Format("Mouse event: {0}: ({1},{2}).", mEvent.ToString(), point.X, point.Y)
            End Sub


            Private Sub keyboardHook_KeyboardEvent(ByVal kEvent As Kennedy.ManagedHooks.KeyboardEvents, ByVal key As Keys)
                Dim msg As String = String.Format("Keyboard event: {0}: {1}.", kEvent.ToString(), key)
            End Sub

            Private Sub AddText(ByVal message As String)
                If message Is Nothing Then
                    Exit Sub
                End If

                Dim length As Integer = textBoxMessages.Text.Length + message.Length
                If length >= textBoxMessages.MaxLength Then
                    textBoxMessages.Text = ""
                End If

                If Not message.EndsWith(vbCr & vbLf) Then
                    message += vbCr & vbLf
                End If

                textBoxMessages.Text = message + textBoxMessages.Text
            End Sub

        End Class

    End Namespace


    But :  Kennedy.ManagedHooks.KeyboardEvents and Kennedy.ManagedHooks.MouseEvents isn't defined

    Monday, January 19, 2009 3:24 PM