2017-04-30 2 views
-1

텍스트 편집 프로그램을 만들고 있습니다. (프로그래밍을 좋아하는 이유는 묻지 않지만 오히려 새로운 것을 묻습니다.) 나는 사용자 입력을 얻고 싶다. (더 나은 명령이 있다면 알려주기 위해 console.readline()을 시도했다.) 일단 특정 요구 사항이 만족되면 (예를 들어, 현재의 경우 입력이 한 문자 길어질 때).특정 요구 사항이 충족 될 때 readline을 중지하는 방법

그래서,

input = ReadLine() 

나는 그것에 if 문을 주입 다소 것처럼 그것을 중지하는 방법 : 키를 누를 경우

If input.length = 1 
     EndReadLine (or something like that) 

내가 검출하는 방법을 찾는 노력을 하지만 나는 시프트 키와 그 같은 것들에 대한 탐지만을 찾을 수 있었고, 문자 들인 & 숫자들에 대한 것이 아닙니다. 문자를 감지하는 방법이 있다면 매우 유용 할 것입니다.

+0

사용자가 입력 한 내용을 구문 분석하고 이에 따라 행동하십시오. – Plutonix

+0

어떻게 할 수 있습니까? –

+0

적어도 Enter를 누를 때까지는 ** ReadLine()을 ** 차단 ** 할 수 없습니다. 필요한 것은 [KeyAvailable()] (https://msdn.microsoft.com/en-us/library/system.console.keyavailable (v = vs.110) .aspx) 및 [ReadKey()] (https : //msdn.microsoft.com/en-us/library/x3h8xffw(v=vs.110).aspx) 긴밀한 루프에서 ... 그러나 이것은 입력 된 내용의 표시를 관리하고 수동으로 커서를 이동해야한다는 것을 의미합니다 . 이것은 생각보다 어렵습니다. –

답변

0

콘솔 응용 프로그램 인 경우 ReadLine의 대안을 쓰는 것은 어렵지 않습니다.

Console.WriteLine("Start to type. Press ctrl-enter to quit.") 

' Keep track of the pressed keys. 
Dim input = New StringBuilder() 

' An alternative could be a counter. Accept only n numbers of characters. 
Dim isTyping As Boolean = True 
While isTyping 
    ' Wait until a key was pressed. 
    Dim k = Console.ReadKey() 
    ' If ctrl-enter was pressed exit the loop (no need to add this to input). 
    ' Otherwise append the key to the input variable. 
    If k.Key = ConsoleKey.Enter AndAlso k.Modifiers = ConsoleModifiers.Control Then 
     isTyping = False 
    Else 
     input.Append(k.KeyChar) 
    End If 
End While 

' You can read what was typed, the pressed keys were send to the console. 
Console.WriteLine() 
Console.WriteLine("And the result is: {0}", input.ToString) 

Console.WriteLine() 
Console.WriteLine("Press a key to stop the program.") 
' Notice the parameter. The pressed key will not be send to the console. 
Console.ReadKey(True) 

또한 특정 키를 검색하는 방법에 대한 질문에 대답합니다.

하나의 키가 필요하기 때문에 훨씬 쉽습니다. 따라서 루프가 필요하지 않습니다.

Dim k = Console.ReadKey() 
Console.WriteLine("Key pressed: {0}", k.KeyChar)