Example of a rudimentary console app. I'd be interested in your approach to send "macros" back to the system using Direct Input. Hopefully, you can post some code to illustrate this macro sending concept.
Code:
#include <windows.h>
#include <stdio.h>
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#pragma comment (lib, "dinput8.lib")
LPDIRECTINPUT8 din;
LPDIRECTINPUTDEVICE8 directinputdevice;
void InitializeDirectInput(HINSTANCE hInstance, HWND hWnd)
{
DirectInput8Create(hInstance,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&din,
NULL);
din->CreateDevice(GUID_SysKeyboard,
&directinputdevice,
NULL);
directinputdevice->SetDataFormat(&c_dfDIKeyboard);
directinputdevice->SetCooperativeLevel(hWnd,
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
}
void DetectKeys(void)
{
static BYTE keystate[256];
directinputdevice->Acquire();
directinputdevice->GetDeviceState(256, (LPVOID)keystate);
if(keystate[DIK_A] & 0x80) // Check for a depressed 'A'
printf("A\n"); // This is where you'll implement
// your custom key capture routine
}
void CloseDirectInput(void)
{
directinputdevice->Unacquire();
din->Release();
}
void MessageLoop()
{
MSG msg;
while(TRUE)
{
DWORD dwStartPoint = GetTickCount();
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DetectKeys();
while ((GetTickCount() - dwStartPoint) < 25);
}
CloseDirectInput();
}
DWORD WINAPI MyDirectInput(LPVOID lpParm)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
InitializeDirectInput(hInstance, NULL);
MessageLoop();
return 0;
}
int main(int argc, char** argv)
{
HANDLE hThread;
DWORD dwThread;
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
MyDirectInput, (LPVOID) argv[0], NULL, &dwThread);
if (hThread)
return WaitForSingleObject(hThread,INFINITE);
else return 1;
}