Created
August 2, 2025 14:52
-
-
Save electerious/cadb78bcc13cf3455321077d1b82edbf to your computer and use it in GitHub Desktop.
Swift keyboard listener with HTTP request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/swift | |
import Cocoa | |
class AppDelegate: NSObject, NSApplicationDelegate { | |
func applicationDidFinishLaunching(_ notification: Notification) { | |
NSEvent.addGlobalMonitorForEvents(matching: .keyDown) { event in | |
print("Key code pressed: \(event.keyCode)") | |
print("Modifier pressed: \(event.modifierFlags.contains(.command))") | |
if event.keyCode == 33 { | |
let url = URL(string: "http://localhost:3000")! | |
let task = URLSession.shared.dataTask(with: url) { data, response, error in | |
if let error = error { | |
print("Error: \(error)") | |
} else if let data = data, let responseString = String(data: data, encoding: .utf8) { | |
print("Response: \(responseString)") | |
} | |
} | |
task.resume() | |
} | |
} | |
} | |
} | |
let app = NSApplication.shared | |
let delegate = AppDelegate() | |
app.delegate = delegate | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment