【问题标题】:Console : Check for Keypress (Esc)控制台:检查按键 (Esc)
【发布时间】:2016-03-07 18:32:08
【问题描述】:

在你标记这个之前,我已经尝试了几个在这里找到的解决方案(我发誓它不是重复的)并最终出现导致 VS 崩溃的错误。

编辑:有人说我的循环不起作用。问题不存在,它与按键有关

这是我的原始代码:

        while (true)
            {
                Console.WriteLine("Voer een getal in : ");
                string invoer = Console.ReadLine();
                Console.Write("Voer de macht in waarmee u wilt vermenigvuldigen :");
                string macht = Console.ReadLine();

                int getal = Convert.ToInt32(invoer);
                int getalmacht = Convert.ToInt32(macht);

                int uitkomst = (int)Math.Pow(getal, getalmacht);
                Console.WriteLine("De macht van " + getal + " is " + uitkomst + " .");
                Console.ReadLine();


            }

它工作正常,但它没有寻找按键。

这是一个检查按键并且不抛出错误的方法:

namespace Democonsole
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Press ESC to stop.");

        do
        {
            while (!Console.KeyAvailable)
            {

                Console.WriteLine("Voer een getal in : ");
                string invoer = Console.ReadLine();
                Console.Write("Voer de macht in waarmee u wilt vermenigvuldigen :");
                string macht = Console.ReadLine();

                int getal = Convert.ToInt32(invoer);
                int getalmacht = Convert.ToInt32(macht);

                int uitkomst = (int)Math.Pow(getal, getalmacht);
                Console.WriteLine("De macht van " + getal + " is " + uitkomst + " .");
                Console.ReadLine();


            }
        } while (Console.ReadKey(true).Key != ConsoleKey.Escape);




    }
}

}

但它会在运行时崩溃并给出打印屏幕中看到的错误。

还有其他方法吗?

编辑:添加了更多我应要求尝试的解决方案示例。

    static void Main(string[] args)
{
    var myWorker = new MyWorker();
    myWorker.DoStuff();
    Console.WriteLine("Press any key to stop...");
    Console.ReadKey();
}

休息

     while (true)
                    {
                        Console.WriteLine("Voer een getal in : ");
                        string invoer = Console.ReadLine();
                        Console.Write("Voer de macht in waarmee u wilt vermenigvuldigen :");
                        string macht = Console.ReadLine();

                        int getal = Convert.ToInt32(invoer);
                        int getalmacht = Convert.ToInt32(macht);

                        int uitkomst = (int)Math.Pow(getal, getalmacht);
                        Console.WriteLine("De macht van " + getal + " is " + uitkomst + " .");
                        Console.ReadLine();
                        Console.Keypress()



`

【问题讨论】:

  • 请在您的问题中以文本形式显示您的代码和错误。人们不会关注 3rd 方网站来查看您的代码和错误消息。
  • 显示您尝试过的内容
  • @SonerGönül 已编辑。虽然上次我发布了一个问题,但人们告诉我不要将代码放在帖子中。
  • @MaartenWachters - 谁告诉你的? 发布相关代码是个坏习惯!也许您发布了 太多 代码(这与根本不发布任何代码一样糟糕)。此处描述了有关多少代码是正确数量的指南:sscce
  • @CPR43 嗯?第二个实现到底有多正确?你真的看过它的作用吗???

标签: c#


【解决方案1】:

错误消息告诉您几乎所有关于该错误的信息,即您正在尝试将字符串转换为整数,而字符串无法转换为整数。可能是因为它是空的。

这只是问题的一部分,我稍后会解决。

首先,您的循环结构是错误的。只要没有按键等待读取,内部循环就会运行。但在循环体的末尾,您明确清除了控制台缓冲区的内容,因此永远不会退出内部循环。

考虑这个最小的例子:

while (!Console.KeyAvailable)
{
    Console.ReadLine();
}

假设当遇到while 语句时没有等待读取的键(这将跳过整个过程),内部语句将从击键中累积字符,直到您按下回车键,然后将字符作为字符串返回。几微秒后,它会检查你是否在输入后按下了另一个键,这实际上是不可能的。因此,这是一种编写无限循环的奇怪方式。

下一个问题是您要求用户输入并假设输入是有效的。不是,这就是导致您之前遇到的那些异常的原因。始终假设用户将输入您的程序没有预料到的内容,并弄清楚如何处理它出错。

例如:

string invoer = Console.ReadLine();
int getal;
if (!int.TryParse(invoer, out getal))
{
    Console.WriteLine("Invalid value '{0}'", invoer);
    continue;
}

如果该值没有转换为数字,那么这将抱怨它并返回到循环的开头。如果值为空,您可能还想尝试退出循环:

string invoer = Console.ReadLine();
if (string.IsNullOrEmpty(invoer))
    break;
int getal;
if (!int.TryParse(invoer, out getal))
{
    Console.WriteLine("Invalid value '{0}'", invoer);
    continue;
}

这至少可以让你摆脱目前没有的循环。

【讨论】:

  • @MaartenWachters 你的循环要么没有做你认为的事情,要么做了一些愚蠢的事情。如果你没问题,我帮不了你。
  • 我不确定你的意思。正如你在这里看到的imgur.com/JLkUm8s 它显然有效。
  • @MaartenWachters 你如何退出循环 Maarten?
  • 控制台键可用(这不起作用,我为什么来这里)。我尝试了您的解决方案,但它也不起作用,可能是我的错。
【解决方案2】:

我有一个使用以下解决方案的完全替代解决方案。标题是,你想要 esc 检查。所以就在这里。高度抄袭this solution

ConsoleHoKeyManager.cs>

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleHotKey
{
    public static class HotKeyManager
    {
        public static event EventHandler<HotKeyEventArgs> HotKeyPressed;

        public static int RegisterHotKey(Keys key, KeyModifiers modifiers)
        {
            _windowReadyEvent.WaitOne();
            int id = System.Threading.Interlocked.Increment(ref _id);
            _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key);
            return id;
        }

        public static void UnregisterHotKey(int id)
        {
            _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id);
        }

        delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key);
        delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);

        private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key)
        {
            RegisterHotKey(hwnd, id, modifiers, key);
        }

        private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id)
        {
            UnregisterHotKey(_hwnd, id);
        }

        private static void OnHotKeyPressed(HotKeyEventArgs e)
        {
            if (HotKeyManager.HotKeyPressed != null)
            {
                HotKeyManager.HotKeyPressed(null, e);
            }
        }

        private static volatile MessageWindow _wnd;
        private static volatile IntPtr _hwnd;
        private static ManualResetEvent _windowReadyEvent = new ManualResetEvent(false);
        static HotKeyManager()
        {
            Thread messageLoop = new Thread(delegate()
            {
                Application.Run(new MessageWindow());
            });
            messageLoop.Name = "MessageLoopThread";
            messageLoop.IsBackground = true;
            messageLoop.Start();
        }

        private class MessageWindow : Form
        {
            public MessageWindow()
            {
                _wnd = this;
                _hwnd = this.Handle;
                _windowReadyEvent.Set();
            }

            protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_HOTKEY)
                {
                    HotKeyEventArgs e = new HotKeyEventArgs(m.LParam);
                    HotKeyManager.OnHotKeyPressed(e);
                }

                base.WndProc(ref m);
            }

            protected override void SetVisibleCore(bool value)
            {
                // Ensure the window never becomes visible
                base.SetVisibleCore(false);
            }

            private const int WM_HOTKEY = 0x312;
        }

        [DllImport("user32", SetLastError = true)]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("user32", SetLastError = true)]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        private static int _id = 0;
    }


    public class HotKeyEventArgs : EventArgs
    {
        public readonly Keys Key;
        public readonly KeyModifiers Modifiers;

        public HotKeyEventArgs(Keys key, KeyModifiers modifiers)
        {
            this.Key = key;
            this.Modifiers = modifiers;
        }

        public HotKeyEventArgs(IntPtr hotKeyParam)
        {
            uint param = (uint)hotKeyParam.ToInt64();
            Key = (Keys)((param & 0xffff0000) >> 16);
            Modifiers = (KeyModifiers)(param & 0x0000ffff);
        }
    }

    [Flags]
    public enum KeyModifiers
    {
        Alt = 1,
        Control = 2,
        Shift = 4,
        Windows = 8,
        NoRepeat = 0x4000,
        None = 0
    }
}

程序.cs>

using ConsoleHotKey;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Democonsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press ESC to stop.");
            HotKeyManager.RegisterHotKey(Keys.Escape, KeyModifiers.None);
            HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(Console_CancelKeyPress);
            while (true)
            {
                Console.WriteLine("Voer een getal in : ");
                string invoer = Console.ReadLine();
                Console.Write("Voer de macht in waarmee u wilt vermenigvuldigen :");
                string macht = Console.ReadLine();

                int getal = Convert.ToInt32(invoer);
                int getalmacht = Convert.ToInt32(macht);

                int uitkomst = (int)Math.Pow(getal, getalmacht);
                Console.WriteLine("De macht van " + getal + " is " + uitkomst + " .");
                Console.ReadLine();
            }
        }

        static void Console_CancelKeyPress(object sender, HotKeyEventArgs e)
        {
            Environment.Exit(0);
        }
    }
}

您也必须参考 system.windows.forms。

【讨论】:

  • @Chris Taylor 应该为该解决方案提供功劳。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多