none
wpf에서 키보드 입력 구현하는 것 관련 질문입니다.

    질문

  • 안녕하세요.

    wpf를 이용해서 간단한 계산기 앱을 개발중입니다. 키보드와 마우스로 둘 다 입력가능하게 하려고 하는데 키보드 입력에서 막히네요.

    여러분들의 지적 및 조언을 기다립니다.

    UI 디자인은 이렇습니다:

    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            Title="MainWindow" Height="525" Width="350">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Label x:Name="resultLabel"
                   Content="0"
                   HorizontalAlignment="Right"
                   VerticalAlignment="Bottom"
                   Grid.ColumnSpan="4"/>
            <Button x:Name="acButton"
                    Style="{StaticResource additionalButtonsStyle}"
                    Content="AC"
                    Grid.Row="1"/>
            <Button x:Name="negativeButton"
                    Style="{StaticResource additionalButtonsStyle}"
                    Content="+/-"
                    Grid.Row="1"
                    Grid.Column="1"/>
            <Button x:Name="percentageButton"
                    Style="{StaticResource additionalButtonsStyle}"
                    Click="PercentageButton_Click"
                    Content="%"
                    Grid.Row="1"
                    Grid.Column="2"/>
            <Button x:Name="divisionButton"
                    Click="OperationButton_Click"
                    Style="{StaticResource operatorButtonsStyle}"
                    Content="/"
                    Grid.Row="1"
                    Grid.Column="3"/>
            <Button x:Name="sevenButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="7"
                    Grid.Row="2"/>
            <Button x:Name="eightButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="8"
                    Grid.Row="2"
                    Grid.Column="1"/>
            <Button x:Name="nineButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="9"
                    Grid.Row="2"
                    Grid.Column="2"/>
            <Button x:Name="multiplicationButton"
                    Click="OperationButton_Click"
                    Style="{StaticResource operatorButtonsStyle}"
                    Content="*"
                    Grid.Row="2"
                    Grid.Column="3"/>
            <Button x:Name="fourButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="4"
                    Grid.Row="3"/>
            <Button x:Name="fiveButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="5"
                    Grid.Row="3"
                    Grid.Column="1"/>
            <Button x:Name="sixButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="6"
                    Grid.Row="3"
                    Grid.Column="2"/>
            <Button x:Name="minusButton"
                    Click="OperationButton_Click"
                    Style="{StaticResource operatorButtonsStyle}"
                    Content="-"
                    Grid.Row="3"
                    Grid.Column="3"/>
            <Button x:Name="oneButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="1"
                    Grid.Row="4"/>
            <Button x:Name="twoButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="2"
                    Grid.Row="4"
                    Grid.Column="1"/>
            <Button x:Name="threeButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="3"
                    Grid.Row="4"
                    Grid.Column="2"/>
            <Button x:Name="plusButton"
                    Click="OperationButton_Click"
                    Style="{StaticResource operatorButtonsStyle}"
                    Content="+"
                    Grid.Row="4"
                    Grid.Column="3"/>
            <Button x:Name="zeroButton"
                    Click="NumberButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="0"
                    Grid.Row="5"
                    Grid.ColumnSpan="2"/>
            <Button x:Name="pointButton"
                    Click="pointButton_Click"
                    Style="{StaticResource numberButtonsStyle}"
                    Content="."
                    Grid.Row="5"
                    Grid.Column="2"/>
            <Button x:Name="equalButton"
                    Style="{StaticResource operatorButtonsStyle}"
                    Content="="
                    Grid.Row="5"
                    Grid.Column="3"/>
        </Grid>
    </Window>

    메인페이지 용 코드입니다.

    using System.Windows;
    using System.Windows.Input;
    
    namespace WpfApp1
    {
        /// <summary>
        /// MainWindow.xaml에 대한 상호 작용 논리
        /// </summary>
        public partial class MainWindow : Window
        {
            double lastNumber, result;
            SelectedOperator selectedOperator;
            public MainWindow()
            {
                InitializeComponent();
    
                acButton.Click += AcButton_Click;
                acButton.KeyDown += AcButton_KeyDown;
                negativeButton.Click += NegativeButton_Click;
                percentageButton.Click += PercentageButton_Click;
                equalButton.Click += EqualButton_Click;
            }
    
           
    
            private void EqualButton_Click(object sender, RoutedEventArgs e)
            {
                double newNumber;
                if (double.TryParse(resultLabel.Content.ToString(), out newNumber))
                {
                    switch (selectedOperator)
                    {
                        case SelectedOperator.Addition:
                            result = SimpleMath.Add(lastNumber, newNumber);
                            break;
                        case SelectedOperator.Sustraction:
                            result = SimpleMath.Sustraction(lastNumber, newNumber);
                            break;
                        case SelectedOperator.Multiplication:
                            result = SimpleMath.Multiply(lastNumber, newNumber);
                            break;
                        case SelectedOperator.Division:
                            result = SimpleMath.Divide(lastNumber, newNumber);
                            break;
                    }
    
                    resultLabel.Content = result.ToString("n0");
                }
            }
    
            private void PercentageButton_Click(object sender, RoutedEventArgs e)
            {
                if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
                {
                    lastNumber = lastNumber / 100;
                    resultLabel.Content = lastNumber.ToString();
                }
            }
    
            private void NegativeButton_Click(object sender, RoutedEventArgs e)
            {
                if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
                {
                    lastNumber = lastNumber * -1;
                    resultLabel.Content = lastNumber.ToString();
                }
            }
    
            private void AcButton_Click(object sender, RoutedEventArgs e)
            {
                resultLabel.Content = "0";
            }
    
            private void AcButton_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.C)
                    resultLabel.Content = "0";
            }
    
            private void OperationButton_Click(object sender, RoutedEventArgs e)
            {
                if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
                {
                    resultLabel.Content = "0";
                }
    
                if (sender == multiplicationButton)
                    selectedOperator = SelectedOperator.Multiplication;
                if (sender == divisionButton)
                    selectedOperator = SelectedOperator.Division;
                if (sender == plusButton)
                    selectedOperator = SelectedOperator.Addition;
                if (sender == minusButton)
                    selectedOperator = SelectedOperator.Sustraction;
            }
    
            private void pointButton_Click(object sender, RoutedEventArgs e)
            {
                if (resultLabel.Content.ToString().Contains("."))
                {
                    // Do nothing
                }
                else
                {
                    resultLabel.Content = $"{resultLabel.Content}.";
                }
            }
    
            private void NumberButton_Click(object sender, RoutedEventArgs e)
            {
                int selectedValue = 0;
    
                if (sender == zeroButton)
                    selectedValue = 0;
                if (sender == oneButton)
                    selectedValue = 1;
                if (sender == twoButton)
                    selectedValue = 2;
                if (sender == threeButton)
                    selectedValue = 3;
                if (sender == fourButton)
                    selectedValue = 4;
                if (sender == fiveButton)
                    selectedValue = 5;
                if (sender == sixButton)
                    selectedValue = 6;
                if (sender == sevenButton)
                    selectedValue = 7;
                if (sender == eightButton)
                    selectedValue = 8;
                if (sender == nineButton)
                    selectedValue = 9;
    
                if (resultLabel.Content.ToString() == "0")
                {
                    resultLabel.Content = $"{selectedValue}";
                }
                else
                {
                    resultLabel.Content = $"{resultLabel.Content}{selectedValue}";
                }
            }
        }
    
        public enum SelectedOperator
        {
            Addition,
            Sustraction,
            Multiplication,
            Division
        }
    
        public class SimpleMath
        {
            public static double Add(double n1, double n2)
            {
                return n1 + n2;
            }
    
            public static double Sustraction(double n1, double n2)
            {
                return n1 - n2;
            }
    
            public static double Multiply(double n1, double n2)
            {
                return n1 * n2;
            }
    
            public static double Divide(double n1, double n2)
            {
                if (n2 == 0)
                {
                    MessageBox.Show("Division by 0 is not supported", "Wrong operation", MessageBoxButton.OK, MessageBoxImage.Error);
                    return 0;
                }
    
                return n1 / n2;
            }
        }
    }

    키보드 입력을 구현하려고 일단 ac 버튼에만 keydown 이벤트를 건 다음 c 키를 누르면 작동하게 했는데 막히네요...

    다시 한 번 여러분들의 지적 및 조언을 바랍니다.

    2019년 5월 14일 화요일 오후 1:18

답변

모든 응답