如何检测“>”和“<” .NET控制台应用程序中的按键

时间:2014-09-10 09:39:03

标签: vb.net console keyboard-events

鉴于以下代码......

Dim KeyPress As ConsoleKeyInfo
Do
    KeyPress = Console.ReadKey(True)
    If KeyPress.KeyChar = Convert.ToChar(">") Then
      debug.WriteLine("Greater Than")
    End If
Loop While KeyPress.Key <> ConsoleKey.X

我希望按下“&gt;”键盘上的键会产生调试输出。但是,当我单步执行代码时,当“&gt;”时,IF语句的计算结果为false。被压了。

检测“&gt;”的正确方法是什么?使用Console.ReadKey()时按键?

2 个答案:

答案 0 :(得分:1)

尝试实际的字面值:If KeyPress.KeyChar = ">"c

答案 1 :(得分:0)

对代码进行简单的更改:

    Dim keypress As ConsoleKeyInfo
    Do
        keypress = Console.ReadKey()
        If keypress.KeyChar = Chr(62) Then
            Console.WriteLine("Greater Than")
        End If
    Loop While keypress.Key <> ConsoleKey.X

或者您可以使用Like

    Dim keypress As ConsoleKeyInfo
    Do
        keypress = Console.ReadKey()
        If keypress.KeyChar = ">"C Then
            Console.WriteLine("Greater Than")
        End If
    Loop While keypress.Key <> ConsoleKey.X