사용자 입력 처리

이 항목에서는 System.Windows.Forms.Control에서 제공하는 기본 키보드 및 마우스 이벤트에 대해 설명합니다. 이벤트를 처리할 경우 컨트롤 작성자는 이벤트에 대리자를 연결하는 대신 보호된 OnEventName 메서드를 재정의해야 합니다. 이벤트를 검토하려면 구성 요소에서 이벤트 발생시키기를 참조하십시오.

참고

이벤트에 데이터가 연결되어 있지 않으면 기본 클래스 EventArgs의 인스턴스가 OnEventName 메서드에 인수로 전달됩니다.

키보드 이벤트

컨트롤에서 처리할 수 있는 공용 키보드 이벤트는 KeyDown, KeyPressKeyUp입니다.

이벤트 이름

재정의할 메서드

이벤트 설명

KeyDown

void OnKeyDown(KeyEventArgs)

키를 맨 처음 눌렀을 때만 발생합니다.

KeyPress

void OnKeyPress

(KeyPressEventArgs)

키를 누를 때마다 발생합니다. 키를 누르고 있으면 운영 체제에서 정의한 반복 속도로 KeyPress 이벤트가 발생합니다.

KeyUp

void OnKeyUp(KeyEventArgs)

키를 놓으면 발생합니다.

참고

키보드 입력을 처리하는 것은 앞에 있는 표의 이벤트를 재정의하는 것보다 복잡하고 이 항목에서 다룰 수 없습니다. 자세한 내용은 Windows Forms에 사용자 입력을 참조하십시오.

마우스 이벤트

컨트롤에서 처리할 수 있는 마우스 이벤트는 MouseDown, MouseEnter, MouseHover, MouseLeave, MouseMoveMouseUp입니다.

이벤트 이름

재정의할 메서드

이벤트 설명

MouseDown

void OnMouseDown(MouseEventArgs)

포인터가 컨트롤 위에 있는 동안 마우스 단추를 누르면 발생합니다.

MouseEnter

void OnMouseEnter(EventArgs)

포인터가 맨 처음 컨트롤 영역에 들어갈 경우 발생합니다.

MouseHover

void OnMouseHover(EventArgs)

포인터가 컨트롤을 가리킬 경우 발생합니다.

MouseLeave

void OnMouseLeave(EventArgs)

포인터가 컨트롤 영역을 벗어날 경우 발생합니다.

MouseMove

void OnMouseMove(MouseEventArgs)

포인터가 컨트롤 영역에서 이동할 경우 발생합니다.

MouseUp

void OnMouseUp(MouseEventArgs)

포인터가 컨트롤 위에 있거나 컨트롤 영역을 벗어나 있을 때 마우스 단추를 놓으면 발생합니다.

다음 코드 부분에서는 MouseDown 이벤트를 재정의하는 예제를 보여 줍니다.

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
    MyBase.OnMouseDown(e)
    If Not (myAllowUserEdit) Then
        Return
    End If
    Capture = True
    dragging = True
    SetDragValue(New Point(e.X, e.Y))
End Sub
protected override void OnMouseDown(MouseEventArgs e) {
    base.OnMouseDown(e);
    if (!allowUserEdit) {
        return;
    }
    Capture = true;
    dragging = true;
    SetDragValue(new Point(e.X, e.Y));
}

다음 코드 부분에서는 MouseMove 이벤트를 재정의하는 예제를 보여 줍니다.

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
    MyBase.OnMouseMove(e)
    If (Not myAllowUserEdit Or Not dragging) Then
        Return
    End If
    SetDragValue(New Point(e.X, e.Y))
End Sub
protected override void OnMouseMove(MouseEventArgs e) {
    base.OnMouseMove(e);
    if (!allowUserEdit || !dragging) {
        return;
    }
    SetDragValue(new Point(e.X, e.Y));
}

다음 코드 부분에서는 MouseUp 이벤트를 재정의하는 예제를 보여 줍니다.

Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
    MyBase.OnMouseUp(e)
    If (Not myAllowUserEdit Or Not dragging) Then
        Return
    End If
    Capture = False
    dragging = False
    Value = dragValue
    OnValueChanged(EventArgs.Empty)
End Sub
protected override void OnMouseUp(MouseEventArgs e) {
    base.OnMouseUp(e);
    if (!allowUserEdit || !dragging) {
        return;
    }
    Capture = false;
    dragging = false;
    value = dragValue;
    OnValueChanged(EventArgs.Empty);
}

FlashTrackBar 샘플의 전체 소스 코드를 보려면 방법: 진행률을 보여 주는 Windows Forms 컨트롤 만들기를 참조하십시오.

참고 항목

개념

Windows Forms 컨트롤의 이벤트

Windows Forms 컨트롤에서 이벤트 정의

기타 리소스

이벤트 처리 및 발생

Windows Forms에 사용자 입력