CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    Do not receive focus events in console

    Hi There,

    I thought this must have been asked many times. I use something like:
    Code:
        static HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
    
        const DWORD consoleInputCommandMode = ENABLE_ECHO_INPUT + ENABLE_PROCESSED_INPUT;
        SetConsoleMode(stdinHandle, consoleInputCommandMode);
        FlushConsoleInputBuffer(stdinHandle);
    
        INPUT_RECORD record;
        DWORD numRead;
    
        switch (WaitForSingleObject(stdinHandle, 1000)) {
            case(WAIT_OBJECT_0):
            if (ReadConsoleInput(stdinHandle, &record, 1, &numRead)) {
                if (record.EventType != KEY_EVENT) {
    // THIS IS THE PROBLEM, I just want key events, nothing else
                }
            }
                break;
        }
    I could disable mouse events but MENU_EVENT 0x0008 and FOCUS_EVENT 0x0010 keep coming.

    How can I get rid of those - I cannot see an option in SetConsoleMode;

    Thank you
    Last edited by luftwaffe; December 12th, 2023 at 03:34 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Do not receive focus events in console

    Well this is based upon how I do this. I use client/server with a shared circular buffer. This code is part of the console server and obtains chars from the keyboard and puts them in the buffer and converts 'special' codes depending upon conCodes array. The client code just takes chars from the buffer when any are available. I do it this way so that there can be different servers (eg I also have one for telnet).

    nterm->getstdin() returns a handle to the console input (stdinHandle).
    Code:
    ....
    for (char ch; WaitForSingleObject(nterm->getstdin(), INFINITE) != WAIT_TIMEOUT; ) {
    		bool got {};
    
    		if (DWORD no {}, evt {}; GetNumberOfConsoleInputEvents(nterm->getstdin(), &no) && no)
    			if (const auto ibuf {std::make_unique<INPUT_RECORD[]>(no)}; ReadConsoleInputA(nterm->getstdin(), ibuf.get(), no, &evt) && evt == no)
    				for (DWORD r {}; r < evt; ++r)
    					if ((ibuf[r].EventType == KEY_EVENT) && ibuf[r].Event.KeyEvent.bKeyDown)
    						if (const auto ker {ibuf[r].Event.KeyEvent}; ker.wVirtualKeyCode >= 0x21 && ker.wVirtualKeyCode <= 0x2f) {
    							nterm->in.wait_enqueue(conCodes[ker.wVirtualKeyCode - 0x21]);
    							got = true;
    						} else
    							if (ch = ker.uChar.AsciiChar; ch) {
    								nterm->in.wait_enqueue(ch);
    								got = true;
    							}
    
    						if (got)
    							nterm->setInpEvt();
    
    						ResetEvent(nterm->getstdin());
    	}
    ....
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    Re: Do not receive focus events in console

    Hmm, I was hoping that no threads will be needed - basically I would like to print something on timeout and something else on keypressed.

    You might be right and the idea is probably difficult since key pressed events are received multiple times while a key kept pushed, only release is received just once - so holding key is treated as multiple key press events.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Do not receive focus events in console

    No. You don't need threads. I just pasted as an example some of my code which is within a thread and explained why a queue is used. Where it uses wait_enqueue() just directly use the char. Alter the code as needed. It's just an example as a starter.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured