-
Notifications
You must be signed in to change notification settings - Fork 4
/
keycodepy.c
41 lines (36 loc) · 1.12 KB
/
keycodepy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <Python.h>
#include "keycode.h"
static PyObject *cfStringConsumedToPy(CFStringRef s) {
if (!s) Py_RETURN_NONE;
CFIndex len = CFStringGetLength(s);
UniChar *buffer = calloc(len+1, sizeof(UniChar));
if (!buffer) {
CFRelease(s);
return NULL;
}
CFStringGetCharacters(s, CFRangeMake(0, len), buffer);
CFRelease(s);
return Py_BuildValue("u#", buffer, len);
}
static PyObject *
keycode_tostring(PyObject *self, PyObject *args) {
int keyCode;
if (!PyArg_ParseTuple(args, "i", &keyCode)) return NULL;
return cfStringConsumedToPy(createStringForKey((CGKeyCode)keyCode));
}
static PyObject *
keycode_tokeycode(PyObject *self, PyObject *args) {
char c;
if (!PyArg_ParseTuple(args, "c", &c)) return NULL;
return Py_BuildValue("i", keyCodeForChar(c));
}
static PyMethodDef KeycodeMethods[] = {
{"tostring", keycode_tostring, METH_VARARGS,
"Convert a keycode to the equivalent string"},
{"tokeycode", keycode_tokeycode, METH_VARARGS,
"Convert a character to the equivalent keycode"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initkeycode(void) {
(void)Py_InitModule("keycode", KeycodeMethods);
}