Keyboard hook

Posted on 2006-05-02
Last Modified: 2008-02-01

i found this code somewhere on the inet:

in a module:

Option Explicit

Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long

    vkCode As Long        'value of the key you pressed
    scanCode As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type

Public Const WH_KEYBOARD = 2
Public Const WH_KEYBOARD_LL = 13
Public Const HC_ACTION = 0
Public Const VK_DELETE = &H2E
Public KeyBoardHook As Long

'notice that the lparam is passed byref.  this is becuase lparam will be a pointer to a structure _
This is a user defined routine it will recieve messages from the keyboard before they are _
entered into the calling threads que.
Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, lParam As Long) As Long


    If nCode = HC_ACTION Then
        CopyMemory xpInfo, lParam, Len(xpInfo) 'copy the structure from lParam to xpinfo
        Debug.Print Chr(xpInfo.vkCode)
'        If xpInfo.vkCode = VK_DELETE Then 'the delete key was hit
'            If xpInfo.flags = 1 Then 'the delete key is in the down state
'                LowLevelKeyboardProc = -1 'In essence this will prevent the delete key from being recognized _
'                                           in the calling threads cue
'            End If
'        End If
        'this will be called if there are multiple hooks made to the keyboard
        LowLevelKeyboardProc = CallNextHookEx(KeyBoardHook, nCode, wParam, lParam) 'this will be called if there are multiple hooks made to the keyboard
    End If

End Function

in a form:
Private Sub Form_Load()
    'hook the keyboard and recieve messages from the keyboard
    KeyBoardHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'unhook the keyboard you will have some problems if this isnt called
    UnhookWindowsHookEx KeyBoardHook

End Sub

but it is printing the letters 2 times!
when i type hello,
the debug screen shows

how can i change this so it is working correcty?
Question by:Mark_FreeSoftware
    LVL 5

    Accepted Solution

    it prints for both keyup and keydown - check 31 bit of the lparam in LowLevelKeyboardProc

    LVL 13

    Author Comment



    when i check at the keyup od keydown it does indeed work,

    points to you!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    729 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

    23 Experts available now in Live!

    Get 1:1 Help Now