사용자 입력 처리

이 토픽은 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 override void OnMouseDown(MouseEventArgs e) {
    base.OnMouseDown(e);
    if (!allowUserEdit) {
        return;
    }
    Capture = true;
    dragging = true;
    SetDragValue(new Point(e.X, e.Y));
}
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

다음 코드 조각은 MouseMove 이벤트를 재정의하는 예제를 보여 줍니다.

protected override void OnMouseMove(MouseEventArgs e) {
    base.OnMouseMove(e);
    if (!allowUserEdit || !dragging) {
        return;
    }
    SetDragValue(new Point(e.X, e.Y));
}
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

다음 코드 조각은 MouseUp 이벤트를 재정의하는 예제를 보여 줍니다.

protected override void OnMouseUp(MouseEventArgs e) {
    base.OnMouseUp(e);
    if (!allowUserEdit || !dragging) {
        return;
    }
    Capture = false;
    dragging = false;
    value = dragValue;
    OnValueChanged(EventArgs.Empty);
}
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

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

참고 항목