// ALL RIGHTS RESERVED:
// ====================
// The contents of this file, and associated files in this directory, are 
// Copyright (C) SPT , all rights reserved, 2009.
// All software Source Code, Images, Database-Design and code, Graphics Design 
// and source files, and related content (collectively referred to as SOURCE) are 
// Copyright (c) SPT, 2009, All Rights Reserved.
// http://SerialPortTool.com

// PTZ Controller is a Registered Trademark of SerialPortTool.com.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Spt.Share.Data;

namespace Spt.Share.Controls
{
    public partial class ctlHotKey : UserControl
    {
        Keys[] _controlKeys;

        private HotKey _key;

        public HotKey Key
        {
            get { return _key; }
            set { _key = value; }
        }
	

        public ctlHotKey()
        {
            _controlKeys = new Keys[]
            {
                Keys.ShiftKey,
                Keys.ControlKey,
                Keys.Menu,
                Keys.Escape,
                Keys.Back,
                Keys.Delete
            };

            _key = new HotKey();

            InitializeComponent();
        }

        private void _keyEdit_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;

            string text = "";

            if (e.Control)
            {
                Add(ref text, "Control");
            }

            if (e.Alt)
            {
                Add(ref text, "Alt");
            }

            if (e.Shift)
            {
                Add(ref text, "Shift");
            }

            if (!IsControlKey(e.KeyCode))
            {
                Add(ref text, e.KeyCode.ToString());

                _key.AltKey = e.Alt;
                _key.ShiftKey = e.Shift;
                _key.ControlKey = e.Control;
                _key.Code = e.KeyCode;
                _keyEdit.Text = text;
            }
            else
            {
                _key.Code = Keys.None;
                _keyEdit.Text = "";
            }
        }

        private void Add(ref string text, string word)
        {
            if (text.Length > 0)
            {
                text += "+";
            }

            text += word;
        }

        private bool IsControlKey(Keys key)
        {
            bool flag = false;

            foreach (Keys i in _controlKeys)
            {
                if (i == key)
                {
                    flag = true;
                    break;
                }
            }

            return flag;
        }
    }
}
