如何通过按F2或F3之类的键使控制台打印某些内容

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

我正在尝试通过按f1,f2,f3等数字来使switch语句在控制台上显示一些内容。我愿意停止使用switch语句,但这是我最熟悉的,因此,如果解决方案将switch语句保留在其中。我不能不写更多而发布它。抱歉忘了,这是WINDOWS 10

#include <iostream>
#include <istream>
#include <fstream>
#include <string.h>
#include <conio.h>

using namespace std;

int main() {

    char gun;
    char att;

    cout << "Guns" << endl;

    cout << "[F1]   AK" << endl;
    cout << "[F2]   MP5" << endl;
    cout << "[F3]   M2" << endl;
    cout << "[F4]   SAR" << endl;
    cout << "[F5]   Tommy" << endl;
    cout << "[F6]   Custom" << endl;
    cin >> gun;

    cout << "Attachnments" << endl;

    cout << "[F7]   Holosight" << endl;
    cout << "[F8]   Simplesight" << endl;
    cout << "[F9]   Silencer" << endl;
    cout << "[F10]  8x Zoom Scope" << endl;
    cout << "[F11]  16x Zoom Scope" << endl;
    cout << "[F12]  No Attachment" << endl;
    cin >> att;

    system("CLS");

    switch (gun) {

    case'1':
        cout << "Current Gun: AK" << endl;
        break;
    case'2':
        cout << "Current Gun: MP5" << endl;
        break;
    case'3':
        cout << "Current Gun: M2" << endl;
        break;
    case'4':
        cout << "Current Gun: SAR" << endl;
        break;
    case'5':
        cout << "Current Gun: Tommy" << endl;
        break;
    case'6':
        cout << "Current Gun: Custom" << endl;
        break;
    }

    switch (att) {

    case'A':
        cout << "Current Attachment: Holosight" << endl;
        break;
    case'B':
        cout << "Current Attachment: Simplesight" << endl;
        break;
    case'C':
        cout << "Current Attachment: Silencer" << endl;
        break;
    case'D':
        cout << "Current Attachment: Muzzel Boost" << endl;
        break;
    case'E':
        cout << "Current Attachment: Muzzel Break" << endl;
        break;
    case'F':
        cout << "Current Attachment: 8x Zoom Scope" << endl;
        break;
    case'G':
        cout << "Current Attachment: 16x Zoom Scope" << endl;
        break;
    case'H':
        cout << "Current Attachment: Lasersight" << endl;
        break;
    }
}```

the numbers and letters are just place holders for the f1 f2 and f3 etc. 
c++ input switch-statement
2个回答
2
投票

[我认为pm100的答案是实现此目的的一种完全可接受的方法,但是这种技术所产生的代码将是脆弱的,并且依赖于平台(因为他提到的没有标准表示法。)

如果您满意,请使用他的答案。一种更便携的方法是使用终端控制库,最常见的是curses(从技术上讲,这是C库供参考)。主要实现有两种:pdcurses主要用于Windows,ncurses对于Mac和Linux。我不会在这里进行设置,因为这主要取决于库,但是如果要使其跨平台,则可以使用cmake,但这并不是一件容易的事(因为您必须处理pdcurses和ncurses分别)。


1
投票

没有功能键的标准表示。因此,首先计算一下在按下Fn时,哪些字符到达您的应用程序。它很可能超过一个字符。

© www.soinside.com 2019 - 2024. All rights reserved.