Skip to content

Commit

Permalink
Hotkey bugfixes, made keys more readable
Browse files Browse the repository at this point in the history
In rare cases Windows would detect KeyDown on keys like "None" or "Modifier" that weren't being pushed by the user. These are now excluded.

Key names are now translated from names like OemPeriod to Period.
  • Loading branch information
753 authored and 753 committed Dec 10, 2020
1 parent e2a8848 commit df7f10b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 39 deletions.
8 changes: 7 additions & 1 deletion AddEditHotkeyForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ private void tbKeys_Leave(object sender, EventArgs e)

int lastAmountPressed = 0;

const string invalidKeys = " None Shift Control Alt Modifiers LButton RButton ";

private void timer1_Tick(object sender, EventArgs e)
{
int amountPressed = 0;
Expand All @@ -206,9 +208,13 @@ private void timer1_Tick(object sender, EventArgs e)
{
var pressedKeys = new List<Keys>();

string lastKey = "None";
foreach (Keys key in Enum.GetValues(typeof(Keys)))
{
if (Keyboard.IsKeyDown(key) && key.ToString() != "LButton" && key.ToString() != "RButton")
if (key.ToString() == lastKey) { continue; }
lastKey = key.ToString();

if (Keyboard.IsKeyDown(key) && !invalidKeys.Contains(" "+key.ToString()+" "))
{
amountPressed++;
pressedKeys.Add(key);
Expand Down
114 changes: 78 additions & 36 deletions Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,44 @@ internal static string userGetXMLLoc()
else return "";
}

internal static string[] keysArrayToStringArray(Keys[] keysArr)
{
var arr = new List<string>();

for (int i = 0; i < keysArr.Length; i++)
{
arr.Add(keysArr[i].ToString());
}

return arr.ToArray();
}

internal static Keys[] stringArrayToKeysArray(string[] strArr)
{
if (strArr == null) return new Keys[] { 0 };
var arr = new List<Keys>();

for (int i = 0; i < strArr.Length; i++)
{
Keys key;

if (Enum.TryParse(strArr[i], out key))
{
arr.Add(key);
}
else
{
return new Keys[] { 0 };
}
}

return arr.ToArray();
}
//internal static string[] keysArrayToStringArray(Keys[] keysArr)
//{
// var arr = new List<string>();

// for (int i = 0; i < keysArr.Length; i++)
// {
// arr.Add(keysArr[i].ToString());
// }

// return arr.ToArray();
//}

//internal static Keys[] stringArrayToKeysArray(string[] strArr)
//{
// if (strArr == null) return new Keys[] { 0 };
// var arr = new List<Keys>();

// for (int i = 0; i < strArr.Length; i++)
// {
// Keys key;

// if (Enum.TryParse(strArr[i], out key))
// {
// arr.Add(key);
// }
// else
// {
// return new Keys[] { 0 };
// }
// }

// return arr.ToArray();
//}

internal static bool keysArrayFromString(string key, out Keys[] keysArr, out string errorMessage)
{
Dictionary<string, string> stringToKeyTranslations = keyToStringTranslations.ToDictionary(x => x.Value, x => x.Key);

if (key.Contains("+"))
{
string[] sKeys = key.Split('+');
Expand All @@ -69,6 +71,11 @@ internal static bool keysArrayFromString(string key, out Keys[] keysArr, out str
{
Keys kKey;

if (stringToKeyTranslations.ContainsKey(sKeys[i]))
{
sKeys[i] = stringToKeyTranslations[sKeys[i]];
}

if (Enum.TryParse(sKeys[i], out kKey))
{
kKeys.Add(kKey);
Expand All @@ -89,6 +96,11 @@ internal static bool keysArrayFromString(string key, out Keys[] keysArr, out str
{
Keys kKey;

if (stringToKeyTranslations.ContainsKey(key))
{
key = stringToKeyTranslations[key];
}

if (Enum.TryParse(key, out kKey))
{
keysArr = new Keys[] { kKey };
Expand All @@ -104,18 +116,48 @@ internal static bool keysArrayFromString(string key, out Keys[] keysArr, out str
}
}

public static Dictionary<string, string> keyToStringTranslations = new Dictionary<string, string>(){
{"Oemcomma", "Comma"},
{"OemPeriod", "Period"},
{"OemQuestion", "ForwardSlash"},
{"OemOpenBrackets", "LeftBracket"},
{"Oem6", "RightBracket"},
{"Oem5", "BackSlash"},
{"Oemplus", "Plus"},
{"OemMinus", "Minus"},
{"Back", "Backspace"},
{"Oemtilde", "Backtick"},
{"D0", "0"},
{"D1", "1"},
{"D2", "2"},
{"D3", "3"},
{"D4", "4"},
{"D5", "5"},
{"D6", "6"},
{"D7", "7"},
{"D8", "8"},
{"D9", "9"},
{"Oem1", "Semicolon"},
{"Oem7", "Quotation"}
};
internal static string keysToString(params Keys[] keysArr)
{
if (keysArr == null) return "";
string temp = "";
string result = "";
int kLen = keysArr.Length;

for (int i = 0; i < kLen; i++)
{
temp += keysArr[i].ToString() + (i == kLen - 1 ? "" : "+");
string next = keysArr[i].ToString();
if (keyToStringTranslations.ContainsKey(next))
{
next = keyToStringTranslations[next];
}

result += next + (i == kLen - 1 ? "" : "+");
}

return temp;
return result;
}

internal static bool soundLocsArrayFromString(string soundLocsStr, out string[] soundLocs, out string errorMessage)
Expand Down
14 changes: 12 additions & 2 deletions SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ private void tbStopSoundKeys_Leave(object sender, EventArgs e)

int lastAmountPressed = 0;

const string invalidKeys = " None Shift Control Alt Modifiers LButton RButton ";

private void timer1_Tick(object sender, EventArgs e)
{
int amountPressed = 0;
Expand All @@ -153,9 +155,13 @@ private void timer1_Tick(object sender, EventArgs e)
{
var pressedKeys = new List<Keys>();

string lastKey = "None";
foreach (Keys key in Enum.GetValues(typeof(Keys)))
{
if (Keyboard.IsKeyDown(key))
if (key.ToString() == lastKey) { continue; }
lastKey = key.ToString();

if (Keyboard.IsKeyDown(key) && !invalidKeys.Contains(" " + key.ToString() + " "))
{
amountPressed++;
pressedKeys.Add(key);
Expand Down Expand Up @@ -201,9 +207,13 @@ private void Timer2_Tick(object sender, EventArgs e)
{
var pressedKeys = new List<Keys>();

string lastKey = "None";
foreach (Keys key in Enum.GetValues(typeof(Keys)))
{
if (Keyboard.IsKeyDown(key))
if (key.ToString() == lastKey) { continue; }
lastKey = key.ToString();

if (Keyboard.IsKeyDown(key) && !invalidKeys.Contains(" " + key.ToString() + " "))
{
amountPressed++;
pressedKeys.Add(key);
Expand Down

0 comments on commit df7f10b

Please sign in to comment.