I decided to write a console Mario and ran into such a problem: how to catch the arrow click event on the keyboard, say via WinApi or using standard C ++ tools? (Windows)

void Game::input() { Key key; char c; while (true) { cin >> c; switch (c) { case up_key: movePlayer(player_pos + new Position(0, -3)); break; case down_key: movePlayer(player_pos + new Position(0, 1)); break; case right_key: movePlayer(player_pos + new Position(1, 0)); break; case left_key: movePlayer(player_pos - new Position(1, 0)); break; } show(); } } 
  • Standard C ++ means nothing at all. Only by means of OS (or third-party libraries). Specify the OS in which you work, and there already will help you. - Flowneee

2 answers 2

Well, in the Windows console, you can use the getch() not included in the standard, but is included in the SDK

 #include <stdio.h> #include <conio.h> int main(int argc, const char * argv[]) { int k1, k2; for(;;) { k1 = _getch(); if (k1 == 0xE0 || k1 == 0x00) { k2 = _getch(); switch(k2) { case 0x4B: printf("Left on %s kbd\n", k1 ? "main" : "extended"); break; case 0x48: printf("Up on %s kbd\n", k1 ? "main" : "extended"); break; case 0x4D: printf("Right on %s kbd\n", k1 ? "main" : "extended"); break; case 0x50: printf("Down on %s kbd\n", k1 ? "main" : "extended"); break; } } } } 
  • Harry, this is nice, but it is) are there any other options?) - Xambey
  • one
    And what will suit you? This is the easiest option you can come up with ... Want - see here for yourself: msdn.microsoft.com/en-us/library/windows/desktop/… - Harry
  • Harry, I get it, your version is really the easiest) - Xambey

Hooks need to use, hooks)

 #include <stdio.h> #include <Windows.h> #define _WIN32_WINNT 0x050 LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam ) { BOOL fEatKeystroke = FALSE; if( nCode == HC_ACTION ) { switch( wParam ) { case WM_KEYDOWN: case WM_SYSKEYDOWN: case WM_KEYUP: case WM_SYSKEYUP: PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam; if( fEatKeystroke = (p->scanCode == 0x48) ) { printf( "UP\n" ); /*keybd_event( 'B', 0, 0, 0 ); keybd_event( 'B', 0, KEYEVENTF_KEYUP, 0 );*/ break; } if( fEatKeystroke = (p->scanCode == 0x4B) ) { printf( "LEFT\n" ); /*keybd_event( 'B', 0, 0, 0 ); keybd_event( 'B', 0, KEYEVENTF_KEYUP, 0 );*/ break; } if( fEatKeystroke = (p->scanCode == 0x4D) ) { printf( "DOWN\n" ); /*keybd_event( 'B', 0, 0, 0 ); keybd_event( 'B', 0, KEYEVENTF_KEYUP, 0 );*/ break; } if( fEatKeystroke = (p->scanCode == 0x50) ) { printf( "RIGHT\n" ); /*keybd_event( 'B', 0, 0, 0 ); keybd_event( 'B', 0, KEYEVENTF_KEYUP, 0 );*/ break; } break; } } return(fEatKeystroke ? 1 : CallNextHookEx( NULL, nCode, wParam, lParam )); } int main() { // Install the low-level keyboard & mouse hooks HHOOK hhkLowLevelKybd = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0 ); // Keep this app running until we're told to stop MSG msg; while( !GetMessage( &msg, NULL, NULL, NULL ) ) { //this while loop keeps the hook TranslateMessage( &msg ); DispatchMessage( &msg ); } UnhookWindowsHookEx( hhkLowLevelKybd ); return(0); }