按键松开时如何执行动作(С++)?

问题描述 投票:0回答:0

伙计们正在做一个程序,当按下一个键后释放时应该按下另一个键。比如如果你松开一个“a”键程序应该按“d”,如果是“d”,“a”。我将非常感谢您的回答。

这是我的代码

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;
            }

        }
    }
}
c++ keyboard key
© www.soinside.com 2019 - 2024. All rights reserved.