Quasar/Client/Core/Helper/KeyloggerHelper.cs

112 lines
3.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Text;
2015-05-24 21:17:21 +00:00
using System.Windows.Forms;
2015-07-26 17:36:11 +00:00
using xClient.Core.MouseKeyHook.WinApi;
2015-05-23 13:57:38 +00:00
2015-07-26 17:36:11 +00:00
namespace xClient.Core.Helper
2015-05-23 13:57:38 +00:00
{
2015-07-26 17:36:11 +00:00
public static class KeyloggerHelper
2015-05-23 13:57:38 +00:00
{
2015-05-25 09:55:41 +00:00
#region "Extension Methods"
public static bool IsModifierKeysSet(this List<Keys> pressedKeys)
{
return pressedKeys != null &&
(pressedKeys.Contains(Keys.LControlKey)
|| pressedKeys.Contains(Keys.RControlKey)
|| pressedKeys.Contains(Keys.LMenu)
|| pressedKeys.Contains(Keys.RMenu)
|| pressedKeys.Contains(Keys.LWin)
|| pressedKeys.Contains(Keys.RWin)
|| pressedKeys.Contains(Keys.Control)
|| pressedKeys.Contains(Keys.Alt));
}
public static bool IsModifierKey(this Keys key)
{
return (key == Keys.LControlKey
|| key == Keys.RControlKey
|| key == Keys.LMenu
|| key == Keys.RMenu
|| key == Keys.LWin
|| key == Keys.RWin
|| key == Keys.Control
|| key == Keys.Alt);
}
public static bool ContainsKeyChar(this List<Keys> pressedKeys, char c)
{
return pressedKeys.Contains((Keys)char.ToUpper(c));
}
2015-05-25 09:55:41 +00:00
public static bool IsExcludedKey(this Keys k)
{
// The keys below are excluded. If it is one of the keys below,
// the KeyPress event will handle these characters. If the keys
// are not any of those specified below, we can continue.
return (k >= Keys.A && k <= Keys.Z
2015-05-25 09:55:41 +00:00
|| k >= Keys.NumPad0 && k <= Keys.Divide
|| k >= Keys.D0 && k <= Keys.D9
|| k >= Keys.Oem1 && k <= Keys.OemClear
|| k >= Keys.LShiftKey && k <= Keys.RShiftKey
|| k == Keys.CapsLock
|| k == Keys.Space);
2015-05-25 09:55:41 +00:00
}
#endregion
public static bool DetectKeyHolding(List<char> list, char search)
{
return list.FindAll(s => s.Equals(search)).Count > 1;
}
2015-05-23 13:57:38 +00:00
public static string Filter(char key)
{
if ((int)key < 32) return string.Empty;
2015-05-23 13:57:38 +00:00
switch (key)
{
case '<':
return "&lt;";
case '>':
return "&gt;";
case '#':
return "&#35;";
case '&':
return "&amp;";
case '"':
return "&quot;";
case '\'':
return "&apos;";
case ' ':
return "&nbsp;";
2015-05-23 13:57:38 +00:00
}
return key.ToString();
}
2015-05-24 21:17:21 +00:00
public static string GetDisplayName(Keys key, bool altGr = false)
2015-05-23 13:57:38 +00:00
{
2015-05-24 21:17:21 +00:00
string name = key.ToString();
if (name.Contains("ControlKey"))
2015-05-23 13:57:38 +00:00
return "Control";
2015-05-24 21:17:21 +00:00
else if (name.Contains("Menu"))
2015-05-23 13:57:38 +00:00
return "Alt";
2015-05-24 21:17:21 +00:00
else if (name.Contains("Win"))
2015-05-23 13:57:38 +00:00
return "Win";
2015-05-24 21:17:21 +00:00
else if (name.Contains("Shift"))
return "Shift";
2015-05-24 21:17:21 +00:00
return name;
2015-05-23 13:57:38 +00:00
}
public static string GetActiveWindowTitle()
{
StringBuilder sbTitle = new StringBuilder(1024);
2015-07-26 17:36:11 +00:00
ThreadNativeMethods.GetWindowText(ThreadNativeMethods.GetForegroundWindow(), sbTitle,
2015-05-23 13:57:38 +00:00
sbTitle.Capacity);
string title = sbTitle.ToString();
return (!string.IsNullOrEmpty(title)) ? title : null;
}
}
}