0

I write simple console application and I want to control it by key press There is my code:

#include "stdafx.h"
#include <windows.h>

int i = 1;
BOOL WINAPI ConsoleHandlerRoutine(DWORD dwCtrlType)
{
    if (dwCtrlType == CTRL_CLOSE_EVENT)
    {
        i = 0;
        return TRUE;
    }
    return FALSE;
}

int _tmain(int argc, _TCHAR* argv[])
{
    BOOL ret = SetConsoleCtrlHandler(ConsoleHandlerRoutine, TRUE);
    while(i == 1){
    SetCursorPos(200, 200);
        mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
        SetCursorPos(205, 205);
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }

}

how I can make program start and stop on keyboard press when console window not active?

Klasik
  • 872
  • 1
  • 9
  • 29

1 Answers1

1

Allowing a program to know what you're typing when it's not the active program is a pretty intrusive scenario.

You can do it though, with SetWindowsHookEx.

The callback function needs to have this signature.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180