Good evening. How can I write a program (WinApi) that will work to respond to the keyboard. For example, press 'Q' and the program closes or collapses. Or pressing the 'space' key will cause the program to print the character 'S'. In general, what would the keys print completely different characters or execute other commands. If you can sample the implementation of at least one button.

Closed due to the fact that the issue is too common for the participants Vlad from Moscow , αλεχολυτ , Denis , HamSter , Bald 22 Nov '16 at 4:45 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Look for any examples of the event loop in any entry level tutorial. - PinkTux

1 answer 1

Well, here is an example of the event handling function on WinAPI, I don’t cite the main registration code for the window, read about it here: http://cppstudio.com/post/9621/ .

Specifically, this program catches all keystrokes from the keyboard and draws the letter of the corresponding key (ignoring service keys: home, pageup ...).

Here in the code switch/case (which accepts wParam ) is responsible for this, where case WM_KEYDOWN is the default option. (char)wParam is the code of the pressed key, your task is to add conditions and processing logic for each key you need.

 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hDc; PAINTSTRUCT ps; RECT rect; HFONT hFont; static char text[2] = { ' ', '\0' }; switch (uMsg) { case WM_CREATE: MessageBox(NULL, L"Стучи по клаве!", L"Слышь!", MB_ICONASTERISK | MB_OK); break; case WM_PAINT: hDc = BeginPaint(hWnd, &ps); GetClientRect(hWnd, &rect); SetTextColor(hDc, NULL); hFont = CreateFont(90, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, L"Arial Bold"); SelectObject(hDc, hFont); DrawText(hDc, (LPWSTR)text, 1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(hWnd, &ps); break; case WM_KEYDOWN: switch (wParam) { case VK_HOME:case VK_END:case VK_PRIOR: case VK_NEXT:case VK_LEFT:case VK_RIGHT: case VK_UP:case VK_DOWN:case VK_DELETE: case VK_SHIFT:case VK_SPACE:case VK_CONTROL: case VK_CAPITAL:case VK_MENU:case VK_TAB: case VK_BACK:case VK_RETURN: break; default: text[0] = (char)wParam; InvalidateRect(hWnd, NULL, TRUE); break; } break; case WM_DESTROY: PostQuitMessage(NULL); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); break; } return NULL;} 
  • thanks for the answer. I will try. - Petr
  • Something's failing me. With the addition of events for keys that are not listed in (wParam). text [0] = (char) wParam; For example, I need to replace the output of the same Q key with W. How can I paint it correctly? My options do not fit at all. Although the compiler is silent. - Petr
  • @Petr, well write that if (char) wParam is equal to the Q key code, I won’t say what the code is, look in the table of code as codes, then in the text [0] load the W key code. - Klym
  • @Petr, polish, watch what comes to wParam in any given situation. - Klym
  • thank. I will try. On the table now I’ll scroll all the parameters. - Petr