From 7c7d17d47f2a376ff6f631c774f8effb3ae7a2bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Gessler?= Date: Sun, 4 Aug 2024 17:25:22 +0200 Subject: [PATCH] save research script --- research/keylogger/appname.c | 72 ++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 research/keylogger/appname.c diff --git a/research/keylogger/appname.c b/research/keylogger/appname.c new file mode 100644 index 0000000..dc0fa99 --- /dev/null +++ b/research/keylogger/appname.c @@ -0,0 +1,72 @@ +#include +#include + +CFStringRef GetActiveWindowTitle() { + // Get the accessibility element corresponding to the frontmost application + AXUIElementRef appElem = AXUIElementCreateApplication(CGMainPID()); + if (!appElem) { + return NULL; + } + + // Get the accessibility element corresponding to the frontmost window + AXUIElementRef window = NULL; + AXError error = AXUIElementCopyAttributeValue(appElem, kAXFocusedWindowAttribute, (CFTypeRef*)&window); + CFRelease(appElem); // Release the app element as we don't need it anymore + + if (error != kAXErrorSuccess) { + return NULL; + } + + // Get the title of the frontmost window + CFStringRef title = NULL; + error = AXUIElementCopyAttributeValue(window, kAXTitleAttribute, (CFTypeRef*)&title); + CFRelease(window); // Release the window element + + if (error != kAXErrorSuccess) { + return NULL; + } + + return title; +} + +// Function to get the application name (optional) +CFStringRef GetActiveWindowAppName() { + AXUIElementRef appElem = AXUIElementCreateApplication(CGMainPID()); + if (!appElem) { + return NULL; + } + + // Get the application name + CFStringRef appName = NULL; + AXError error = AXUIElementCopyAttributeValue(appElem, kAXApplicationNameAttribute, (CFTypeRef*)&appName); + CFRelease(appElem); + + if (error != kAXErrorSuccess) { + return NULL; + } + + return appName; +} + +int main() { + // Get the title of the active window + CFStringRef title = GetActiveWindowTitle(); + if (title) { + // Process the title (e.g., print it) + printf("Active Window Title: %s\n", CFStringGetCString(title, kCFStringEncodingUTF8)); + CFRelease(title); + } else { + printf("Failed to get active window title\n"); + } + + // Optionally get the application name + CFStringRef appName = GetActiveWindowAppName(); + if (appName) { + printf("Active Window Application: %s\n", CFStringGetCString(appName, kCFStringEncodingUTF8)); + CFRelease(appName); + } else { + printf("Failed to get active window application name\n"); + } + + return 0; +} \ No newline at end of file