■ 가상 키보드 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


VirtualKeyboardHelper.cs

 

 

using System.Diagnostics;

using System.Linq;

using System.Windows;

using System.Windows.Input;

using System.Windows.Controls;

 

namespace TestProject

{

    /// <summary>

    /// 가상 키보드 헬퍼

    /// </summary>

    public static class VirtualKeyboardHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region Field

 

        /// <summary>

        /// 가상 키보드 지원 여부 속성

        /// </summary>

        public static readonly DependencyProperty IsSurpportVirtualKeyboardProperty;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 파일명

        /// </summary>

        private static string _fileName = "osk.exe";

 

        /// <summary>

        /// 프로세스

        /// </summary>

        private static Process _process = null;

 

        /// <summary>

        /// 라우티드 이벤트 핸들러

        /// </summary>

        private static RoutedEventHandler _routedEventHandler = new RoutedEventHandler(targetFrameworkElement_Focus);

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Static

 

        #region 생성자 - VirtualKeyboardHelper()

 

        /// <summary>

        /// 생성자

        /// </summary>

        static VirtualKeyboardHelper()

        {

            IsSurpportVirtualKeyboardProperty = DependencyProperty.RegisterAttached

            (

                "IsSurpportVirtualKeyboard",

                typeof(bool?),

                typeof(VirtualKeyboardHelper),

                new FrameworkPropertyMetadata

                (

                    null,

                    new PropertyChangedCallback(IsSurpportScreenKeyboard_PropertyChanged)

                )

            );

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 가상 키보드 지원 여부 구하기 - GetIsSurpportVirtualKeyboard(targetFrameworkElement)

 

        /// <summary>

        /// 가상 키보드 지원 여부 구하기

        /// </summary>

        /// <param name="targetFrameworkElement">타겟 프레임워크 엘리먼트</param>

        /// <returns>가상 키보드 지원 여부</returns>

        public static bool GetIsSurpportVirtualKeyboard(FrameworkElement targetFrameworkElement)

        {

            return (bool)targetFrameworkElement.GetValue(IsSurpportVirtualKeyboardProperty);

        }

 

        #endregion

        #region 가상 키보드 지원 여부 설정하기 - SetIsSurpportVirtualKeyboard(targetFrameworkElement, isSurpportVirtualKeyboard)

 

        /// <summary>

        /// 가상 키보드 지원 여부 설정하기

        /// </summary>

        /// <param name="targetFrameworkElement">타겟</param>

        /// <param name="isSurpportVirtualKeyboard">가상 키보드 지원 여부</param>

        public static void SetIsSurpportVirtualKeyboard(FrameworkElement targetFrameworkElement, bool? isSurpportVirtualKeyboard)

        {

            targetFrameworkElement.SetValue(IsSurpportVirtualKeyboardProperty, isSurpportVirtualKeyboard);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 가상 키보드 지원 여부 속성 변경시 처리하기 - IsSurpportScreenKeyboard_PropertyChanged(dependencyObject, e)

 

        /// <summary>

        /// 가상 키보드 지원 여부 속성 변경시 처리하기

        /// </summary>

        /// <param name="dependencyObject">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private static void IsSurpportScreenKeyboard_PropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)

        {

            FrameworkElement targetFrameworkElement = dependencyObject as FrameworkElement;

 

            if((bool)e.NewValue)

            {

                targetFrameworkElement.GotFocus  += _routedEventHandler;

                targetFrameworkElement.LostFocus += _routedEventHandler;

            }

            else if(_routedEventHandler.GetInvocationList().First().Target == targetFrameworkElement)

            {

                targetFrameworkElement.GotFocus  -= _routedEventHandler;

                targetFrameworkElement.LostFocus -= _routedEventHandler;    

            }

        }

 

        #endregion

        #region 타겟 프레임워크 엘리먼트 포커스시 처리하기 - targetFrameworkElement_Focus(sender, e)

 

        /// <summary>

        /// 타겟 프레임워크 엘리먼트 포커스시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private static void targetFrameworkElement_Focus(object sender, RoutedEventArgs e)

        {

            FrameworkElement targetFrameworkElement = sender   as FrameworkElement;

            FrameworkElement sourceFrameworkElement = e.Source as FrameworkElement;

 

            if((bool?)targetFrameworkElement.GetValue(IsSurpportVirtualKeyboardProperty) == false)

            {

                targetFrameworkElement.GotFocus -= _routedEventHandler;

                targetFrameworkElement.GotFocus -= _routedEventHandler;

 

                return;

            }

            else if((bool?)sourceFrameworkElement.GetValue(IsSurpportVirtualKeyboardProperty) != false &&

                e.RoutedEvent == FocusManager.GotFocusEvent && (e.Source is TextBox || e.Source is PasswordBox))

            {

                _process = Process.Start(_fileName);

            }

            else if(e.RoutedEvent == FocusManager.LostFocusEvent && _process != null)

            {

                if(_process.HasExited == false)

                {

                    _process.CloseMainWindow();

                }

 

                _process = null;

            }

        }  

 

        #endregion

    }

}

 

 

MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:TestProject"

    Width="800"

    Height="600"

    Title="가상 키보드 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="50*" />

            <ColumnDefinition Width="50*" />

        </Grid.ColumnDefinitions>

        <Border Grid.Column="0"

            Margin="3">

            <GroupBox Header="가상 키보드 사용">

                <StackPanel

                    Margin="10 20 10 10"

                    local:VirtualKeyboardHelper.IsSurpportVirtualKeyboard="True">

                    <TextBlock

                        Margin="0 0 0 10"

                        Text="사용자 ID :" />

                    <TextBox

                        Margin="0 0 0 10"

                        Height="25" />

                    <TextBlock

                        Margin="0 0 0 10"

                        Text="비밀 번호 :" />

                    <PasswordBox

                        Margin="0 0 0 10"

                        Height="25" />

                </StackPanel>

            </GroupBox>

        </Border>

        <Border Grid.Column="1" Margin="3">

            <GroupBox Header="가상 키보드 미사용">

                <StackPanel

                    Margin="10 20 10 10"

                    local:VirtualKeyboardHelper.IsSurpportVirtualKeyboard="False">

                    <TextBlock

                        Margin="0 0 0 10"

                        Text="사용자 ID :" />

                    <TextBox

                        Margin="0 0 0 10"

                        Height="25" />

                    <TextBlock

                        Margin="0 0 0 10"

                        Text="비밀 번호 :" />

                    <PasswordBox

                        Margin="0 0 0 10"

                        Height="25" />

                </StackPanel>

            </GroupBox>

        </Border>

    </Grid>

</Window>

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요