Results 1 to 9 of 9

Thread: How Do I Make This A System Wide Hook?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    How Do I Make This A System Wide Hook?

    How do I make this a system wide keyboard hook because this code only traps keys while my application is in focus!

    VB Code:
    1. '******* MODULE CODE *******'
    2. Option Explicit
    3.  
    4. Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
    5. Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer
    6. 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
    7. Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    8.  
    9. Public TheHook As Long
    10.  
    11. Public Const WH_KEYBOARD = 2
    12.  
    13. Const VK_LBUTTON = &H1
    14. Const VK_RBUTTON = &H2
    15. Const VK_CANCEL = &H3
    16. Const VK_MBUTTON = &H4
    17. Const VK_BACK = &H8
    18. Const VK_TAB = &H9
    19. Const VK_CLEAR = &HC
    20. Const VK_RETURN = &HD
    21. Const VK_SHIFT = &H10
    22. Const VK_CONTROL = &H11
    23. Const VK_MENU = &H12
    24. Const VK_PAUSE = &H13
    25. Const VK_CAPITAL = &H14
    26. Const VK_ESCAPE = &H1B
    27. Const VK_SPACE = &H20
    28. Const VK_PRIOR = &H21
    29. Const VK_NEXT = &H22
    30. Const VK_END = &H23
    31. Const VK_HOME = &H24
    32. Const VK_LEFT = &H25
    33. Const VK_UP = &H26
    34. Const VK_RIGHT = &H27
    35. Const VK_DOWN = &H28
    36. Const VK_SELECT = &H29
    37. Const VK_PRINT = &H2A
    38. Const VK_EXECUTE = &H2B
    39. Const VK_SNAPSHOT = &H2C
    40. Const VK_INSERT = &H2D
    41. Const VK_DELETE = &H2E
    42. Const VK_HELP = &H2F
    43. Const VK_0 = &H30
    44. Const VK_1 = &H31
    45. Const VK_2 = &H32
    46. Const VK_3 = &H33
    47. Const VK_4 = &H34
    48. Const VK_5 = &H35
    49. Const VK_6 = &H36
    50. Const VK_7 = &H37
    51. Const VK_8 = &H38
    52. Const VK_9 = &H39
    53. Const VK_A = &H41
    54. Const VK_B = &H42
    55. Const VK_C = &H43
    56. Const VK_D = &H44
    57. Const VK_E = &H45
    58. Const VK_F = &H46
    59. Const VK_G = &H47
    60. Const VK_H = &H48
    61. Const VK_I = &H49
    62. Const VK_J = &H4A
    63. Const VK_K = &H4B
    64. Const VK_L = &H4C
    65. Const VK_M = &H4D
    66. Const VK_N = &H4E
    67. Const VK_O = &H4F
    68. Const VK_P = &H50
    69. Const VK_Q = &H51
    70. Const VK_R = &H52
    71. Const VK_S = &H53
    72. Const VK_T = &H54
    73. Const VK_U = &H55
    74. Const VK_V = &H56
    75. Const VK_W = &H57
    76. Const VK_X = &H58
    77. Const VK_Y = &H59
    78. Const VK_Z = &H5A
    79. Const VK_STARTKEY = &H5B
    80. Const VK_CONTEXTKEY = &H5D
    81. Const VK_NUMPAD0 = &H60
    82. Const VK_NUMPAD1 = &H61
    83. Const VK_NUMPAD2 = &H62
    84. Const VK_NUMPAD3 = &H63
    85. Const VK_NUMPAD4 = &H64
    86. Const VK_NUMPAD5 = &H65
    87. Const VK_NUMPAD6 = &H66
    88. Const VK_NUMPAD7 = &H67
    89. Const VK_NUMPAD8 = &H68
    90. Const VK_NUMPAD9 = &H69
    91. Const VK_MULTIPLY = &H6A
    92. Const VK_ADD = &H6B
    93. Const VK_SEPARATOR = &H6C
    94. Const VK_SUBTRACT = &H6D
    95. Const VK_DECIMAL = &H6E
    96. Const VK_DIVIDE = &H6F
    97. Const VK_F1 = &H70
    98. Const VK_F2 = &H71
    99. Const VK_F3 = &H72
    100. Const VK_F4 = &H73
    101. Const VK_F5 = &H74
    102. Const VK_F6 = &H75
    103. Const VK_F7 = &H76
    104. Const VK_F8 = &H77
    105. Const VK_F9 = &H78
    106. Const VK_F10 = &H79
    107. Const VK_F11 = &H7A
    108. Const VK_F12 = &H7B
    109. Const VK_F13 = &H7C
    110. Const VK_F14 = &H7D
    111. Const VK_F15 = &H7E
    112. Const VK_F16 = &H7F
    113. Const VK_F17 = &H80
    114. Const VK_F18 = &H81
    115. Const VK_F19 = &H82
    116. Const VK_F20 = &H83
    117. Const VK_F21 = &H84
    118. Const VK_F22 = &H85
    119. Const VK_F23 = &H86
    120. Const VK_F24 = &H87
    121. Const VK_NUMLOCK = &H90
    122. Const VK_OEM_SCROLL = &H91
    123. Const VK_OEM_1 = &HBA
    124. Const VK_OEM_PLUS = &HBB
    125. Const VK_OEM_COMMA = &HBC
    126. Const VK_OEM_MINUS = &HBD
    127. Const VK_OEM_PERIOD = &HBE
    128. Const VK_OEM_2 = &HBF
    129. Const VK_OEM_3 = &HC0
    130. Const VK_OEM_4 = &HDB
    131. Const VK_OEM_5 = &HDC
    132. Const VK_OEM_6 = &HDD
    133. Const VK_OEM_7 = &HDE
    134. Const VK_OEM_8 = &HDF
    135. Const VK_ICO_F17 = &HE0
    136. Const VK_ICO_F18 = &HE1
    137. Const VK_OEM102 = &HE2
    138. Const VK_ICO_HELP = &HE3
    139. Const VK_ICO_00 = &HE4
    140. Const VK_ICO_CLEAR = &HE6
    141. Const VK_OEM_RESET = &HE9
    142. Const VK_OEM_JUMP = &HEA
    143. Const VK_OEM_PA1 = &HEB
    144. Const VK_OEM_PA2 = &HEC
    145. Const VK_OEM_PA3 = &HED
    146. Const VK_OEM_WSCTRL = &HEE
    147. Const VK_OEM_CUSEL = &HEF
    148. Const VK_OEM_ATTN = &HF0
    149. Const VK_OEM_FINNISH = &HF1
    150. Const VK_OEM_COPY = &HF2
    151. Const VK_OEM_AUTO = &HF3
    152. Const VK_OEM_ENLW = &HF4
    153. Const VK_OEM_BACKTAB = &HF5
    154. Const VK_ATTN = &HF6
    155. Const VK_CRSEL = &HF7
    156. Const VK_EXSEL = &HF8
    157. Const VK_EREOF = &HF9
    158. Const VK_PLAY = &HFA
    159. Const VK_ZOOM = &HFB
    160. Const VK_NONAME = &HFC
    161. Const VK_PA1 = &HFD
    162. Const VK_OEM_CLEAR = &HFE
    163.  
    164. Public KeyLog As String
    165.  
    166. Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    167.     Dim Key As Integer
    168.     Dim Shift As Boolean
    169.    
    170.     If idHook < 0 Then
    171.         KeyboardProc = CallNextHookEx(TheHook, idHook, wParam, ByVal lParam)
    172.     Else
    173.        
    174.         If GetAsyncKeyState(VK_SHIFT) And &H8000 Then
    175.             Shift = True
    176.         Else
    177.             Shift = False
    178.         End If
    179.  
    180.        
    181.         For Key = 65 To 90
    182.             If (GetAsyncKeyState(Key) And &H1) Then
    183.                 If Shift = True Then
    184.                     KeyLog = KeyLog + UCase$(Chr(Key))
    185.                 Else
    186.                     KeyLog = KeyLog + LCase$(Chr(Key))
    187.                 End If
    188.             End If
    189.         Next Key
    190.  
    191.         For Key = 48 To 57
    192.             If (GetAsyncKeyState(Key) And &H1) Then
    193.                 If Shift = True Then
    194.                     Select Case Key
    195.                     Case 49
    196.                         KeyLog = KeyLog + "!"
    197.                     Case 50
    198.                         KeyLog = KeyLog + "@"
    199.                     Case 51
    200.                         KeyLog = KeyLog + "#"
    201.                     Case 52
    202.                         KeyLog = KeyLog + "$"
    203.                     Case 53
    204.                         KeyLog = KeyLog + "%"
    205.                     Case 54
    206.                         KeyLog = KeyLog + "^"
    207.                     Case 55
    208.                         KeyLog = KeyLog + "&"
    209.                     Case 56
    210.                         KeyLog = KeyLog + "*"
    211.                     Case 57
    212.                         KeyLog = KeyLog + "("
    213.                     Case 48
    214.                         KeyLog = KeyLog + ")"
    215.                     End Select
    216.                 Else
    217.                     KeyLog = KeyLog + Chr(Key)
    218.                 End If
    219.             End If
    220.         Next Key
    221.  
    222.         If (GetAsyncKeyState(VK_BACK) And &H1) Then KeyLog = KeyLog & "{BackSpace}"
    223.  
    224.         If (GetAsyncKeyState(VK_SPACE) And &H1) Then KeyLog = KeyLog + " "
    225.  
    226.         If (GetAsyncKeyState(VK_RETURN) And &H1) Then KeyLog = KeyLog + Chr(13)
    227.        
    228.         KeyboardProc = CallNextHookEx(TheHook, idHook, wParam, ByVal lParam)
    229.        
    230.     End If
    231. End Function
    232.  
    233.  
    234.  
    235. '******* Form Code *******'
    236. Private Sub Form_Load()
    237.     TheHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
    238. End Sub
    239.  
    240. Private Sub Form_Unload(Cancel As Integer)
    241.     UnhookWindowsHookEx TheHook
    242. End Sub

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Why don't you try GetAsyncKeyBoardState api call?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    What would that do? I don't understand what you're going at?

  4. #4
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    this is hwat you want. it hooks the system using the journal hooks. it will also capture Left,middle,right mouse down, keyboard, and scroll wheel.
    Attached Files Attached Files
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  5. #5
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    269
    BuggyProgrammer

    Thanks for that file, but I cant seem to get it to do things when a keyboard button is pressed.

    It responds great to mouse clicks etc.

    What do I have to change in the module to get it to respond to, say a ctrl + 1 key press, or a simple 1 key press.

    Thanks

  6. #6
    Lively Member
    Join Date
    Jul 2000
    Location
    Lost in the Mojave Desert
    Posts
    82
    set app.threadid to 0& that is for system wide hook
    Don't go away mad, just go away.

    Bam-Bam

  7. #7
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463
    Hi,

    I downloaded the attachment that BuggyProgrammer uploaded. How would I actually be able to capture the keys into a textbox though? It looks like this program knows when a key is pressed but can it tell me which key itself is pressed so I can log it?

    Thanks for your help!

  8. #8
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    what you could do is use GetAsyncKeyState or GetKeyboardState to get the current key.

    if you are in 2000/xp you could implement a low-level keyboard hook (WH_KEYBOARD_LL), which would allow it to hook all keyboard messages, regardless where the focus is.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  9. #9
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463
    Do you mean use a timer to do this? I'm trying to find a way to use a hook DLL. I downlaoded a couple from here and on PSC that were delphi and also a C++ one and both stopped working after a while for no given reason.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width