2013-07-06 9 views

답변

3

누르고있는 현재 키를 추적 할 수 있습니다. 한 번에 두 개 이상의 키를 처리하는 경우,이

bool bKeyIsDown = false; 
Keys currentKey = Keys.None; 

public event EventHandler OnKeyPressedOnce; 

protected override void OnKeyDown(KeyEventArgs e) 
{ 
    if (bKeyIsDown && currentKey == e.KeyCode) 
     e.SuppressKeyPress = true; 
    else 
    { 
     currentKey = e.KeyCode;  

     //have your class handle this event and play the sound when this fires  
     //Could also create custom EventArgs and pass the key pressed 
     if(OnKeyPressedOnce != null) 
      OnKeyPressedOnce(null, EventArgs.Empty); 

     bKeyIsDown = true; 
    } 

    base.OnKeyDown(e); 
} 


protected override void OnKeyUp(KeyEventArgs e) 
{ 
    bKeyIsDown = false; 
    base.OnKeyUp(e); 
} 

같은 것을, 당신은 키의 목록이 대신 현재 하나 누르는 저장할 수 있습니다.

의견에서 업데이트. 다음은 이벤트를 처리하는 기본 요점입니다.

private void KeyPressedOnce_PlaySound(object sender, EventArgs e) 
{ 
    if (currentKey == Keys.A) 
    { 
     MediaPlayer p1 = new MediaPlayer(); 
     p1.Open(new System.Uri(wav1Path)); 
     p1.Play(); 
    } 


    if (currentKey == Keys.S) 
    { 
     MediaPlayer p2 = new MediaPlayer(); 
     p2.Open(new System.Uri(wav2Path)); 
     p2.Play(); 
    } 
} 
+0

키가 눌려지면 여전히 루프가 발생합니다. – Joscplan

+0

'else {}'블록 내에서 사운드를 재생해야합니다. OnKeyDown/Up 이벤트를 처리하여 일반 텍스트 상자에서이 작업을 수행 할 수 있습니다. 또는 텍스트 상자를 여러 번 사용하는 경우 주 프로그램에서 처리하는 해당 블록에서 사용자 지정 이벤트를 실행할 수 있습니다. – keyboardP

+0

이벤트 핸들러의 예를 보여주기 위해 코드를 업데이트했습니다. – keyboardP