簡體   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