none
keylogger code in c#.net

    Question

  • I need keylogger code in c#.net even when the application is minimized to system tray.
    Can any one please help me?

    Thank you..
    Thursday, August 16, 2007 7:33 AM

Answers

  • Hi,

    This is the code i use to capture a print screen key. NOTE: this is just a snippit to give you an idea this code will not work if you just copy and paste it; this is basically the engine to what you are looking for it just needs to be modified into your code.

    Code Snippet

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Drawing;

        class Program
        {
            private const int WH_KEYBOARD_LL = 13;
            private const int WM_KEYDOWN = 0x0100;
            private static LowLevelKeyboardProc _proc = HookCallback;
            private static IntPtr _hookID = IntPtr.Zero;

           
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

            private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

            [STAThread]
            static void Main(string[] args)
            {

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                _hookID = SetHook(_proc);

                Application.Run();

                UnhookWindowsHookEx(_hookID);

            }

            private static IntPtr SetHook(LowLevelKeyboardProc proc)
            {
                using (Process curProcess = Process.GetCurrentProcess())
                using (ProcessModule curModule = curProcess.MainModule)
                {
                    return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
                }
            }       

            private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
                {               
                    int vkCode = Marshal.ReadInt32(lParam);

                    if ((Keys)vkCode == Keys.PrintScreen)
                    {
                            CaptureScreen();
                    }
                    //Console.WriteLine((Keys)vkCode);
                }
                return CallNextHookEx(_hookID, nCode, wParam, lParam);
            }





    enjoy

    Lee
    Thursday, August 16, 2007 10:31 AM

All replies

  • Hi,

    This is the code i use to capture a print screen key. NOTE: this is just a snippit to give you an idea this code will not work if you just copy and paste it; this is basically the engine to what you are looking for it just needs to be modified into your code.

    Code Snippet

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Drawing;

        class Program
        {
            private const int WH_KEYBOARD_LL = 13;
            private const int WM_KEYDOWN = 0x0100;
            private static LowLevelKeyboardProc _proc = HookCallback;
            private static IntPtr _hookID = IntPtr.Zero;

           
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

            private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

            [STAThread]
            static void Main(string[] args)
            {

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                _hookID = SetHook(_proc);

                Application.Run();

                UnhookWindowsHookEx(_hookID);

            }

            private static IntPtr SetHook(LowLevelKeyboardProc proc)
            {
                using (Process curProcess = Process.GetCurrentProcess())
                using (ProcessModule curModule = curProcess.MainModule)
                {
                    return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
                }
            }       

            private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
                {               
                    int vkCode = Marshal.ReadInt32(lParam);

                    if ((Keys)vkCode == Keys.PrintScreen)
                    {
                            CaptureScreen();
                    }
                    //Console.WriteLine((Keys)vkCode);
                }
                return CallNextHookEx(_hookID, nCode, wParam, lParam);
            }





    enjoy

    Lee
    Thursday, August 16, 2007 10:31 AM
  • I'll assume for a moment that this isn't for any nefarious purposes.

     

    Check out this code: http://www.codeproject.com/csharp/globalhook.asp

     

    with some tweaking, it will definitely work (I've used it myself in the past). Be warned though, it's pretty advanced stuff.

     

    The really interesting stuff all happens in:

    KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)

     

    I am designing a Kiosk application, and needed to block certain keys (eg the Win key) among several other combinations. Add:

     

    switch (keyData.ToString()) {

    case "LWin": handled = truebreak;

    }

     

    If u need to catch key-combinations, bear in mind that this is processing each key in turn. As the author states:

     

    "You'll have to track which keys have gone down but not up. Only the most recently pressed key keeps sending KeyDown messages, but the others will still send a KeyUp when released. So if you make three flags IsCtrlDown, IsShiftDown, and IsADown, and set them to true at KeyDown and false at KeyUp, the expression (IsCtrlDown && IsShiftDown && IsADown) will give you the required result."

     

    My version of what the author is suggesting:

     

    private bool _LAlt = false;

    private bool _RAlt = false;

    private bool _LCtl = false;

    private bool _RCtl = false;

    private bool _LSft = false;

    private bool _RSft = false;

    private bool _LWin = false;

    private bool _RWin = false;

     

    private bool _Alt;

    private bool _Ctl;

    private bool _Sft;

    private bool _Win;

     

    // then in

    // KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) 

    //     if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) {

    // section:

     

    switch (keyData.ToString()) {

    case "LMenu": _LAlt = true; _Alt = true; break;

    case "LControlKey": _LCtl = true; _Ctl = true; break;

    case "LShiftKey": _LSft = true; _Sft = true; break;

    case "LWin": _LWin = true; _Win = true; break;

    case "RMenu": _RAlt = true; _Alt = true; break;

    case "RControlKey": _RCtl = true; _Ctl = true; break;

    case "RShiftKey": _RSft = true; _Sft = true; break;

    case "RWin": _RWin = true; _Win = true; break;

    }

     

    // then in the KeyUp section:

     

    switch (keyData.ToString()) {

    case "LMenu": _LAlt = false; if (_RAlt) _Alt = true; else _Alt = false; break;

    case "LControlKey": _LCtl = false; if (_RCtl) _Ctl = true; else _Ctl = false; break;

    case "LShiftKey": _LSft = false; if (_RSft) _Sft = true; else _Sft = false; break;

    case "LWin": _LWin = false; if (_RWin) _Win = true; else _Win = false; break;

    case "RMenu": _RAlt = false; if (_LAlt) _Alt = true; else _Alt = false; break;

    case "RControlKey": _RCtl = false; if (_LCtl) _Ctl = true; else _Ctl = false; break;

    case "RShiftKey": _RSft = false; if (_LSft) _Sft = true; else _Sft = false; break;

    case "RWin": _RWin = false; if (_LWin) _Win = true; else _Win = false; break;

    }

     

    to potentially save you lots of work, u might also find this useful:

     

    #region MediaKeys

    /*

    181 SelectMedia

    179 MediaPlayPause

    173 VolumeMute

    175 VolumeUp

    174 VolumeDown

    171 BrowserFavorites

    180 LaunchMail

    172 BrowserHome

    */

    #endregion

    #region Escape Row

    /*

    27 Escape

    112 F1

    113 F2

    114 F3

    115 F4

    116 F5

    117 F6

    118 F7

    119 F8

    120 F9

    121 F10

    122 F11

    123 F12

    (44) PrintScreen

    145 Scroll

    19 Pause

    */

    #endregion

    #region Number Row

    /*

    223 Oem8

    48-57 D0-D1

    189 OemMinus

    187 Oemplus

    8 Back

    45 Insert

    36 Home

    33 PageUp

    */

    #endregion

    #region Tab Row

    /*

    (9) Tab

    81 Q

    87 W

    69 E

    82 R

    84 T

    89 Y

    85 U

    73 I

    79 O

    80 P

    219 OemOpenBrackets

    221 Oem6

    (13) Return

    46 Delete

    35 End

    34 Next

    */

    #endregion

    #region CapsLock Row

    /*

    20 Capital

    65 A

    83 S

    68 D

    70 F

    71 G

    72 H

    74 J

    75 K

    76 L

    186 Oem1

    192 Oemtilde

    222 Oem7

    */

    #endregion

    #region ShiftKey Row

    /*

    16 LShiftKey

    220 Oem5

    90 Z

    88 X

    67 C

    86 V

    66 B

    78 N

    77 M

    188 Oemcomma

    190 OemPeriod

    191 OemQuestion

    16 RShiftKey

    */

    #endregion

    #region Bottom Row

    /*

    17 LControlKey

    (91) LWin

    18 LMenu

    32 Space

    18 LControlKey THEN RMenu

    (92) RWin

    93 Apps

    17 RControlKey

    */

    #endregion

    #region ArrowKeys

    /*

    38 Up

    37 Left

    40 Down

    39 Right

    */

    #endregion

    #region NumpPad

    /*

    144 NumLock

    111 Divide

    106 Multiply

    109 Subtract

    ----------------------------------------------------------------

    103 NumPad7 36 Home

    104 NumPad8 38 Up

    105 NumPad9 33 PageUp

    107 Add

    ----------------------------------------------------------------

    100 NumPad4 37 Left

    101 NumPad5 12 Clear

    102 NumPad6 39 Right

    ----------------------------------------------------------------

    97 NumPad1 35 End

    98 NumPad2 40 Down

    99 NumPad3 34 Next

    13 Return

    ----------------------------------------------------------------

    96 NumPad0 45 Insert

    110 Decimal 46 Delete

    */

    #endregion

    Thursday, August 16, 2007 10:35 AM
  • i made up a open source keylogging component for c#

     

    try it

     

    link

    Tuesday, June 01, 2010 2:05 AM
  • You can also try this simple & useful created using vs2010 (c#).
    http://csharpdemos.blogspot.in/2010/03/personal-keylogger.html
    Thursday, September 20, 2012 7:59 AM
  • Explained with details plus c# source code just goto 

    http://csharpdemos.blogspot.com/2012/09/how-to-create-keylogger-using-c.html

    Thursday, September 20, 2012 8:03 AM