2011년 11월 28일 월요일

KeyEventArgs 의 Control 키와 Shift 키에 대하여.


WinForm의 키 이벤트 정보 인자로 KeyEventArgs가 있다.
이 이벤트 정보를 이용하는 이벤트는 다음 두 가지가 있다.
- KeyDown
- KeyUp

리스트뷰와 같은 컨트롤에서 여러개의 노드를 선택하기 위해서는 다음 방법을 사용한다.
1. Shift 키 + 방향 버튼
2. Control 키 + 방향 버튼

따라서 Shift 키 또는 Control 키가 눌려있는지 판별해야 할 필요가 있다.

해당 컨트롤의 KeyDown 이벤트 핸들러에서 키가 눌렀을 때 플래그값을 설정한다.


if (e.Control || e.Shift)

    key = true;




해당 컨트롤의 KeyUp 이벤트 핸들러에서 키가 눌렀다 띄었을 때 플래그값을 설정한다.


if ((e.KeyData == (Keys.LButton | Keys.ShiftKey)) || e.KeyData == Keys.ShiftKey)

     key = false;





의문스러운 것은 KeyUp 이벤트 핸들러에서 e.Control 과 e.Shift 값으로는 해당 이벤트 값을 잡을 수 없었다.
Control 키를 띄었을 때 e.KeyData == (Keys.LButton | Keys.ShiftKey) 

Shift 키를 띄었을 때e.KeyData == Keys.ShiftKey
아.. 왜 그런걸까??

우선 위와 같이 처리!


*) Conrol + a 키 처리
KeyDown 이벤트 핸들러에서


if (e.Control)

{
     if (e.KeyCode.ToString() == "A" || e.KeyCode.ToString() == "a")
     { ... }




*) Keboard Events - http://visualcsharptutorials.com

댓글 1개:

  1. This school management system that allows its users to maintain a record of important information like enrollments, hiring procedures, and other necessary documents related to the management of test score records for the first-class students based on Python with Django framework.
    .School Management system project in django with source code

    답글삭제