?
Solved

What is a hook?

Posted on 2004-09-08
7
Medium Priority
?
285 Views
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.

Thanks,
Rich  
0
Comment
Question by:rcherne1
[X]
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
7 Comments
 
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.

Regards,

Idle_Mind
0
 
LVL 10

Expert Comment

by:jayca
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)

Example: http://www.codeguru.com/vb/gen/vb_system/keyboard/article.php/c4831
0
 
LVL 19

Accepted Solution

by:
Shauli earned 1500 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:

http://www.vbaccelerator.com/home/VB/Code/Libraries/Hooks/vbAccelerator_Hook_Library/article.asp

S
0
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.

=/
Idle_Mind
0
 

Author Comment

by:rcherne1
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.  
0
 

Author Comment

by:rcherne1
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.
0
 

Author Comment

by:rcherne1
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?”
0

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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