CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Keyboard Hook

  1. #1
    Join Date
    Dec 2000
    Location
    Los Angeles, CA
    Posts
    34

    Keyboard Hook

    I am looking for a DLL that can not only log, but also comsume keyboard key strokes using a system keyboard hook. I know VB cannot create such a DLL, but I was hoping someone might know of/have one that was created in another language [Delphi, C, C++].

    I have found several that I could use to log key strokes, but I specifically want to stop keystrokes from reaching the window that is in focus under certain conditions.

    Does anyone know if the condition must reside in the DLL in order for it to consume some keystrokes and not others? Also, if i end up needing to make the DLL myself, is there a way to raise an event from a DLL?

    Any info is appreciated.

    Nathan Anderson


  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Keyboard Hook

    I have written such a low level keyboard hook in Visual basic - it involves a lot of APIs, but is not as hard as you might think.

    The DLL does a whole raft of other things - subclassing windows, all the hooks, various graphics and file ahndling functions etc.

    I'm looking for people to Beta test it and it sounds like it would suit your requirement perfectly.

    Drop me a private message with an email address and I'll send it on.

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  3. #3
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Keyboard Hook

    I just remembered - the DLL ids also available for download from
    http://www.a1vbcode.com/app.asp?ID=894

    As it is an early Beta I would appreciate any commenst, error reports or suggestions for additional features.

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  4. #4
    Join Date
    Dec 2000
    Location
    Los Angeles, CA
    Posts
    34

    Re: Keyboard Hook

    I checked out the link you sent. Do you have any documentation thay says how to use this .DLL? My specific problem is that I need to intercept keystrokes so that they do not get to the window that has focus.

    Basically, I want to make an app that will run in the background until a certain keystroke event occurs [ALT+F10]. After receiving the ALT+F10, I want all subsequent keystrokes to be logged by my program, until a [RETURN] is logged. After which my app will return to it's "sleeper" state and allow keystrokes to be passed on to the app with focus.

    From all the info I have gathered it appears that I will need to consume the keystrokes within the .DLL, and the .DLL needs to be created with a language other than VB. This is because VB can only make ActiveX DLL's, not standard DLL's that are needed to make a system wide hook instead of a thread specific hook.

    Unfortunatly, I have limited experience with C, and no experience making a standard .DLL. So I don't really know where to begin if I were to make my own. I do have some C++ code that was used to create a really cool VB control. But I don't know how to modify it to do what I want [and I don't have a C++ compiler anyway].

    As far as beta testing your app or .DLL, I'll see what I can do with it. But I wouldn't have a daily use for a Euro converter, so I don't know how much time I can spend with it. If you can tell me how to interface with the .DLL myself I may have more use for it...

    Thanks for your help to this point.. Hopefully, we can help each other more...

    Nathan


  5. #5
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Keyboard Hook

    The latest versionof this DLL (which has numerous bugs removed, so is worth getting) is at [url]http://www.merrioncomputing.com/EventVb.dll[url]

    The way you need to use it is thus (in your application main form):

    '\\ Declarations
    option Explicit

    private withevents eventConnection as EventVB.APIFunctions
    private withevents enumConnection as EventVB.EnumHandler

    private bLogging as Boolean




    Then start the api connection objects in your Form load:

    private Sub Form_Load()

    '\\ Initialise the api dll connection
    set eventConnection = new EventVB.APIFunctions
    '\\ Initialise the hook message producer
    set enumConnection = eventConnection.EventhandlerLink

    '\\ Start a low level keyboard hook
    Call enumConnection.StartHook(WH_KEYBOARD_LL, 0, eventConnection.AppModuleHandle)


    End Sub




    and you must remember to release them in the form unload:

    private Sub Form_Load()

    '\\ Initialise the api dll connection
    set eventConnection = new EventVB.APIFunctions
    '\\ Initialise the hook message producer
    set enumConnection = eventConnection.EventhandlerLink

    '\\ Start a low level keyboard hook
    Call enumConnection.StartHook(WH_KEYBOARD_LL, 0, eventConnection.AppModuleHandle)


    End Sub




    Then put something in the error event to notify you of any API errors:

    private Sub eventConnection_ApiError(byval Number as Long, byval Source as string, byval Description as string)

    MsgBox Description, vbCritical, "API error - " & Source

    End Sub




    and then put your code in the low level keyboard hook process:

    private Sub enumConnection_HOOKPROCKEYBOARDLL(Action as EventVB.enHookCode, VirtualKey as Long, KeyStrokeInfo as Long, lMsgRet as Long)

    '\\ Put code to start and stop logging app here....


    End Sub




    Note that this hook will hook everything - including the VB runtime so I would recommend you get your logging system working and then plug in the keyboard hook.

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  6. #6
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Keyboard Hook

    The URL got mangled.
    The latest version is:
    http://www.merrioncomputing.com/Download/EventVB.dll

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

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

Featured