Python和C交互 – 回调函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python和C交互 – 回调函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3331字,纯文字阅读大概需要5分钟。
内容图文
![Python和C交互 – 回调函数](/upload/InfoBanner/zyjiaocheng/790/2c22b63da8864b028d2ce05cb6659167.jpg)
我正在尝试为我的一个研究项目制作Mac OS的关键记录器.
我有一个C代码,它将抓取击键并将它们写入文本文件. (以下代码我已经拿出一些不重要的东西)
我现在需要做的就像PyHook,而不是将数据写入文本文件,
将Python回调函数传递给C代码并使其将密钥输入传递回Python,因此我可以使用Python进行必要的分析.
我一直在寻找如何做到这一点,但说实话,我不知道如何处理这个,因为我不习惯C编程或Python扩展.任何帮助将不胜感激.
#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"
}
}
解决方法:
这很简单 – 只需按照说明操作 – Calling Python Functions from C.
或者,如果您尝试从Python调用C/C++函数,则可以使用SWIG或Python的模块CTypes之一
内容总结
以上是互联网集市为您收集整理的Python和C交互 – 回调函数全部内容,希望文章能够帮你解决Python和C交互 – 回调函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。