使用的Readline()和ReadKey()同时 [英] Using Readline() and ReadKey() Simultaneously

查看:771
本文介绍了使用的Readline()和ReadKey()同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法同时检测的ReadLine和ReadKey,因此在大多数情况下,它表现为一个readline的,除应检测到一些特殊的关键投入?



我需要一些水货的实施引进同时发生。
下面的代码是同步的,不符合我的需要。



 ,而((行=到Console.ReadLine()) !=X)
{如果(行==BLABLA

{
//东西
}

,否则
{
//东西
}

ConsoleKeyInfoき= Console.ReadKey(真);
如果((ki.Key == ConsoleKey.V)及及(ki.Modifiers == ConsoleModifiers.Control))
{
//东西
}
}


解决方案

不,不是这样。这两种方法都被阻塞,直到用户输入的控制台上的东西。所以,即使你会找到一种方法,有并列的两个来看,这将是不确定的哪一个得到的第一枪。



有一个(不显着)相似问题:如何让到Console.ReadLine()中止/而无需用户输入一定的时间后分手。



有过这个问题,在这里多次尝试:





大部分都是围绕要么仿照创建您自己的,增加了一个超时的ReadLine 函数的版本(或某些字符(代码)你的情况特殊处理),或者使用某种线程。



这两种方法要么是不平凡的,或有自己的问题(请确保您查看的意见,即使是公认的答案)。



总之,我认为你需要与你的包括特殊处理,而且很多真正的推出自己版本的ReadLine的基础上, Console.ReadKey 到Console.ReadLine 您需要的行为。注意,这甚至包括一些基本的东西作为返回,箭头键,退格处理等



更新:有是的 getline.cs 代码从的 Mono项目,它实现就像是被一些古老的UNIX外壳提供了一个在线编辑功能(emacs模式,如果你愿意)。对于这一点,我相信这将需要实现某种的ReadLine更换的,虽然我还没有检查。也许你可以使用它作为一个起点。


Is there any way to detect both Readline and ReadKey, so that in most cases it behaves as a readline, except for some special key inputs that should be detected?

I need some "parallel" implementation to introduce simultaneity. The code below is synchronous and does not meet my need

while ((line = Console.ReadLine()) != "x")
{    
    if (line == "BLABLA")
    {
        //Stuff
    }

    else
    {
        //Stuff
    }

    ConsoleKeyInfo ki = Console.ReadKey(true);
    if ((ki.Key == ConsoleKey.V) && (ki.Modifiers == ConsoleModifiers.Control))
    {
        //Stuff
    }
}

解决方案

No, not as such. Both methods block until the user enters something on the console. So even if you would find a way to have both run in parallel, it will not be deterministic which one gets the first shot.

There is a (not obvious) similar problem: how to make Console.ReadLine() abort/break after a certain amount of time without user input.

There have been multiple attempts for this problem here:

Most are modelled around either creating your own version of a ReadLine function that adds a timeout (or in your case special handling for certain character (codes)) or the use some sort of threading.

Both ways are either non-trivial or have their own issues (make sure you review the comments, even for the accepted answers).

In short, I think you will need to roll your own version of ReadLine, based on Console.ReadKey with your special handling included and that much of the genuine Console.ReadLine behavior that you require. Note that this even include such basic things as RETURN, ARROW KEYS, BACKSPACE handling, etc.

Update: There is the getline.cs Code from the Mono project, which implements a line editing capability like it was provided by some venerable UNIX shells (EMACS mode, if you care). For that, I believe it will need to implement some sort of ReadLine replacement, although I haven't checked. Maybe you can use this as a starting point.

这篇关于使用的Readline()和ReadKey()同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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