■ UIElement 클래스를 사용해 키 이벤트를 추적하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="UIElement 클래스 : 키 이벤트 추적하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Name="headerTextBlock" Grid.Row="0" FontWeight="Bold" /> <ScrollViewer Name="scrollViewer" Grid.Row="1"> <StackPanel Name="outputStackPanel" /> </ScrollViewer> </Grid> </Window> |
▶ 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 헤더 /// </summary> private string header = "Event Key Sys-Key Text Ctrl-Text Sys-Text Ime KeyStates" + " IsDown IsUp IsToggled IsRepeat"; /// <summary> /// 키 포맷 문자열 /// </summary> private string keyFormatString = "{0,-10} {1,-10} {2,-10} " + "{3,-10} {4,-15} {5,-10} {6,-10} {7,-10} {8,-10}"; /// <summary> /// 텍스트 포맷 문자열 /// </summary> private string textFormatString = "{0,-10} {1,-10} {2,-10} {3,-10}"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.headerTextBlock.Text = header; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 키 DOWN 처리하기 - OnKeyDown(e) /// <summary> /// 키 DOWN 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); string message = string.Format ( this.keyFormatString, e.RoutedEvent.Name , e.Key , e.SystemKey , e.ImeProcessedKey , e.KeyStates , e.IsDown , e.IsUp , e.IsToggled , e.IsRepeat ); DisplayInformation(message); } #endregion #region 키 UP 처리하기 - OnKeyUp(e) /// <summary> /// 키 UP 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnKeyUp(KeyEventArgs e) { base.OnKeyUp(e); string message = string.Format ( this.keyFormatString, e.RoutedEvent.Name , e.Key , e.SystemKey , e.ImeProcessedKey , e.KeyStates , e.IsDown , e.IsUp , e.IsToggled , e.IsRepeat ); DisplayInformation(message); } #endregion #region 텍스트 입력시 처리하기 - OnTextInput(e) /// <summary> /// 텍스트 입력시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnTextInput(TextCompositionEventArgs e) { base.OnTextInput(e); string message = string.Format ( this.textFormatString, e.RoutedEvent.Name , e.Text , e.ControlText , e.SystemText ); DisplayInformation(message); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 정보 표시하기 - DisplayInformation(message) /// <summary> /// 정보 표시하기 /// </summary> /// <param name="message">메시지</param> private void DisplayInformation(string message) { TextBlock textBlock = new TextBlock(); textBlock.Text = message; this.outputStackPanel.Children.Add(textBlock); this.scrollViewer.ScrollToBottom(); } #endregion } } |