Solved

Retrive Win messages whitout focus in Borland Builder

Posted on 1998-11-02
12
502 Views
Last Modified: 2008-03-04
Hi.

I'm using Borland C++ Builder and I want to to be able to
read a lot of messages from Windows (like WM_KEYDOWN,WM_KEYUP,
WM_ENDSESSION ,WM_QUERYENDSESSION) in a program, whitout Windows having focus on that window.

I have seen a small program example that make their own windows from Win API calls, that handle WM_ENDSESSION, but I can't modify it to Borland way.

I want an simple example in how to "hook" this messages in Borland C++ Builder and to handle them in a procedure of my own (in my program, not in a DLL).

Do I need to use API SetWindowsHook, or can I do it without it?

The reason I want this is that I want a program
that listen to all keybord input.

/Per
0
Comment
Question by:PerBoman
[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
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1176819
You will have to use SetWindowsHook().  
You will have do this from a DLL, this cannot be done from an application.  The DLL can "inform" the EXE when it needs to.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176820
Note that your window will get the WM_ENDSESSION message even if it doesn't have the focus.  The focus only affects keyboard messages.

I'm wondering if you aren't trying to fix a problem the wrong way.  What is the actual goal, why do you want this?
0
 

Author Comment

by:PerBoman
ID: 1176821
I want to have my program listen to all keybord input.
(just for fun and learning)(and maybe save them to a file)
If for example, someone is writing in Notepad then I want my
program too see all that the user types in notepad.
I know now that I must use DLL.(for keybord input)
I have an example in Delphi, but I really can't convert
all the way out to C++, so I want an small example
that show how to do this in Borland C++ Builder.
(included the code for the dll)



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:jkr
ID: 1176822
Here's a _small_ example of a hook that monitors all message traffic & emulates XWin-like copy&paste using the middle mouse button (in 'plain Win32' ;-) :
LRESULT CALLBACK HookProc   (   int     nCode,  // hook code
                                WPARAM  wParam, // removal flag
                                LPARAM  lParam  // address of structure with message
                            )
{
    PMSG    pmsg    =   ( PMSG) lParam;

    if  (   0   >   nCode   ||  PM_NOREMOVE ==  wParam)
        {
            return  (   CallNextHookEx  (   g_hhk,
                                            nCode,
                                            wParam,
                                            lParam
                                        )
                    );
        }

    if  (       g_xArgs.uMouseButton    ==  pmsg->message
            ||  (       WM_SETCURSOR    ==  pmsg->message  
                    &&  g_xArgs.uMouseButton    ==  HIWORD  (   lParam)
                )
        )
        {
            /* this one is for us, so check key state */
            if  (       0x80000000 & GetKeyState    (   g_xArgs.sCopyKey)
                    ||  0x80000000 & GetKeyState    (   g_xArgs.sPasteKey)
                )
                {
                    pmsg->message   =   WM_KEYDOWN;
                    pmsg->wParam    =   VK_INSERT;
                    pmsg->lParam    =   0x01520001;
                }
        }

    return  (   CallNextHookEx  (   g_hhk,
                                    nCode,
                                    wParam,
                                    lParam
                                )
            );
}


0
 
LVL 11

Expert Comment

by:alexo
ID: 1176823
Maybe http://www.experts-exchange.com/Q.10058680 (5 points) can be of some assistance?
0
 

Author Comment

by:PerBoman
ID: 1176824
I want an example in Borland C++ Builder.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1176825
>> I want an example in Borland C++ Builder.
What is more compiler-independat then Win32 called by plain C ?????
0
 

Author Comment

by:PerBoman
ID: 1176826
OK, but I dont know how to connect it through SetWindowsHookEx.
Where do I put the code above, in the same file as my main
program or in a seperat file (dll).  
0
 
LVL 86

Expert Comment

by:jkr
ID: 1176827
You'll have to put it in a DLL in order to receive all messages without having the focus, and then call 'SetWindowsHookEx()' supplying 0 as the thread id and the instance handle of the DLL (can be obtained by 'LoadLibrary()' or intercepted in the 'DllMain()' function).
0
 

Expert Comment

by:cmihalache
ID: 1176828
If you don't know how to make a dll, you can install a timer in your window and at each WM_TIMER message, call GetAsyncKeyState to findout if a key is being pressed. But it i think it will be better with dll and SetWindowsHookEx.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1176829
Have you tried putting the hook in a DLL?
0
 

Accepted Solution

by:
shudecek earned 200 total points
ID: 1176830
I think Windows hooks using SetWindowsHook() and SetWindowsHookEx() that want to hook other process (other than the current) need to be in a DLL, hooks in the same .exe file only receive messages from the current process.

There is a good example of a keyboard hook on ZDnet (www.zdnet.com), i think its called ZOOMIN.ZIP, and it can be easily modified to monitor any other type off message
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 80
Least Squares Curve Fitting 4 134
c++, dynamic object by json 1 65
No module found pypyodbc, 3 58
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

752 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