Search code examples
c++keyboardkey

How to perform an action when a key is released after being pressed(С++)?


I am making a program which should press another key when a key is released after being pressed. Such as, if you release an a key then the program should press d, if release d then press a. The program should emulate a user keystroke, and so that it does this even when another program is on top.

How can I do this?

Here is my code so far:

Header.h

#include <iostream>
#include <windows.h>
#include <conio.h>
//#include<stdlib.h>

bool isOn = false;
//bool a = false;
//bool d = false;

Source.cpp

#include "Header.h"

void OnOff();

void Action();

int main() 
{
    printf("Press: Home to start\n");
    while (true)
    {
        OnOff();
        if (isOn)
            Action();
        //std::cout << a << "," << d << std::endl;
    }
}

void Action() 
{
    // code should be here
}

void OnOff()
{
    if (_kbhit()) {
        switch (_getch())
        {
        case 71:
            if (!isOn)
            {
                printf("ON\n");
                isOn = true;
                break;
            }
            else if (isOn)
            {
                printf("OFF\n");
                isOn = false;
                break;
            }

        }
    }
}

Solution

  • NOTE: This answer was written before the following sentence was added to the question:

    The program should emulate a user keystroke, and so that it does this even when another program is on top.

    Therefore, this answer now only partially answers the question.

    The function _getch will only inform you when the user presses a key, but not when the user releases a key. If you want to be informed of both "key down" and "key up" events, then I suggest that you use the function ReadConsoleInput instead.

    Here is an example:

    #define WIN32_LEAN_AND_MEAN
    
    #include <Windows.h>
    #include <iostream>
    #include <cctype>
    
    void HandleKeyEvent( const KEY_EVENT_RECORD &key_event );
    void HandleMouseEvent( const MOUSE_EVENT_RECORD &mouse_event );
    
    int main( void )
    {
        HANDLE hStdin;
        INPUT_RECORD input[128];
        DWORD num_input;
    
        hStdin = GetStdHandle( STD_INPUT_HANDLE );
    
        while ( ReadConsoleInput( hStdin, input, sizeof input / sizeof *input, &num_input ) )
        {
            for ( DWORD i = 0; i < num_input; i++ )
            {
                if ( input[i].EventType == KEY_EVENT )
                {
                    HandleKeyEvent( input[i].Event.KeyEvent );
                }
            }
        }
    
        std::cerr << "Error calling ReadConsoleInput!\n";
    }
    
    void HandleKeyEvent( const KEY_EVENT_RECORD &key_event )
    {
        std::cout << "Key ";
        if ( std::isprint( static_cast<unsigned char>(key_event.uChar.AsciiChar) ) )
            std::cout << (char)std::toupper( static_cast<unsigned char>(key_event.uChar.AsciiChar) );
        else
            std::cout << "<unprintable>";
        if ( key_event.bKeyDown)
        {
            std::cout << " down!\n";
        }
        else
        {
            std::cout << " up!\n";
        }
    }
    

    This program has the following output when I press and release the W, A, S, D keys.

    Key W down!
    Key W up!
    Key A down!
    Key A up!
    Key S down!
    Key S up!
    Key D down!
    Key D up!