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