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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

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:

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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