What is a hook?

Posted on 2004-09-08
Last Modified: 2010-05-02
First off I want to thank everyone that helps me with this question. I’m not an expert, but hope to become one. I like programming, and one of the things I like to do with Visual Basic is to write macros to programs using API. But I don’t know how to write a macro that is activated by an event (like if a button on a form is pressed). I’m thinking I should use a hook, but don’t understand them. Can someone explain to me exactly what a hook is, using plain English? I don’t understand Microsoft’s explanation. I will be offering more points after I start using hooks and have more questions.

Question by:rcherne1
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12011987
A hook is simply a way of intercepting windows messages before they reach their intended targets.  You can then pass on the message to the target, change the message, or cancel the message.

>> But I don’t know how to write a macro that is activated by an event (like if a button on a form is pressed).

You don't need hooks to make a piece of code run when a button is pushed.  For instance, drag a CommandButton onto a form in the IDE and then double click on it.  You should get code like this:

    Private Sub Command1_Click()

    End Sub

Whatever is inside this block will be executed whenever that button is clicked.  So if you wanted to display a message box when the button is clicked you could modify it to this:

    Private Sub Command1_Click()
        MsgBox "Hello World!"
    End Sub

Now when you run the program and cick the button, a "Hello World!" message will appear.

When you are in the code view for a form, you can access other events using the two drop down menus at the top of the screen.  If you select "Command1" in the left drop down, you can then see all the events that you can write code for in the right drop down.


LVL 10

Expert Comment

ID: 12011990
I would imagine you are looking to use hooks to activate a key or in english terms press the key for you.

hooks are basically predefined names, in your case keys on your keyboard.

Hooks can be used to trap keys - This is useful if you are expecting the user to press a certain key.
Hooks can be used to execute keys - This is useful if you are trying to write a macro that uses keys.

I will warn you that in this particular case hooks are good except in cases where the macro would be used internationally, as you would have to take in account different keyboard layouts. (Though I have never dealt with that scenario, in the back of my mind it sounds as if it could be an issue)

LVL 19

Accepted Solution

Shauli earned 500 total points
ID: 12012024
As a nutshell, hooking is a way to monitor the message traffic between windows, and to "interfere" with subroutines to affect this process (if my English is good enough :))
See the link here:

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12012029
I hope I haven't insulted you with my very simple example.


Author Comment

ID: 12012038
Let me give you an example of what I’m looking for. Lets say I have a button on a vb form that activates the windows calculator. I what to do the math, but when I close the app I want to copy the result to a text box in my vb app.  

Author Comment

ID: 12012053
Sorry I didn’t word that correct (When I close the “Calculator App”) I want it to copy the result to a text box in my vb app.

Author Comment

ID: 12012134
Shauli I was answering jayca’s question. But I like your answer (hooking is a way to monitor the message traffic between windows, and to "interfere") “My vb app”. I’m giving you the points because I think I have the idea. Now can I get an example? My new question is (you can search for it) “How do I make a hook to copy a result from windows calculator?”

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

707 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

14 Experts available now in Live!

Get 1:1 Help Now