[英]Python and C interaction - callback function
I'm trying to make a key logger for Mac OS for one of my research projects. 我正在尝试为我的一个研究项目制作Mac OS的关键记录器。 I have a C code which will grab keystroke and write them to a text file.
我有一个C代码,它将抓取击键并将它们写入文本文件。 (The following code I have taken out some not important stuff)
(以下代码我已经拿出一些不重要的东西)
What I need to do now is just like PyHook, instead of write the data to a text file, to pass a Python callback function to the C code and make it passes back the key input to Python, so I can do necessary analysis with Python. 我现在需要做的就是像PyHook,而不是将数据写入文本文件,将Python回调函数传递给C代码并使其将密钥输入传递回Python,因此我可以使用Python进行必要的分析。
I have look for how to do it, but honestly I have no idea how to approach this, as I am not used to C programming or Python extensions. 我一直在寻找如何做到这一点,但说实话,我不知道如何处理这个,因为我不习惯C编程或Python扩展。 Any help would be greatly 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, for fgets
#define LINE_LENGTH 80
#define kShowMouse TRUE
OSStatus RUIRecordingEventOccurred(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData);
void prepareToRecord(); // install the event handler, wait for record signal
// note that keyboard character codes are found in Figure C2 of the document
// Inside Macintosh: Text available from http://developer.apple.com
char * keyStringForKeyCode(int keyCode); // get the representation of the Mac keycode
// Global Variables
int dieNow = 0; // should the program terminate
int ifexit = 0; // Exit state
char *filename = NULL; // Log file name
FILE *fd = NULL; // Log file descriptor
int typecount = 0; // count keystroke to periodically save to a txt file
struct timeval thetime; // for gettimeofday
long currenttime; // the current time in milliseconds
int main()
{
filename = "test.txt";
fd = fopen(filename, "a");
// Get RUI ready to record or play, based off of mode
prepareToRecord();
return EXIT_SUCCESS;
}
// event handler for RUI recorder
OSStatus RUIRecordingEventOccurred(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData)
{
// Determine class and 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; // what did the user press? any modifier keys down?
// gather keystroke information
GetEventParameter(theEvent, kEventParamKeyCode, typeInteger, NULL, sizeof(keyCode), NULL, &keyCode);
GetEventParameter(theEvent, kEventParamKeyModifiers, typeInteger, NULL, sizeof(modifiers), NULL, &modifiers);
// What time is 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 an event for examination
// Types of events to listen for
EventTypeSpec eventTypes[NUM_RECORDING_EVENT_TYPES] = {{kEventClassKeyboard, kEventRawKeyDown}};
// Install the event handler
InstallEventHandler(GetEventMonitorTarget(), NewEventHandlerUPP(RUIRecordingEventOccurred), NUM_RECORDING_EVENT_TYPES, eventTypes, nil, nil);
// event loop - get events until die command
do {
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 - Just Follow the instructions - Calling Python Functions from C . 这很简单 - 只需按照说明操作 - 从C调用Python函数 。
Alternatively if you are trying to call C/C++ functions from Python you can use SWIG or one of Python's module CTypes 或者,如果您尝试从Python调用C / C ++函数,则可以使用SWIG或Python的模块CType之一
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.