[C# 윈폼] ModifierKeys 이용하여 누른 보조키 확인하기


안녕하세요.

 

오늘은 C# 윈폼에서 Control, Shift, Alt 키와 같이 누른 보조키를 확인하는 방법에 대해서 알려 드리려고 합니다.

 

오늘 제가 예제를 들려고 하는 것은, Button 컨트롤 하나를 두고 해당 Button 컨트롤을 Control 키와 함께 Click 하였을 때, 해당 Button 컨트롤을 숨기는 기능을 예제로 작성해 보려고 합니다.

 

이렇게 누른 보조키를 사용하는 방법을 알게 되시면, 프로젝트를 진행하시면서 유용하게 사용가능함으로 한번씩 따라해 보시는 것을 추천드리겠습니다.


 

먼저 아래와 같이 빈 윈폼 프로젝트를 생성해 주시고 Button 컨트롤 하나를 배치해 주시기 바랍니다.

 

빈 윈폼 프로젝트 생성 및 Button 컨트롤 배치



예제 코드


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace ModifierKeyTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            this.uiBtn_Main.Click += uiBtn_Main_Click_Event;

        }

 

        /// <summary>

        /// 버튼 클릭 이벤트 핸들러

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void uiBtn_Main_Click_Event(object sender, EventArgs e)

        {

            Button btn = sender as Button;

            if (ModifierKeys == (Keys.Control)) //Keys.Shift, Keys.Alt  가능

            {

                btn.Hide();

            }

        }

    }

}

 

Colored by Color Scripter

cs

 

실행 결과




위와 같이 Control 키를 함께 누른 상태에서 Button 컨트롤을 함께 클릭하면 ButtonHide 되는 것을 확인하실 수 있습니다.


감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY