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 Feb 15, 2022
1 parent 9f3de05 commit c7afc65
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
58 changes: 58 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 @@ -853,11 +854,68 @@ 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)
{
if (line.TrimStart().StartsWith("//"))
continue; // skipping a comment

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).TrimEnd();
}
}

param2 = Regex.Unescape(UnquoteString(param2));
param1 = Regex.Unescape(UnquoteString(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 c7afc65

Please sign in to comment.