Results 1 to 3 of 3

Thread: [RESOLVED] Keyboard Hooking Question

  1. #1

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Resolved [RESOLVED] Keyboard Hooking Question

    Ok, here I go again... I built an application that runs on top of a game that I play. I use a keyboard hook to intercept certain keys and pass them through my app. The problem I am haveing is that the commands are getting passed through my application, but is there a way to stop the keys from then being sent to the game as well.

    For example I want to intercept Ctrl + T which in my application does something, but in the game, it opens a window. What I want to happen is it to fire the event in my app and then not send that combination to the game and open the window.

    I hope I was clear with my question and I thank you in advance for any help

    This is the function I am currently using, which works, but the keys still get sent to the game

    VB Code:
    1. Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
    2.  
    3.         '  CTL Keys
    4.  
    5.         Dim key As Integer
    6.         Dim mcr As Integer
    7.         Dim VK_KEY As Integer
    8.  
    9.         For i As Integer = 0 To setKeys.GetUpperBound(0)
    10.             key = setKeys(i).subKey 'The subkey (1=Ctrl,2=Alt,0=none)
    11.             mcr = setKeys(i).mcr 'The macro index to be fired
    12.             VK_KEY = setKeys(i).key 'The key value
    13.  
    14.             If key = 1 Then 'Subkey is Control
    15.                 If (Hookstruct.vkCode = VK_KEY) And CBool(GetAsyncKeyState(VK_CONTROL) And &H8000) _
    16.                         And CBool(Hookstruct.flags And LLKHF_UP) Then
    17.                     Call mcrFire(mcr)
    18.                     Return True
    19.                 ElseIf (Hookstruct.vkCode = VK_KEY) And CBool(GetAsyncKeyState(VK_CONTROL) And &H8000) Then
    20.                     'Do nothing
    21.                 End If
    22.             ElseIf key = 2 Then 'Subkey is Alt
    23.                 If (Hookstruct.vkCode = VK_KEY) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) _
    24.                     And CBool(Hookstruct.flags And LLKHF_UP) Then
    25.                     Call mcrFire(mcr)
    26.                     Return True
    27.                 ElseIf (Hookstruct.vkCode = VK_KEY) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) Then
    28.                     'Do nothing
    29.                 End If
    30.             Else 'No Subkey
    31.                 If (Hookstruct.vkCode = VK_KEY) And CBool(Hookstruct.flags And LLKHF_UP) Then
    32.                     Call mcrFire(mcr)
    33.                     Return True
    34.                 ElseIf (Hookstruct.vkCode = VK_KEY) Then
    35.                     'Do nothing
    36.                 End If
    37.             End If
    38.         Next i
    39.  
    40.         'Page up and down for switching macro sets
    41.  
    42.         If (Hookstruct.vkCode = VK_NEXT) And CBool(Hookstruct.flags And LLKHF_UP) Then
    43.             MacroSets(1)
    44.             Return True
    45.         ElseIf (Hookstruct.vkCode = VK_NEXT) Then
    46.             Return True
    47.         End If
    48.  
    49.         If (Hookstruct.vkCode = VK_PRIOR) And CBool(Hookstruct.flags And LLKHF_UP) Then
    50.             MacroSets(-1)
    51.             Return True
    52.         ElseIf (Hookstruct.vkCode = VK_PRIOR) Then
    53.             Return True
    54.         End If
    55.  
    56.         Return False
    57.     End Function
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  2. #2
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Keyboard Hooking Question

    unfortunately, the low level keyboard hook does not allow you to change messages.

    see my signature for a link to hooking external processes

  3. #3

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Keyboard Hooking Question

    Thanks for the response Moeur. I actually figured out what I was doing wrong and got it working.

    I needed to return True on the key down event because it was sending to the game when the key down event was firing and then to my app on the key up event.

    Here is my fixed code

    VB Code:
    1. Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
    2.  
    3.         '  CTL Keys
    4.  
    5.         Dim key As Integer
    6.         Dim mcr As Integer
    7.         Dim VK_KEY As Integer
    8.  
    9.         For i As Integer = 0 To setKeys.GetUpperBound(0)
    10.             key = setKeys(i).subKey 'The subkey (1=Ctrl,2=Alt,0=none)
    11.             mcr = setKeys(i).mcr 'The macro index to be fired
    12.             VK_KEY = setKeys(i).key 'The key value
    13.  
    14.             If key = 1 Then 'Subkey is Control
    15.                 If (Hookstruct.vkCode = VK_KEY) And CBool(GetAsyncKeyState(VK_CONTROL) And &H8000) _
    16.                         And CBool(Hookstruct.flags And LLKHF_UP) Then
    17.                     Call mcrFire(mcr)
    18.                     Return True
    19.                 ElseIf (Hookstruct.vkCode = VK_KEY) And CBool(GetAsyncKeyState(VK_CONTROL) And &H8000) Then
    20.                     Return True '<-------Changed this to Return True
    21.                 End If
    22.             ElseIf key = 2 Then 'Subkey is Alt
    23.                 If (Hookstruct.vkCode = VK_KEY) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) _
    24.                     And CBool(Hookstruct.flags And LLKHF_UP) Then
    25.                     Call mcrFire(mcr)
    26.                     Return True
    27.                 ElseIf (Hookstruct.vkCode = VK_KEY) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) Then
    28.                     Return True '<-------Changed this to Return True
    29.                 End If
    30.             Else 'No Subkey
    31.                 If (Hookstruct.vkCode = VK_KEY) And CBool(Hookstruct.flags And LLKHF_UP) Then
    32.                     Call mcrFire(mcr)
    33.                     Return True
    34.                 ElseIf (Hookstruct.vkCode = VK_KEY) Then
    35.                     Return True '<-------Changed this to Return True
    36.                 End If
    37.             End If
    38.         Next i
    39.  
    40.         'Page up and down for switching macro sets
    41.  
    42.         If (Hookstruct.vkCode = VK_NEXT) And CBool(Hookstruct.flags And LLKHF_UP) Then
    43.             MacroSets(1)
    44.             Return True
    45.         ElseIf (Hookstruct.vkCode = VK_NEXT) Then
    46.             Return True
    47.         End If
    48.  
    49.         If (Hookstruct.vkCode = VK_PRIOR) And CBool(Hookstruct.flags And LLKHF_UP) Then
    50.             MacroSets(-1)
    51.             Return True
    52.         ElseIf (Hookstruct.vkCode = VK_PRIOR) Then
    53.             Return True
    54.         End If
    55.  
    56.         Return False
    57.     End Function

    Thanks again.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

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