Skip to content

Commit

Permalink
Reading the Hash Keys from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
d0vgan committed Dec 10, 2020
1 parent e7a7692 commit 8fee283
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions SharpKeys/Dialog_Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace SharpKeys
Expand Down Expand Up @@ -826,11 +827,69 @@ private void btnSaveKeys_Click(object sender, EventArgs e)
}
}

private string UnquoteString(string s)
{
// remove the leading & trailing '"'
if (s.StartsWith("\"") && s.EndsWith("\""))
{
s = s.Substring(1, s.Length - 2);
}
return s;
}

private bool ReadHashKeysFromFile(string pathToKeysFile)
{
if (!File.Exists(pathToKeysFile))
return false; // the file does not exist

string[] lines = File.ReadAllLines(pathToKeysFile, System.Text.Encoding.Unicode);
if (lines.Length == 0)
return false; // the file is empty

m_hashKeys = new Hashtable();

foreach (string line in lines)
{
string[] items = line.Split(new char[]{','}, 2);
if (items.GetLength(0) == 2)
{
string param1 = items[0].Trim(); // w/o leading & trailing spaces
string param2 = items[1].Trim(); // w/o leading & trailing spaces

int n = param2.LastIndexOf('"');
if (n != -1)
{
// removing the trailing '//' after the last '"', if any
n = param2.IndexOf("//", n);
if (n != -1)
{
param2 = param2.Remove(n);
param2 = param2.TrimEnd();
}
}

param2 = UnquoteString(param2);
param2 = Regex.Unescape(param2);

param1 = UnquoteString(param1);
param1 = Regex.Unescape(param1);

m_hashKeys.Add(param1, param2);
}
}
return true;
}

private void BuildParseTables()
{
if (m_hashKeys != null)
return;

string pathToExeDir = Path.GetDirectoryName(Application.ExecutablePath);
string pathToKeysFile = Path.Combine(pathToExeDir, "SharpKeys.keys");
if (ReadHashKeysFromFile(pathToKeysFile))
return;

// the hash table uses a string in the form of Hi_Lo scan code (in Hex values)
// that most sources say are scan codes. The 00_00 will disable a key - everything else
// is pretty obvious. There is a bit of a reverse lookup however, so labels changed here
Expand Down
Binary file added SharpKeys/bin/Debug/SharpKeys.keys
Binary file not shown.
Binary file added SharpKeys/bin/Release/SharpKeys.keys
Binary file not shown.

0 comments on commit 8fee283

Please sign in to comment.