繁体   English   中英

C++ 解释/映射 getch() output

[英]C++ interpreting/mapping getch() output

考虑这个程序:

#include <iostream>
#include <string>

int main(int argc, char* argv[]) {
    std::string input;
    std::cin >> input;
}

用户可以输入任何字符串(或单个字符),程序将按原样转到 output(大写/小写或类似符号。@#$%^&* 取决于修饰符)。

所以,我的问题是:使用<conio.h>_getch()获得相同结果的最佳方法是什么? map _getch()键码到相应符号的最直接方法是什么(也取决于当前系统的区域设置)?

我试过的是:

while (true) {
    const int key = _getch();
    const char translated = VkKeyScanA(key); // From <Windows.h>
    std::cout << translated;
}

虽然这正确地映射了字母,但它们都是大写的,并且这没有 map 任何符号(并且没有考虑修饰符)。 例如:
当我输入 _ 或 - 时它输出 ╜
当我输入 [ 或 { 时它输出 █

跨平台解决方案将不胜感激。

以下代码有效:

// Code 1 (option 1)
while (true) 
{
    const int key = _getch(); // Get the user pressed key (int)
    //const char translated = VkKeyScanA(key);
    std::cout << char(key); // Convert int to char and then print it
}

// Code 2 (option 2)
while (true) 
{
    const char key = _getch(); // Get the user pressed key
    //const char translated = VkKeyScanA(key);
    std::cout << key; // Print the key
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM