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 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:

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

730 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