---
# System prepended metadata

title: SelfSpy in .NET

---

# Windows

### Part 1/6: Project Setup and Understanding SelfSpy

**Project Setup:** Start by setting up your project in Visual Studio, or your preferred IDE, as a .NET Core Console Application. Ensure that you target .NET Core 3.1 or higher.

**Understanding SelfSpy:** Familiarize yourself with the original SelfSpy Python script. It's a self-monitoring application that records your activities, keys pressed, mouse movements etc.

### Part 2/6: Key Logging & Mouse Tracking

**Key Logging:** For keylogging, use the GetAsyncKeyState function from the user32.dll to detect key presses. You'll need to use P/Invoke to call this function from C#. You can run this call in a loop to continuously track key states.

**Mouse Tracking:** Similar to keylogging, you can use GetCursorPos function from user32.dll to track mouse events.

Remember, getting these low-level inputs will involve reading unmanaged code and could be subject to permissions issues as it might be considered invasive activity by some anti-virus programs.

### Part 3/6: Window Titles & Active Applications

**Active Window Title:** You can use GetForegroundWindow and GetWindowText functions from user32.dll to get the title of the active window.

**Active Application:** To get the active application, first get the active window handle. Then get the process id associated with that window handle using GetWindowThreadProcessId. Finally, get the process name from the process id.

Remember to be careful with memory management when interacting with the Windows API, handle any exceptions and always clean up unmanaged resources.

### Part 4/6: Handling Data

**Data Management:** Once you have the necessary data, you need to store it properly. For easy cross-platform compatibility, consider using SQLite to store your data locally. You can use the Microsoft Entity Framework Core to interact with SQLite.

**Data Structure:** Key presses, mouse clicks, active window, active application, etc. can be separate tables in your database with each table having a timestamp so that you can collate the data later to gain insights.

### Part 5/6: Building UI

Create a simple UI for users to be able to start/stop tracking and review their data. This UI could be a simple tray application, with options to start/stop the tracking.

### Part 6/6: Cross-Platform Compatibility & Testing

**macOS Compatibility:** .NET Core is cross-platform, but since you're interacting with Windows' native functions, these features will not work on macOS. For macOS, you will need to find equivalent macOS APIs and use P/Invoke to call these functions, or use something like Xamarin.Mac to access macOS APIs in C#.

**Testing:** Test the application thoroughly on both Windows and macOS for different scenarios, properly handle all possible exceptions and ensure all resources are being cleaned after use.

# macOS

### Part 1/3: Project Setup & Data Management

**Project Setup:** You can follow the same project setup steps but make sure to use Xamarin.Mac to access macOS APIs in C#.

**Data Management:** Similar to the Windows version, you can use SQLite with Entity Framework Core to store your data. Structure your data based on the information you plan to collect, such as key presses, mouse clicks, active window, active application, etc.

### Part 2/3: Key Logging, Mouse Tracking, & Active Applications

**Key Logging:** macOS does not provide an easy native API for keylogging due to security reasons. However, you can use the Accessibility API to observe keystrokes by registering as an event handler for global keyboard events.

**Mouse Tracking:** Use the CGEvent.tapCreate function of the Core Graphics framework to capture global mouse events.

**Active Window & Application:** Use the NSWorkspace.sharedWorkspace API to get the active application and its details.

Remember, access to these APIs requires explicit permission from the user and is subject to macOS's security policies.

### Part 3/3: Building UI & Testing

**Building UI:** Xamarin.Mac gives you access to native macOS APIs for building the UI. You can create a simple status bar application, with options to start/stop tracking.

**Testing:** Thorough testing on macOS is especially important given the stricter security guidelines. Make sure all the permissions are handled correctly, all possible exceptions are dealt with and resources are managed properly.