// returns the behavior string associated with a key public string GetBehaviorOfKey(Key key) { // first, check the KeyPress list KeyBehavior keyEvent = PressKeys.Find(x => x.key == key); if (keyEvent.behavior != null) { return(keyEvent.behavior); } // then check the KeyDurEvents keyEvent = DurKeys.Find(x => x.key == key); if (keyEvent.behavior != null) { return(keyEvent.behavior); } // keyEvent is null so throw an exception throw new ArgumentException("Key does not have a behavior"); }
public MainWindow() { InitializeComponent(); // Set the current domain to the application's directory so that Config.xlsx can be found AppDomain.CurrentDomain.SetData("DataDirectory", Directory.GetCurrentDirectory()); // Load resource dictionaries ResourceLoader("Styles.xaml"); _registrar = new Registrar("cfg"); Stopwatch.DataContext = _registrar; Stopwatch.Text = _registrar.Elapsed.ToString(@"mm\:ss\:ff"); // Have the dispatch timer call the update method on stopwatch every millisecond _stopwatchDispatcherTimer.Tick += new EventHandler(StopwatchDispatcherTimer_Tick); _stopwatchDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1); _stopwatchDispatcherTimer.Start(); _flashDispatcherTimer.Tick += new EventHandler(FlashDispatcherTimer_Tick); _flashDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100); // Load the config file using (ExcelPackage xlPackage = new ExcelPackage(new FileInfo("Config.xlsx"))) { var worksheet = xlPackage.Workbook.Worksheets.First(); // Goto the special cell that tells me how many config entries there are int rows = int.Parse(worksheet.Cells[2, 4].Value.ToString()); for (int i = 0; i < rows; i++) { // For each config entry: add the key to the registrar's lists of keys it cares about string keyType = worksheet.Cells[i + 2, 2].Value.ToString(); string keyValue = worksheet.Cells[i + 2, 1].Value.ToString(); string keyBehavior; try { keyBehavior = worksheet.Cells[i + 2, 3].Value.ToString(); } catch (Exception) { keyBehavior = ""; } Key key = (Key)Enum.Parse(typeof(Key), keyValue); KeyBehavior kb = new KeyBehavior() { key = key, behavior = keyBehavior }; if (keyType.Equals("Once")) { _registrar.PressKeys.Add(kb); } else if (keyType.Equals("Duration")) { _registrar.DurKeys.Add(kb); } } } // Programatically create the KeyboardButtons // Uses non ideal parallel arrays to handle the key's display string and the key's enum string[] numRow = new[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=" }; Key[] numRowKeys = new Key[] { Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.D0, Key.OemMinus, Key.OemPlus }; string[] topRow = new[] { "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]" }; Key[] topRowKeys = new Key[] { Key.Q, Key.W, Key.E, Key.R, Key.T, Key.Y, Key.U, Key.I, Key.O, Key.P, Key.OemOpenBrackets, Key.OemCloseBrackets }; string[] midRow = new[] { "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'" }; Key[] midRowKeys = new Key[] { Key.A, Key.S, Key.D, Key.F, Key.G, Key.H, Key.J, Key.K, Key.L, Key.OemSemicolon, Key.OemQuotes }; string[] bottomRow = new[] { "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/" }; Key[] botRowKeys = new Key[] { Key.Z, Key.X, Key.C, Key.V, Key.B, Key.N, Key.M, Key.OemComma, Key.OemPeriod, Key.OemQuestion }; RegisterRow(numRow, numRowKeys, NumRowPanel); RegisterRow(topRow, topRowKeys, TopRowPanel); RegisterRow(midRow, midRowKeys, MidRowPanel); RegisterRow(bottomRow, botRowKeys, BotRowPanel); }