2017-03-28 4 views
0

나는 C#에서 텍스트 상자에 임의의 단어를 입력하여 타이핑 테스트를하려고합니다. 사용자가 단어 입력을 마쳤 으면 공간을 누르거나 다음 단어로 이동하기 위해 입력 할 수 있기를 원합니다. SPACEBAR 또는 ENTER 키의 입력을 인식하도록 프로그램을 가져올 수 있다면 나머지 작업 방법을 알고 있습니다. 이것은 내 코드이며 내 코드에 어떤 기능을 추가해야합니까?C#에서 타이핑 테스트하기

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Windows.Input; 
using System.Windows.Forms; 


namespace DesktopApplication 
{ 
public partial class Form1 : Form 
{ 

    string[] randomWords = new string[] { 
      "Adult", 
      "Aeroplane", 
      "Air", 
      "Aircraft", 
      "Carrier", 
      "Airforce", 
      "Airport", 
      "Album", 
      "Alphabet", 
      "Apple", 
      "Army", 
      "Baby", 
      "Baby", 
      "Backpack", 
      "Balloon", 
      "Banana", 
      "Bank", 
      "Barbecue", 
      "Bathroom", 
      "Cappuccino", 
      "Car", 
      "Car", 
      "Carpet", 
      "Carrot", 
      "Cave", 
      "Chair", 
      "Chess", 
      "Drill", 
      "Drink", 
      "Drum", 
      "Dung"}; 




    public Form1() 
    { 
     InitializeComponent(); 


    } 

    private void label1_Click(object sender, EventArgs e) 
    { 

    } 

    private void label1_Click_1(object sender, EventArgs e) 
    { 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     label1.Text = "Welcome to the typing test!"; 

     label2.Text = "Type all the words that appear in the box below"; 

     label3.Text = ""; 

     button1.Text = "Press when ready to begin"; 


    } 

    private void label1_Click_2(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

     label3.Text = randomWords[new Random().Next(0, randomWords.Length)]; 

     } 

    } 
} 
+0

참고 자료를 읽어보십시오. [Keypress 이벤트] (https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress (v = vs.110) .aspx) –

+0

안녕하세요. 먼저 여기에 질문하기 전에 웹 및 커뮤니티를 검색해야하지만 어쨌든 최선을 다해 도와 드리겠습니다. – Emad

+0

@ P.Pat 'KeyPress'에 입력 키와 스페이스 키는 어떻게해야합니까? – Emad

답변

0

이 코드는 기본적으로 에마의 대답 @ 같은 일을 대신 keydown 이벤트 keypress 이벤트를 사용합니다.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (e.KeyChar == Convert.ToChar(Keys.Enter) || e.KeyChar == Convert.ToChar(Keys.Space)) 
      { 
       button1_Click(sender, e); 
      } 
     } 

그래서 매번 당신은 입력하거나 새로운 단어를 생성하거나 다음 단어로 이동 버튼을 클릭하는 것과 같은 것이다 스페이스 바를 누르십시오.

+0

이 코드를 사용하여 얻을 수있는 것은 모두 Windows 사운드이며 필자는 함수에서 print 메서드를 사용하여 해당 인식을 인식하고이를 인식하지 못했습니다. 그것은 단지 텍스트 상자에 간격을 유지합니다. – TetchedWizard7

+0

코드를 붙여 넣고 텍스트 상자에'keypress' 이벤트를 추가하고 답변을 게시하기 전에 작동하는지 테스트합니다. 작동하지 않는다고 생각하면 게시물에 포함되지 않은 코드를 추가하거나 문제를 일으키는 다른 코드를 테스트하고있는 것입니다. –

0

의 당신이 그것을이 같은 이벤트 핸들러와 KeyDown 이벤트의 우선 txtInput 텍스트 상자에 이름을 생각해 봅시다 :

private void txtInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 
{ 
    if(e.KeyCode==Keys.Space) 
    { 
     //Space is pressed. Process it here. 
    } 
}