Need my program to take commands when not in the foreground.

closed account (Gz64jE8b)
As the title says I need my program to recognize when I press the spacebar, it only works when the program is in the foreground.

1
2
3
4
5
6
7
8
9
10
11
12
while(c=getch())
{
if(c==0x20)
{

GetCursorPos(&cursorPos);
int x = (int) cursorPos.x;
int y = (int) cursorPos.y;
SetCursorPos(x1, y1);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,…
SetCursorPos(x, y);


How can I make my program recognize when the spacebar is pressed when, for example, I'm in Google Chrome not the actual program itself.
Use RegisterHotKey() function.
closed account (Gz64jE8b)
Could I have a simple example of RegisterHotKey()?
This is taken from MSDN:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"

int _cdecl _tmain (
    int argc, 
    TCHAR *argv[])
{           
    if (RegisterHotKey(
        NULL,
        1,
        MOD_ALT | MOD_NOREPEAT,
        0x42))  //0x42 is 'b'
    {
        _tprintf(_T("Hotkey 'ALT+b' registered, using MOD_NOREPEAT flag\n"));
    }
 
    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0) != 0)
    {
        if (msg.message == WM_HOTKEY)
        {
            _tprintf(_T("WM_HOTKEY received\n"));            
        }
    } 
 
    return 0;
}
MSDN Wrote:
This function cannot associate a hot key with a window created by another thread.

The sentence that I copy pasted from the page about "RegisterHotKey()" http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx makes me think that in order to do what you are trying you either need to inject this in to the target process (DLL injection feels like the right way to go here) or you need to write a launcher for the target process with this function included in it. Does anyone have more input on this? It's not something I've tried myself before.

Topic archived. No new replies allowed.