|
1 | 1 | //////////////////////////////////////////////////////////////////////////////// |
2 | 2 | // The MIT License (MIT) |
3 | 3 | // |
4 | | -// Copyright (c) 2021 Tim Stair |
| 4 | +// Copyright (c) 2022 Tim Stair |
5 | 5 | // |
6 | 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | 7 | // of this software and associated documentation files (the "Software"), to deal |
@@ -137,6 +137,46 @@ private void exitMainToolStripMenuItem_Click(object sender, EventArgs e) |
137 | 137 | Close(); |
138 | 138 | } |
139 | 139 |
|
| 140 | + private void btnAddOutputString_Click(object sender, EventArgs e) |
| 141 | + { |
| 142 | + const string MACRO_STRING_KEY = "macro_string_key"; |
| 143 | + var zQuery = new QueryPanelDialog("Enter String Macro", 400, false); |
| 144 | + zQuery.SetIcon(this.Icon); |
| 145 | + zQuery.AddTextBox("String", string.Empty, false, MACRO_STRING_KEY); |
| 146 | + if (DialogResult.OK != zQuery.ShowDialog(this)) |
| 147 | + { |
| 148 | + return; |
| 149 | + } |
| 150 | + var sMacro = zQuery.GetString(MACRO_STRING_KEY); |
| 151 | + if (string.IsNullOrWhiteSpace(sMacro)) |
| 152 | + { |
| 153 | + MessageBox.Show(this, "Please specify a string of output characters.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 154 | + return; |
| 155 | + } |
| 156 | + var zCurrentInputConfig = (InputConfig)txtKeyIn.Tag; |
| 157 | + if (null == zCurrentInputConfig) |
| 158 | + { |
| 159 | + ShowKeysNotDefinedError(); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + try |
| 164 | + { |
| 165 | + var zRemapEntry = new RemapEntry(zCurrentInputConfig, CreateOutputConfigFromCharacter(sMacro[0])); |
| 166 | + for (var nIdx = 1; nIdx < sMacro.Length; nIdx++) |
| 167 | + { |
| 168 | + zRemapEntry.AppendOutputConfig(CreateOutputConfigFromCharacter(sMacro[nIdx])); |
| 169 | + } |
| 170 | + if (!IsInputAlreadyDefined(zRemapEntry)) |
| 171 | + { |
| 172 | + AddRemapEntryToListView(zRemapEntry, true); |
| 173 | + } |
| 174 | + } |
| 175 | + catch (Exception ex) |
| 176 | + { |
| 177 | + MessageBox.Show(this, "Unfortunately you have specified an unsupported character (at this time)." + ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 178 | + } |
| 179 | + } |
140 | 180 |
|
141 | 181 | private void KeyCaptureConfig_FormClosing(object sender, FormClosingEventArgs e) |
142 | 182 | { |
@@ -212,7 +252,7 @@ private void txtKey_Leave(object sender, EventArgs e) |
212 | 252 |
|
213 | 253 | #endregion |
214 | 254 |
|
215 | | - #region AbstractDirtyForm overrides |
| 255 | + #region AbstractDirtyForm overrides |
216 | 256 |
|
217 | 257 | protected override bool SaveFormData(string sFileName) |
218 | 258 | { |
@@ -257,7 +297,7 @@ protected override bool OpenFormData(string sFileName) |
257 | 297 |
|
258 | 298 | #endregion |
259 | 299 |
|
260 | | - #region Menu Events |
| 300 | + #region Menu Events |
261 | 301 |
|
262 | 302 | private void loadToolStripMenuItem_Click(object sender, EventArgs e) |
263 | 303 | { |
@@ -298,7 +338,7 @@ private void recentConfiguration_Click(object sender, EventArgs e) |
298 | 338 |
|
299 | 339 | #endregion |
300 | 340 |
|
301 | | - #region Control Events |
| 341 | + #region Control Events |
302 | 342 |
|
303 | 343 | private void listViewKeys_SelectedIndexChanged(object sender, EventArgs e) |
304 | 344 | { |
@@ -366,15 +406,27 @@ private void btnAdd_Click(object sender, EventArgs e) |
366 | 406 | OutputConfig zOutputConfig = null; |
367 | 407 | RemapEntry zRemapEntry = null; |
368 | 408 | if (!RetrieveAddConfigs(ref zInputConfig, ref zOutputConfig, ref zRemapEntry)) return; |
| 409 | + AddRemapEntryToListView(zRemapEntry, true); |
| 410 | + } |
369 | 411 |
|
370 | | - var zItem = new ListViewItem(new string[] { |
371 | | - zRemapEntry.GetInputString(), |
372 | | - zRemapEntry.GetOutputString() }); |
373 | | - zItem.Tag = zRemapEntry; |
| 412 | + private void AddRemapEntryToListView(RemapEntry zRemapEntry, bool bMarkDirty) |
| 413 | + { |
| 414 | + var zItem = new ListViewItem(new string[] |
| 415 | + { |
| 416 | + zRemapEntry.GetInputString(), |
| 417 | + zRemapEntry.GetOutputString() |
| 418 | + }) |
| 419 | + { |
| 420 | + Tag = zRemapEntry, |
| 421 | + ToolTipText = zRemapEntry.GetOutputString() |
| 422 | + }; |
374 | 423 | listViewKeys.Items.Add(zItem); |
375 | 424 | listViewKeys.SelectedItems.Clear(); |
376 | 425 | zItem.Selected = true; |
377 | | - MarkDirty(); |
| 426 | + if (bMarkDirty) |
| 427 | + { |
| 428 | + MarkDirty(); |
| 429 | + } |
378 | 430 | } |
379 | 431 |
|
380 | 432 | private void btnAppend_Click(object sender, EventArgs e) |
@@ -652,20 +704,26 @@ private bool RetrieveAddConfigs(ref InputConfig zInputConfig, ref OutputConfig z |
652 | 704 |
|
653 | 705 | zRemapEntry = new RemapEntry(zInputConfig, zOutputConfig); |
654 | 706 |
|
| 707 | + // flip this result for indicator of a good remap entry |
| 708 | + return !IsInputAlreadyDefined(zRemapEntry); |
| 709 | + } |
| 710 | + |
| 711 | + private bool IsInputAlreadyDefined(RemapEntry zNewRemapEntry) |
| 712 | + { |
655 | 713 | // verify this is not already defined |
656 | 714 | foreach (ListViewItem zListItem in listViewKeys.Items) |
657 | 715 | { |
658 | 716 | var zKeyOldDef = (RemapEntry)zListItem.Tag; |
659 | | - if (zRemapEntry.GetHashCode() != zKeyOldDef.GetHashCode()) |
| 717 | + if (zNewRemapEntry.GetHashCode() != zKeyOldDef.GetHashCode()) |
660 | 718 | { |
661 | 719 | continue; |
662 | 720 | } |
663 | 721 |
|
664 | 722 | MessageBox.Show(this, "Duplicate inputs are not allowed!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); |
665 | | - return false; |
| 723 | + return true; |
666 | 724 | } |
667 | 725 |
|
668 | | - return true; |
| 726 | + return false; |
669 | 727 | } |
670 | 728 |
|
671 | 729 | private bool RetrieveAppendConfigs(ref OutputConfig zOutputConfig, RemapEntry zRemapEntry) |
@@ -706,6 +764,100 @@ private bool ValidateOutputHasAction(OutputConfig zOutputConfig) |
706 | 764 | return true; |
707 | 765 | } |
708 | 766 |
|
| 767 | + private OutputConfig CreateOutputConfigFromCharacter(char cInput) |
| 768 | + { |
| 769 | + var bShift = false; |
| 770 | + var byteKey = GetKeysByte(cInput, ref bShift); |
| 771 | + var nFlags = (bShift ? (int)OutputConfig.OutputFlag.Shift : 0) | |
| 772 | + (int)OutputConfig.OutputFlag.Down | (int)OutputConfig.OutputFlag.Up; |
| 773 | + return new OutputConfig(nFlags, byteKey); |
| 774 | + } |
| 775 | + |
| 776 | + private byte GetKeysByte(char cInput, ref bool bShift) |
| 777 | + { |
| 778 | + bShift = char.IsUpper(cInput); |
| 779 | + try |
| 780 | + { |
| 781 | + return (byte)(Keys)Enum.Parse(typeof(Keys), cInput.ToString(), true); |
| 782 | + } |
| 783 | + catch (Exception) |
| 784 | + { |
| 785 | + // HACK |
| 786 | + // ignore and try the switch |
| 787 | + } |
| 788 | +#warning HACK This is super limited to a very specific keyboard |
| 789 | + switch (cInput) |
| 790 | + { |
| 791 | + case ' ': |
| 792 | + return (byte)Keys.Space; |
| 793 | + case '!': |
| 794 | + bShift = true; |
| 795 | + return (byte)Keys.D1; |
| 796 | + case '@': |
| 797 | + bShift = true; |
| 798 | + return (byte)Keys.D2; |
| 799 | + case '#': |
| 800 | + bShift = true; |
| 801 | + return (byte)Keys.D3; |
| 802 | + case '$': |
| 803 | + bShift = true; |
| 804 | + return (byte)Keys.D4; |
| 805 | + case '%': |
| 806 | + bShift = true; |
| 807 | + return (byte)Keys.D5; |
| 808 | + case '^': |
| 809 | + bShift = true; |
| 810 | + return (byte)Keys.D6; |
| 811 | + case '&': |
| 812 | + bShift = true; |
| 813 | + return (byte)Keys.D7; |
| 814 | + case '*': |
| 815 | + bShift = true; |
| 816 | + return (byte)Keys.D8; |
| 817 | + case '(': |
| 818 | + bShift = true; |
| 819 | + return (byte)Keys.D9; |
| 820 | + case ')': |
| 821 | + bShift = true; |
| 822 | + return (byte)Keys.D0; |
| 823 | + case '~': |
| 824 | + bShift = true; |
| 825 | + return (byte)Keys.Oemtilde; |
| 826 | + case '{': |
| 827 | + bShift = true; |
| 828 | + return (byte)Keys.OemOpenBrackets; |
| 829 | + case '}': |
| 830 | + bShift = true; |
| 831 | + return (byte)Keys.Oem6; |
| 832 | + case '|': |
| 833 | + bShift = true; |
| 834 | + return (byte)Keys.Oem5; |
| 835 | + case ':': |
| 836 | + bShift = true; |
| 837 | + return (byte)Keys.Oem1; |
| 838 | + case '"': |
| 839 | + bShift = true; |
| 840 | + return (byte)Keys.Oem7; |
| 841 | + case '<': |
| 842 | + bShift = true; |
| 843 | + return (byte)Keys.Oemcomma; |
| 844 | + case '>': |
| 845 | + bShift = true; |
| 846 | + return (byte)Keys.OemPeriod; |
| 847 | + case '?': |
| 848 | + bShift = true; |
| 849 | + return (byte)Keys.OemQuestion; |
| 850 | + } |
| 851 | + |
| 852 | + throw new Exception("Unsupported character: " + cInput); |
| 853 | + } |
| 854 | + |
| 855 | + private void ShowKeysNotDefinedError() |
| 856 | + { |
| 857 | + MessageBox.Show(this, "Please specify both an input and output key.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 858 | + } |
| 859 | + |
| 860 | + |
709 | 861 | #endregion |
710 | 862 |
|
711 | 863 | } |
|
0 commit comments