读取stdin控制台输入-这是最好的方法,并且键值应始终为大写吗? [英] Reading stdin console input - is this the best way and should key value always be uppercase?

查看:44
本文介绍了读取stdin控制台输入-这是最好的方法,并且键值应始终为大写吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个控制台应用程序,该应用程序监视来自控制台的用户输入.因此,我想检测用户的按键按下.这是我第一次这样做,而且似乎不得不做大量的阅读才能拿出这小部分代码.所以我想知道这是否真的是最好的方法.

另外,wKeyCode始终是大写的值,有什么方法可以检测小写的吗?

还有我的最后一个问题.当我单击控制台然后键入时,除非我捕获按下的键,然后使用printf()将其写入控制台窗口,否则在控制台窗口中什么也不会出现,如我的代码所示.这是正常功能吗,并且是用户查看他们键入的内容的唯一方式出现在控制台中,以捕获按键并将其写入控制台吗?

I am writing a console app which monitors the user-input from a console. So I want to detect the user''s key presses. This is the first time I''ve done this, and seem to have had to do a lot of reading to come up with this small chunk of code. So I''m wondering if this really is the best way to do this.

In addition, the wKeyCode is always an upper case value, is there any way to detect lowercase?

And my final question. When I have clicked on the console then type, nothing appears in the console window, unless I capture the key pressed and then write it to the console window using printf(), as in my code. Is this the normal functionality, and is the only way for the user to see what they have typed appear in the console to capture the key press and write it to the console?

HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);

DWORD   saveMode;
SetConsoleMode(hStdin, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
//SetConsoleMode(hStdin, ENABLE_PROCESSED_INPUT);

while(1)
{
     if (WaitForSingleObject(hStdin, INFINITE) == WAIT_OBJECT_0)
     {
    check what event type occurred
    INPUT_RECORD r[512];
    DWORD read;
    ReadConsoleInput(hStdin, r, 512, &read);
    if(r->EventType == KEY_EVENT)
    {
        int k = 2;
        int key = r->Event.KeyEvent.wKeyCode;
        printf ("%c", key);
    }
    //printf("Read: %d\n", read);
     }
}

推荐答案

我将使用getch().有没有理由吗?
还有另一种选择:

I would use getch(). Any reason not to?
There is another option:

#include <iostream>

int main()
{
  std::cin.get();
}



希望这会有所帮助,
巴勃罗.



Hope this helps,
Pablo.


这篇关于读取stdin控制台输入-这是最好的方法,并且键值应始终为大写吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆