view KeyBinding.cs @ 0:aca8706f4eec default tip

Initial commit
author Brad Greco <brad@bgreco.net>
date Mon, 13 Oct 2014 21:28:19 -0500
parents
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

/*
 * Copyright (c) 2014 Brad Greco <brad@bgreco.net>
 */
namespace QuickQR
{
    public class KeyBinding
    {
        const int MOD_ALT = 0x0001;
        const int MOD_CONTROL = 0x0002;
        const int MOD_SHIFT = 0x0004;
        const int MOD_WIN = 0x0008;

        public int key = 0;
        public String keyChar;
        public bool Ctrl = false;
        public bool Alt = false;
        public bool Shift = false;
        public bool Win = false;

        public KeyBinding()
        {

        }

        public KeyBinding(String line)
        {
            strToInts(line);
        }

        public String toString()
        {
            if (key == 0)
                return "(none)";
            return comboString();
        }

        public String comboString()
        {
            if (key == 0)
                return "";
            String myCombo = "";
            if (Ctrl)
                myCombo = "+Ctrl";
            if (Alt)
                myCombo += "+Alt";
            if (Shift)
                myCombo += "+Shift";
            if (Win)
                myCombo += "+Win";
            if (myCombo.Length == 0)
                return "" + keyChar;
            return myCombo.Substring(1) + "+" + keyChar;
        }

        private void strToInts(String s)
        {
            String[] words = s.Split('+');
            foreach (String word in words)
            {
                String w = word.ToLower();
                if (w == "ctrl")
                    Ctrl = true;
                else if (w == "alt")
                    Alt = true;
                else if (w == "shift")
                    Shift = true;
                else if (w == "win")
                    Win = true;
                else
                {
                    keyChar = word;
                    try
                    {
                        key = (int)Enum.Parse(typeof(Keys), word, true);
                        System.Diagnostics.Debug.WriteLine(keyChar + " " + key);
                    }
                    catch (ArgumentException)
                    {
                        key = 0;
                        System.Diagnostics.Debug.WriteLine("Could not convert string to Key: " + word);
                    }
                }
            }
        }

        public int sum()
        {
            int sum = 0;
            if (Ctrl)
                sum += MOD_CONTROL;
            if (Alt)
                sum += MOD_ALT;
            if (Win)
                sum += MOD_WIN;
            if (Shift)
                sum += MOD_SHIFT;
            return sum;
        }

        public bool isValid()
        {
            return key > 0;
        }

        public int getID()
        {
            return 256 * sum() + key;
        }
    }
}