#pragma once
#include <iostream>
#include <vector>
#include <conio.h>
#include <stdio.h>
#include <chrono>
#include <thread>
#include <Windows.h>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
using namespace std;
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_W 119
#define KEY_S 115
#define KEY_ENTER 13
#define KEY_SPACE 32
class Checkbox
{
public:
~Checkbox();
Checkbox(string headline, std::vector<string> lineText);
std::vector<int> run();
private:
string headline;
int lineCount;
unsigned int line = 0;
std::vector<int> checkboxesValue;
std::vector<string> checkboxesText;
int counter;
bool finish = false;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
void processInput(uint8_t input);
char getInput();
void tickLine();
void printLines();
};
。HCECKBOX.CPP
#include "stdafx.h"
#include "checkbox.h"
Checkbox::Checkbox(string headline, std::vector<string> lineText)
{
this->headline = headline;
this->checkboxesText = lineText;
this->lineCount = lineText.size();
for (size_t i = 0; i < lineText.size(); i++)
{
this->checkboxesValue.push_back(0);
}
}
Checkbox::~Checkbox()
{
}
std::vector<int> Checkbox::run()
{
printLines();
while (1)
{
while (!_kbhit())
{
}
processInput(_getch());
printLines();
if (finish)
{
return checkboxesValue;
}
}
}
void Checkbox::processInput(uint8_t input)
{
switch (input) {
case KEY_UP:
case KEY_W:
// W and up key: up
line = line - 1;
if (line == -1)
{
line = lineCount -1;
}
break;
case KEY_DOWN:
case KEY_S:
// S and down key: down
line = (line + 1) % lineCount;
break;
case KEY_ENTER:
// enter
finish = true;
break;
case KEY_SPACE:
// space: select
tickLine();
break;
}
}
char Checkbox::getInput()
{
while (!_kbhit())
{
}
char input = _getch();
return input;
}
void Checkbox::tickLine()
{
if (checkboxesValue[line])
{
checkboxesValue[line] = 0;
}
else
{
checkboxesValue[line] = 1;
}
}
void Checkbox::printLines()
{
system("cls");
SetConsoleTextAttribute(hConsole, 7);
cout << headline.c_str() << endl;
cout << "(Use arrow keys to navigate up and down, space bar to select and enter to submit.)" << endl;
for (size_t i = 0; i < checkboxesValue.size(); i++)
{
if (i == line)
{
SetConsoleTextAttribute(hConsole, 4);
}
else
{
SetConsoleTextAttribute(hConsole, 7);
}
cout << "[";
if (checkboxesValue[i])
{
cout << "x";
}
else
{
cout << " ";
}
cout << "] " << checkboxesText[i].c_str() << endl;
}
}
使用它,只需提供一个字符串的向量,它将返回int的向量,其中0(未检查)或1(检查)。
#include "stdafx.h"
#include "checkbox.h"
#include <string>
int main()
{
std::vector<string> lineTexts;
lineTexts.push_back("first line");
lineTexts.push_back("second line");
lineTexts.push_back("third line");
string headline = "Choose one of the following";
Checkbox checkbox(headline, lineTexts);
std::vector<int> result = checkbox.run();
cout << "Checkboxes: " << endl;
for (size_t i = 0; i < result.size(); i++)
{
cout << "line " << i << " " << result[i] << endl;
}
}
I managed to create a minimal solution with the _kbhit function from Alex F comment.
I am just printing out the console every time some chnage is made. Right before the printing I am clearing the console. With _khbit I am checking if the user is pressing one of the correct keyboard input. If a checkbox is checked or not, i am saving inside a vector. (Works only on windows!)
checkbox.h
#pragma once
#include <iostream>
#include <vector>
#include <conio.h>
#include <stdio.h>
#include <chrono>
#include <thread>
#include <Windows.h>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
using namespace std;
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_W 119
#define KEY_S 115
#define KEY_ENTER 13
#define KEY_SPACE 32
class Checkbox
{
public:
~Checkbox();
Checkbox(string headline, std::vector<string> lineText);
std::vector<int> run();
private:
string headline;
int lineCount;
unsigned int line = 0;
std::vector<int> checkboxesValue;
std::vector<string> checkboxesText;
int counter;
bool finish = false;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
void processInput(uint8_t input);
char getInput();
void tickLine();
void printLines();
};
checkbox.cpp
#include "stdafx.h"
#include "checkbox.h"
Checkbox::Checkbox(string headline, std::vector<string> lineText)
{
this->headline = headline;
this->checkboxesText = lineText;
this->lineCount = lineText.size();
for (size_t i = 0; i < lineText.size(); i++)
{
this->checkboxesValue.push_back(0);
}
}
Checkbox::~Checkbox()
{
}
std::vector<int> Checkbox::run()
{
printLines();
while (1)
{
while (!_kbhit())
{
}
processInput(_getch());
printLines();
if (finish)
{
return checkboxesValue;
}
}
}
void Checkbox::processInput(uint8_t input)
{
switch (input) {
case KEY_UP:
case KEY_W:
// W and up key: up
line = line - 1;
if (line == -1)
{
line = lineCount -1;
}
break;
case KEY_DOWN:
case KEY_S:
// S and down key: down
line = (line + 1) % lineCount;
break;
case KEY_ENTER:
// enter
finish = true;
break;
case KEY_SPACE:
// space: select
tickLine();
break;
}
}
char Checkbox::getInput()
{
while (!_kbhit())
{
}
char input = _getch();
return input;
}
void Checkbox::tickLine()
{
if (checkboxesValue[line])
{
checkboxesValue[line] = 0;
}
else
{
checkboxesValue[line] = 1;
}
}
void Checkbox::printLines()
{
system("cls");
SetConsoleTextAttribute(hConsole, 7);
cout << headline.c_str() << endl;
cout << "(Use arrow keys to navigate up and down, space bar to select and enter to submit.)" << endl;
for (size_t i = 0; i < checkboxesValue.size(); i++)
{
if (i == line)
{
SetConsoleTextAttribute(hConsole, 4);
}
else
{
SetConsoleTextAttribute(hConsole, 7);
}
cout << "[";
if (checkboxesValue[i])
{
cout << "x";
}
else
{
cout << " ";
}
cout << "] " << checkboxesText[i].c_str() << endl;
}
}
To use it just provide a vector of string and it will return a vector of int, with either 0 (not checked) or 1 (checked).
#include "stdafx.h"
#include "checkbox.h"
#include <string>
int main()
{
std::vector<string> lineTexts;
lineTexts.push_back("first line");
lineTexts.push_back("second line");
lineTexts.push_back("third line");
string headline = "Choose one of the following";
Checkbox checkbox(headline, lineTexts);
std::vector<int> result = checkbox.run();
cout << "Checkboxes: " << endl;
for (size_t i = 0; i < result.size(); i++)
{
cout << "line " << i << " " << result[i] << endl;
}
}
发布评论
评论(1)
我设法通过Alex f注释的_KBHIT函数创建了一个最小的解决方案。
每次制作一些货币时,我只是打印出控制台。就在打印之前,我要清理控制台。使用_khbit,我正在检查用户是否按下正确的键盘输入之一。如果是否检查了复选框,我将保存在矢量中。 (仅在Windows!)
复选框
。HCECKBOX.CPP
使用它,只需提供一个字符串的向量,它将返回int的向量,其中0(未检查)或1(检查)。
I managed to create a minimal solution with the _kbhit function from Alex F comment.
I am just printing out the console every time some chnage is made. Right before the printing I am clearing the console. With _khbit I am checking if the user is pressing one of the correct keyboard input. If a checkbox is checked or not, i am saving inside a vector. (Works only on windows!)
checkbox.h
checkbox.cpp
To use it just provide a vector of string and it will return a vector of int, with either 0 (not checked) or 1 (checked).