Skip to content

Instantly share code, notes, and snippets.

@electerious
Created August 2, 2025 14:52
Show Gist options
  • Save electerious/cadb78bcc13cf3455321077d1b82edbf to your computer and use it in GitHub Desktop.
Save electerious/cadb78bcc13cf3455321077d1b82edbf to your computer and use it in GitHub Desktop.
Swift keyboard listener with HTTP request
#!/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