[C#기초] 9. 키보드, 마우스

INDEX

  1. 키보드
  2. KeyDown이벤트 처리
  3. 마우스

키보드

키 구분

####

키 이벤트

Key Down & Press 예제 코드

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    MessageBox.Show("KeyDown " + e.KeyCode.ToString());
}

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    MessageBox.Show("KeyPress "+e.KeyChar.ToString());
}

KeyDown이벤트 처리

KeyDown 이벤트 처리

KeyEventArtgs 속성

  • 속성 설명
    Alt bool 리턴, Alt키의 눌림 체크
    Control bool 리턴, Control 키의 눌림 체크
    Shift bool 리턴, Shift키의 눌림 체크
    KeyCode KeyDown또는 KeyUp에 대한 Keys 열거값. Control, alt, Shift 키 정보가 없음
    KeyData 동시에 누른 키의 조합 = => ‘|’ 연산자로 조합
    Keys문자키 | Keys.Control | Keys.Shift | Keys.Alt
    Modifiers 누른 Control, Alt, Shift 키 조합
    ’|’연산자로 조합

간단한 코드 예제

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.A && e.Shift && e.Control)
    {
        MessageBox.Show("KeyCode + Shift + Control");
    }
    if(e.KeyCode == Keys.A && e.Modifiers == (Keys.Shift | Keys.Alt))
    {
        MessageBox.Show("A + Shift + Alt");
    }
    if(e.Modifiers == (Keys.Shift | Keys.Control))
    {
        MessageBox.Show("Modifiers + Shift + control");
    }
    if(e.KeyData == (Keys.A | Keys.Shift | Keys.Control))
    {
        MessageBox.Show("A + Shift + Control");
    }
}

마우스

컨트롤 마우스 이벤트

폼 위에서의 마우스 이벤트

마우스 좌표

이벤트 발생 순서

MouserEventArgs속성