Return to Snippet

Revision: 33950
at October 15, 2010 03:16 by dzr_gregory


Initial Code
using System.Windows.Forms;

namespace DistrictEditor.DataClasses
{
    
    /// <summary>
    /// Класс состояний клавиатуры
    /// </summary>
    public class Keyboard
    {
        /// <summary>
        /// Нажата ли клавиша Ctrl
        /// </summary>
        public bool IsCtrlPressed { get; private set; }

        /// <summary>
        /// Нажата ли клавиша Alt
        /// </summary>
        public bool IsAltPressed { get; private set; }

        /// <summary>
        /// Конструктор. Принимает экземпляр  формы,
        /// на которой нам необходимо мониторить нажатия
        /// </summary>
        /// <param name="form">Экземпляр формы</param>
        public Keyboard(Form form)
        {
            if (form != null)
            {
                form.KeyDown += FormKeyDown;
                form.KeyUp += FormKeyUp;
            }
        }

        /// <summary>
        /// Вызывается при отпускании клавиши
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormKeyUp(object sender, KeyEventArgs e)
        {
            IsCtrlPressed = IsCtrlPressed ? false : IsCtrlPressed;
            IsAltPressed = IsAltPressed ? false: IsAltPressed;
        }

        /// <summary>
        /// Вызывается при нажатии клавиши
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormKeyDown(object sender, KeyEventArgs e)
        {
            IsCtrlPressed = e.Control;
            IsAltPressed = e.Alt;
        }
    
    }

}

Initial URL

                                

Initial Description

                                

Initial Title
Control keyboard key states anytime you want (Windows Forms)

Initial Tags
c

Initial Language
C#