?
Solved

What is a hook?

Posted on 2004-09-08
7
Medium Priority
?
294 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 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