2014-09-10 127 views
0

考慮下面的代碼按鍵...如何檢測「>」和「<」在.NET控制檯應用程序

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 

我期望我按下鍵盤上的「>」鍵將產生調試輸出。但是,當我遍歷我的代碼時,按下「>」時IF語句的計算結果爲false。

使用Console.ReadKey()時檢測「>」按鍵的正確方法是什麼?

+0

Sepehr - 這正是我所需要的東西。謝謝。 – DWRoelands 2014-09-10 09:52:47

+0

有趣的是,我期望用Option Strict On標記,但它不會出現在 – 2014-09-10 09:57:45

+0

@dwroelands:現在是你的問題解決了嗎? – 2014-09-10 10:03:12

回答

1

嘗試一個實際的字符文字:If KeyPress.KeyChar = ">"c

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 

或者你可以使用像

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 
相關問題