【背景】在python中没有无阻塞的键盘输入函数,
【应用场景1】等待输入,超时后使用缺省值.
【应用场景2】在菜单中输入一个字符,然后根据字符进行不同的功能(不需要键入回车键)
【分析】在c函数库中,有对应的函数getch()和kbhit(),这两个函数在conio.h头文件中
【编程环境】
1、python版本:Python 3.4.3
2、C编译器IDE:MinGW Developer Studio 2.05 Build date:04-01-2005
3、编译器版本:gcc version 3.4.2 (mingw-special)
【MinGW Developer Studio 开发动态包】
1、创建一个工程“Win32 Dynamic-Link Library”,工程名为myPythonExtend
2、添加一个源文件:KeyboardExtend.c
3、在菜单“Edit”-->"Options...",弹出对话框
(1)添加python头文件目录
(2)添加python库文件目录
4、在菜单“Project”-->"Settings...",弹出对话框
(1)添加Debug 的link选项
Debug\Win32CIO.pyd
python34
(2)添加Release的link选项
Release\Win32CIO.pyd
python34
5、KeyboardExtend.c文件内容:
#include
#include
#include
static PyObject * mytest()
{
printf("enter key\n");
for(;;)
{
int d;
if (d = kbhit() != 0)
{
int c = getche();
if (c=='q')
{
break;
}
printf("your input:%c,%c\n",d,c);
}
}
PyObject* resultObject = Py_BuildValue("i" , 0);
return resultObject;
}
static PyObject *mypy_getchar(PyObject *self, PyObject *args)
{
char result = getchar();
PyObject* resultObject = Py_BuildValue("i" , result);
return resultObject;
}
static PyObject *mypy_putchar(PyObject *self, PyObject *args)
{
int ch;
PyArg_ParseTuple(args, "i" , &ch);
int result = putchar(ch);
PyObject* resultObject = Py_BuildValue("i" , result);
return resultObject;
}
static PyObject *mypy_getch(PyObject *self, PyObject *args)
{
int result = getch();
PyObject* resultObject = Py_BuildValue("i" , result);
return resultObject;
}
static PyObject *mypy_getche(PyObject *self, PyObject *args)
{
int result = getche();
PyObject* resultObject = Py_BuildValue("i" , result);
return resultObject;
}
static PyObject *mypy_kbhit(PyObject *self, PyObject *args)
{
int result = kbhit();
PyObject* resultObject = Py_BuildValue("i" , result);
return resultObject;
}
static PyObject *mypy_putch(PyObject *self, PyObject *args)
{
int ch;
PyArg_ParseTuple(args, "i" , &ch);
int result = putch(ch);
PyObject* resultObject = Py_BuildValue("i" , result);
return resultObject;
}
static PyObject *mypy_ungetch(PyObject *self, PyObject *args)
{
int ch;
PyArg_ParseTuple(args, "i" , &ch);
int result = ungetch(ch);
PyObject* resultObject = Py_BuildValue("i" , result);
return resultObject;
}
//模块方法表
static PyMethodDef c_io_methods[] = {
{"getchar" , mypy_getchar, METH_VARARGS, "int getchar (void) " },
{"putchar" , mypy_putchar, METH_VARARGS, "int putchar (int) " },
{"getch" , mypy_getch, METH_VARARGS, "int getch (void) " },
{"getche" , mypy_getche, METH_VARARGS, "int getche (void) " },
{"kbhit" , mypy_kbhit, METH_VARARGS, "int kbhit (void) " },
{"putch" , mypy_putch, METH_VARARGS, "int putch (int) " },
{"ungetch" , mypy_ungetch, METH_VARARGS, "int ungetch (int) " },
{"mytest" , mytest, METH_VARARGS, "mytest " },
{NULL , NULL }
};
static struct PyModuleDef win32Module_c_io = {
PyModuleDef_HEAD_INIT,
"Win32CIO" ,
"Win32 conio and stdio" ,
-1 ,
c_io_methods,
NULL ,
NULL ,
NULL ,
NULL
};
//初始化函数
PyMODINIT_FUNC
PyInit_Win32CIO(void )
{
return PyModule_Create(&win32Module_c_io);
}
6、编译生成Release\Win32CIO.pyd
拷贝该文件到D:\app\Python34\Lib\site-packages
【python调用模块函数】
#!/usr/bin/python
# -*- coding:utf-8 -*-
import Win32CIO
if __name__ == '__main__':
while(True):
if (d != 0):
if (c == None):
break
print("your input:%c,%c\n" % (d,c))
pass