当前位置: 首页>>代码示例>>C#>>正文


C# IConsole.ReadKey方法代码示例

本文整理汇总了C#中IConsole.ReadKey方法的典型用法代码示例。如果您正苦于以下问题:C# IConsole.ReadKey方法的具体用法?C# IConsole.ReadKey怎么用?C# IConsole.ReadKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IConsole的用法示例。


在下文中一共展示了IConsole.ReadKey方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Process

        public IAcceptsInput Process(IConsole console)
        {
            _textInput.Value = null;

            var isCancelled = false;
            var sb = new StringBuilder();

            console.CursorVisible = true;
            var x = console.CursorLeft;
            var y = console.CursorTop;

            while (true)
            {
                var isFinished = false;
                char c;
                switch ((c = console.ReadKey()))
                {
                    case (char)8: // backspace
                        if (sb.Length > 0)
                        {
                            console.SetCursorPosition(x + sb.Length - 1, y);
                            console.Write(' ');
                            sb.Remove(sb.Length - 1, 1);
                            console.SetCursorPosition(x + sb.Length, y);
                        }
                        break;
                    case (char)13: // enter
                        isFinished = true;
                        break;
                    case (char)27: // escape
                        isCancelled = true;
                        break;
                    default:
                        if (!char.IsControl(c))
                        {
                            sb.Append(c);
                            console.Write(c);
                            console.SetCursorPosition(x + sb.Length, y);
                        }
                        break;
                }

                if (isFinished || isCancelled)
                {
                    break;
                }
            }

            console.CursorVisible = false;
            for (int i = 0; i < sb.Length; i++)
            {
                console.SetCursorPosition(x + i, y);
                console.Write(' ');
            }

            _textInput.Value = isCancelled ? null : sb.ToString();
            return _textInput;
        }
开发者ID:bfriesen,项目名称:BadSnowstorm,代码行数:58,代码来源:TextInputProcessor.cs

示例2: Play

        public void Play(IConsole console, bool cancelOnEscape = false)
        {
            bool isCancelled = false;

            Action playNotes = () =>
            {
                foreach (var note in _notes)
                {
                    var durationMilliseconds = _tempo.GetDurationMilliseconds(note.NoteDuration);

                    if (note.NoteTone == Tone.Rest)
                    {
                        Thread.Sleep(durationMilliseconds);
                    }
                    else
                    {
                        console.Beep((int)note.NoteTone, durationMilliseconds);
                    }

                    if (isCancelled)
                    {
                        return;
                    }
                }
            };

            if (cancelOnEscape)
            {
                const char escape = (char)27;

                var playNotesTask = Task.Factory.StartNew(
                    () =>
                    {
                        playNotes();
                        if (!isCancelled)
                        {
                            console.SendChar(escape);
                        }
                    });

                var readKeyTask = Task.Factory.StartNew(
                    () =>
                    {
                        while (console.ReadKey() != escape)
                        {
                        }
                        isCancelled = true;
                    });

                Task.WaitAll(playNotesTask, readKeyTask);
            }
            else
            {
                playNotes();
            }
        }
开发者ID:bfriesen,项目名称:BadSnowstorm,代码行数:56,代码来源:Song.cs

示例3: GetMenuSelection

        private char GetMenuSelection(IConsole console)
        {
            char c;

            do
            {
                c = console.ReadKey();
            } while (_menuItems.All(menuItem => menuItem.Id != c));

            console.Write(c);
            Thread.Sleep(250);
            console.CursorLeft = console.CursorLeft - 1;
            console.Write(' ');

            return c;
        }
开发者ID:bfriesen,项目名称:BadSnowstorm,代码行数:16,代码来源:MenuInputProcessor.cs

示例4: while

		private static void WaitForCancelKey
			(
			ConsoleKeyInfo cancelKey,
			IConsole console,
			CancellationTokenSource cancellationTokenSource
			)
		{
			while (!cancellationTokenSource.IsCancellationRequested)
			{
				if (console.KeyAvailable)
				{
					var key = console.ReadKey(true);

					if (key == cancelKey)
					{
						cancellationTokenSource.Cancel();
						return;
					}
				}
				Thread.Sleep(100);
			}
		}
开发者ID:toolmagic,项目名称:RepeatableTask,代码行数:22,代码来源:ConsoleTask.cs

示例5: LineEditor

        public LineEditor(IConsole console, string name, int histsize)
        {
            this.Console = console;
            handlers = new Handler [] {
                new Handler (ConsoleKey.Home,       CmdHome),
                new Handler (ConsoleKey.End,        CmdEnd),
                new Handler (ConsoleKey.LeftArrow,  CmdLeft),
                new Handler (ConsoleKey.RightArrow, CmdRight),
                new Handler (ConsoleKey.UpArrow,    CmdHistoryPrev),
                new Handler (ConsoleKey.DownArrow,  CmdHistoryNext),
                new Handler (ConsoleKey.Enter,      CmdDone),
                new Handler (ConsoleKey.Backspace,  CmdBackspace),
                new Handler (ConsoleKey.Delete,     CmdDeleteChar),
                new Handler (ConsoleKey.Tab,        CmdTabOrComplete),

                // Emacs keys
                Handler.Control ('A', CmdHome),
                Handler.Control ('E', CmdEnd),
                Handler.Control ('B', CmdLeft),
                Handler.Control ('F', CmdRight),
                Handler.Control ('P', CmdHistoryPrev),
                Handler.Control ('N', CmdHistoryNext),
                Handler.Control ('K', CmdKillToEOF),
                Handler.Control ('Y', CmdYank),
                Handler.Control ('D', CmdDeleteChar),
                Handler.Control ('L', CmdRefresh),
                Handler.Control ('R', CmdReverseSearch),
                Handler.Control ('G', delegate {} ),
                Handler.Alt ('B', ConsoleKey.B, CmdBackwardWord),
                Handler.Alt ('F', ConsoleKey.F, CmdForwardWord),

                Handler.Alt ('D', ConsoleKey.D, CmdDeleteWord),
                Handler.Alt ((char) 8, ConsoleKey.Backspace, CmdDeleteBackword),

                // DEBUG
                //Handler.Control ('T', CmdDebug),

                // quote
                Handler.Control ('Q', delegate { HandleChar (Console.ReadKey (true).KeyChar); })
            };

            rendered_text = new StringBuilder ();
            text = new StringBuilder ();

            history = new History (Console, name, histsize);

            //if (File.Exists ("log"))File.Delete ("log");
            //log = File.CreateText ("log");
        }
开发者ID:stangelandcl,项目名称:csharp-repl,代码行数:49,代码来源:getline.cs


注:本文中的IConsole.ReadKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。