Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

Keyboard hook


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

Public Type KBDLLHOOKSTRUCT
    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

Dim xpInfo As KBDLLHOOKSTRUCT

    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
    Else
        '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
h
h
e
e
l
l
l
l
o
o


how can i change this so it is working correcty?
0
Mark_FreeSoftware
Asked:
Mark_FreeSoftware
1 Solution
 
wings_gauravCommented:
it prints for both keyup and keydown - check 31 bit of the lparam in LowLevelKeyboardProc

-wings
0
 
Mark_FreeSoftwareAuthor Commented:

thanks!

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

points to you!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now