使用COCOS2D-X JSB开发,在js页面中设置iOS键盘模式

时间:2023-12-29 19:19:50

XYSDK.h

void setKeyboardType(int type);

XYSDK.cpp

voidXYSDK::setKeyboardType(int type)

{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

CCEGLView * pGlView =
CCDirector::sharedDirector()->getOpenGLView();

if (pGlView)

{

==type) {

pGlView->setIMEKeyboardDefault();

==type) {

pGlView->setIMEKeyboardNumber();

}

pGlView->setIMEKeyboardState(true);

}

#endif

}

CCEGLView

voidCCEGLView::setIMEKeyboardNumber()

{

EAGLView * view = [EAGLViewsharedEGLView];

view.keyboardType =
UIKeyboardTypeNumbersAndPunctuation;

}

voidCCEGLView::setIMEKeyboardDefault()

{

EAGLView * view = [EAGLViewsharedEGLView];

view.keyboardType =UIKeyboardTypeDefault;

}

Jsb_xy.hpp

JSBool js_xy_XYSDK_setKeyboardType(JSContext *cx,
uint32_t argc,
jsval *vp);

Jsb_xy.cpp

JSBooljs_xy_XYSDK_setKeyboardType(JSContext *cx,
uint32_t argc,
jsval *vp)

{

jsval *argv =
JS_ARGV(cx, vp);

JSBool ok =
JS_TRUE;

JSObject *obj =
JS_THIS_OBJECT(cx, vp);

js_proxy_t *proxy =
jsb_get_js_proxy(obj);

XYSDK* cobj = (XYSDK *)(proxy ?proxy->ptr
: NULL);

JSB_PRECONDITION2( cobj, cx,
JS_FALSE,
"InvalidNative Object");

if (argc ==
) {

int arg0;

ok &=
jsval_to_int32], (int32_t *)&arg0);

JSB_PRECONDITION2(ok, cx,
JS_FALSE,
"Error processing arguments");

cobj->setKeyboardType(arg0);

JS_SET_RVAL(cx, vp,
JSVAL_VOID);

returnJS_TRUE;

}

JS_ReportError(cx,
"wrong number of arguments: %d, was expecting%d", argc,
);

returnJS_FALSE;

}

使用

在js文件里,attachWithIME之前设置setKeyboardType

) //默认键盘


//数字键盘

);

this._MainUI.getWidgetByName("TextField_shu_ru").attachWithIME();