Win32 Keyposting
A small Win32 program I made to simulate the space key every 5 minutes to keep a program from idling out. It just inserts the key code into the keyboard stream and does not manipulate anything or touch the program itself. It was compiled in Visual C++ 2008 with the following settings:
Runtime library: /MT (makes it standalone).
Compile as C Code (/TC).
Main code:
#include <windows.h> #include <time.h> #include "resource.h" /* Generic Win32 handler for close, destroy and other OS messages to the program. */ LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_CLOSE: DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, Msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { /* Declarations. */ WNDCLASSEX wc; HWND hWnd; MSG Msg; INPUT SpamKey = {0}; KEYBDINPUT kb = {0}; time_t timer_start, timer_stop; double timer_diff; /* Misc initializations. */ ZeroMemory(&Msg, sizeof(MSG)); timer_start = time(NULL); kb.wVk = VK_SPACE; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = (WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = "MainClass"; /* Register window class. */ if(!RegisterClassEx(&wc)) { /* Silently exits if it fails. */ return 0; } /* Create the window. */ if((hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,"MainClass","IdleGuard",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,250,100,NULL,NULL,hInstance,NULL)) == 0) { return 0; } /* Explicitly show the window. */ ShowWindow(hWnd, SW_SHOW); /* Start message loop. */ while(Msg.message != WM_QUIT) { /* Sleep 50 milliseconds to lower the CPU cycle stress */ Sleep(50); /* Properly handle WM_CLOSE and WM_DESTROY messages. */ if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) != 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } /* Only spam key every 5 minutes, should be ok for most MMO logouts. */ timer_stop = time(NULL); if((timer_diff = difftime(timer_stop,timer_start)) >= 300) { /* Simulate the space key. */ SpamKey.type = INPUT_KEYBOARD; kb.dwFlags = 0; SpamKey.ki = kb; SendInput(1,&SpamKey,sizeof(INPUT)); Sleep(250); kb.dwFlags = KEYEVENTF_KEYUP; SpamKey.ki = kb; SendInput(1,&SpamKey,sizeof(INPUT)); timer_start = time(NULL); } } return (int)Msg.wParam; }