본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - FunctionKey matching 단축키 + ini 파일 ( 펑션키 LeftCtrl + LeftShift + LeftAlt + F1 )

728x90
반응형
 WPF & C# - FunctionKey matching 단축키 + ini 파일 ( 펑션키 LeftCtrl + LeftShift + LeftAlt + F1 )

FunctionKey.zip
0.04MB
config.ini
0.00MB
FunctionKey.exe
0.01MB

 

MainWindow.xaml

1
2
3
<StackPanel>
    <TextBox x:Name="tbx" PreviewKeyDown="tbx_PreviewKeyDown"></TextBox>
</StackPanel>
cs

 

MainWindow.xaml.cs

 

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public MainWindow()
{
    InitializeComponent();
}
 
#region >> ini 파일 엑세스
// iniSet("AAA", "B1", "CCC");   // ini 파일에 쓰기
// iniGet("AAA", "B1");   // ini 파일에 읽기
// iniSet("AAA", null, null); 섹션 초기화
 
string iniPath = Environment.CurrentDirectory + @"\config.ini";   // ini 파일명
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 
// ini파일에 쓰기
public void iniSet(string _Section, string _Key, string _Value)
{
    WritePrivateProfileString(_Section, _Key, _Value, iniPath);
}
 
// ini파일 값 가져오기
public string iniGet(string _Section, string _Key)
{
    StringBuilder STBD = new StringBuilder(1000);
    GetPrivateProfileString(_Section, _Key, null, STBD, 5000, iniPath);
    return STBD.ToString();
}
#endregion
 
private void tbx_PreviewKeyDown(object sender, KeyEventArgs e)
{
    #region >> FunctionKey matching
    if (e.Key == Key.Escape) tbx.Text = "ESC";
    else
    {
        tbx.Text = "";
        string downKey = string.Empty;
        string downKeyAll = string.Empty;
 
        // TextBox 에서 안되는 조합 : LeftAlt + F1, LeftShiftLeftAlt + F1  ("LeftAlt" 가 "LeftAltSystem" 으로 나온다.)
        if (Keyboard.IsKeyDown(Key.LeftCtrl)) { downKey += Key.LeftCtrl.ToString(); }
        if (Keyboard.IsKeyDown(Key.LeftShift)) { downKey += Key.LeftShift.ToString(); }
        if (Keyboard.IsKeyDown(Key.LeftAlt)) { downKey += Key.LeftAlt.ToString(); }
 
        downKeyAll = downKey + e.Key;
        tbx.Text = downKeyAll + " + " + iniGet("FunctionKey", downKeyAll);
 
        if (downKeyAll == "F1")    // F1
        {
            // ToDo
        }
        if (downKeyAll == "LeftCtrlF1")    // Ctrl + F1
        {
            // ToDo
        }
        if (downKeyAll == "LeftShiftF1")    // Shift + F1
        {
            // ToDo
        }
        if (downKeyAll == "LeftCtrlLeftShiftF1")    // Ctrl + Shift + F1
        {
            // ToDo
        }
        if (downKeyAll == "LeftCtrlLeftAltF1")    // Ctrl + Alt + F1
        {
            // ToDo
        }
        if (downKeyAll == "LeftCtrlLeftShiftLeftAltF1")    // Ctrl + Shift + Alt + F1
        {
            // ToDo
        }
    }
    #endregion
}
cs

 

config.ini 파일에 단축키를 저장해놓고 쓰면 좋을듯하다.

최대한 간단하게 만들어보다 보니 키보드 키값과 e.key 을 string 형식으로 받아서 만들었는데, 간단하게 쓰기로는 좋은듯하다.

728x90
반응형