comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:aca8706f4eec
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Windows.Forms;
5
6 /*
7 * Copyright (c) 2014 Brad Greco <brad@bgreco.net>
8 */
9 namespace QuickQR
10 {
11 public class KeyBinding
12 {
13 const int MOD_ALT = 0x0001;
14 const int MOD_CONTROL = 0x0002;
15 const int MOD_SHIFT = 0x0004;
16 const int MOD_WIN = 0x0008;
17
18 public int key = 0;
19 public String keyChar;
20 public bool Ctrl = false;
21 public bool Alt = false;
22 public bool Shift = false;
23 public bool Win = false;
24
25 public KeyBinding()
26 {
27
28 }
29
30 public KeyBinding(String line)
31 {
32 strToInts(line);
33 }
34
35 public String toString()
36 {
37 if (key == 0)
38 return "(none)";
39 return comboString();
40 }
41
42 public String comboString()
43 {
44 if (key == 0)
45 return "";
46 String myCombo = "";
47 if (Ctrl)
48 myCombo = "+Ctrl";
49 if (Alt)
50 myCombo += "+Alt";
51 if (Shift)
52 myCombo += "+Shift";
53 if (Win)
54 myCombo += "+Win";
55 if (myCombo.Length == 0)
56 return "" + keyChar;
57 return myCombo.Substring(1) + "+" + keyChar;
58 }
59
60 private void strToInts(String s)
61 {
62 String[] words = s.Split('+');
63 foreach (String word in words)
64 {
65 String w = word.ToLower();
66 if (w == "ctrl")
67 Ctrl = true;
68 else if (w == "alt")
69 Alt = true;
70 else if (w == "shift")
71 Shift = true;
72 else if (w == "win")
73 Win = true;
74 else
75 {
76 keyChar = word;
77 try
78 {
79 key = (int)Enum.Parse(typeof(Keys), word, true);
80 System.Diagnostics.Debug.WriteLine(keyChar + " " + key);
81 }
82 catch (ArgumentException)
83 {
84 key = 0;
85 System.Diagnostics.Debug.WriteLine("Could not convert string to Key: " + word);
86 }
87 }
88 }
89 }
90
91 public int sum()
92 {
93 int sum = 0;
94 if (Ctrl)
95 sum += MOD_CONTROL;
96 if (Alt)
97 sum += MOD_ALT;
98 if (Win)
99 sum += MOD_WIN;
100 if (Shift)
101 sum += MOD_SHIFT;
102 return sum;
103 }
104
105 public bool isValid()
106 {
107 return key > 0;
108 }
109
110 public int getID()
111 {
112 return 256 * sum() + key;
113 }
114 }
115 }