[Cocos2d-x] Click blank to hide keyboard

Source: Internet
Author: User

 

 
 

 

The edit box of cocos2dx edit displays a keyboard, but it is not flexible. Click return to hide it. If you need to click the background outside the keyboard to hide the keyboard, it is much easier.

 


Method:

1. Comment two reurns under EGLView. mm to ensure that the click event can be sent to the bottom layer when the soft keyboard is displayed.


[Cpp]
// Pass the touches to the superview
# Pragma mark EAGLView-Touch Delegate
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
{
If (isKeyboardShown _)
{
[Self handleTouchesAfterKeyboardShow];

// WARNING: commented by Teng. Click the background to hide the soft keyboard.
// Return;
}

Int ids [IOS_MAX_TOUCHES_COUNT] = {0 };
Float xs [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Float ys [IOS_MAX_TOUCHES_COUNT] = {0.0f };

Int I = 0;
For (UITouch * touch in touches ){
Ids [I] = (int) touch;
Xs [I] = [touch locationInView: [touch view]. x * view. contentScaleFactor ;;
Ys [I] = [touch locationInView: [touch view]. y * view. contentScaleFactor ;;
++ I;
}
Cocos2d: CCEGLView: Export dopenglview ()-> handleTouchesBegin (I, ids, xs, ys );
}

// Pass the touches to the superview
# Pragma mark EAGLView-Touch Delegate
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
{
If (isKeyboardShown _)
{
[Self handleTouchesAfterKeyboardShow];

// WARNING: commented by Teng. Click the background to hide the soft keyboard.
// Return;
}

Int ids [IOS_MAX_TOUCHES_COUNT] = {0 };
Float xs [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Float ys [IOS_MAX_TOUCHES_COUNT] = {0.0f };

Int I = 0;
For (UITouch * touch in touches ){
Ids [I] = (int) touch;
Xs [I] = [touch locationInView: [touch view]. x * view. contentScaleFactor ;;
Ys [I] = [touch locationInView: [touch view]. y * view. contentScaleFactor ;;
++ I;
}
Cocos2d: CCEGLView: Export dopenglview ()-> handleTouchesBegin (I, ids, xs, ys );
}

[Cpp]
(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event
{
If (isKeyboardShown _)
{
<STRONG> <SPAN style = "COLOR: # ff0000"> // WARNING: commented by Teng. Click the background to hide the soft keyboard. </SPAN>
// Return; </STRONG>
}

Int ids [IOS_MAX_TOUCHES_COUNT] = {0 };
Float xs [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Float ys [IOS_MAX_TOUCHES_COUNT] = {0.0f };

Int I = 0;
For (UITouch * touch in touches ){
Ids [I] = (int) touch;
Xs [I] = [touch locationInView: [touch view]. x * view. contentScaleFactor ;;
Ys [I] = [touch locationInView: [touch view]. y * view. contentScaleFactor ;;
++ I;
}
Cocos2d: CCEGLView: Export dopenglview ()-> handleTouchesEnd (I, ids, xs, ys );
}

-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event
{
If (isKeyboardShown _)
{
// WARNING: commented by Teng. Click the background to hide the soft keyboard.
// Return;
}

Int ids [IOS_MAX_TOUCHES_COUNT] = {0 };
Float xs [IOS_MAX_TOUCHES_COUNT] = {0.0f };
Float ys [IOS_MAX_TOUCHES_COUNT] = {0.0f };

Int I = 0;
For (UITouch * touch in touches ){
Ids [I] = (int) touch;
Xs [I] = [touch locationInView: [touch view]. x * view. contentScaleFactor ;;
Ys [I] = [touch locationInView: [touch view]. y * view. contentScaleFactor ;;
++ I;
}
Cocos2d: CCEGLView: Export dopenglview ()-> handleTouchesEnd (I, ids, xs, ys );
}

 

 


2. Add processing in the bottom layer class: Display and hide the keyboard.


[Cpp]
Void ccTouchEnded (cocos2d: CCTouch * pTouch, cocos2d: CCEvent * pEvent)
{
Do
{
If (mTelNumber ){
CCPoint endPos = pTouch-> getLocation ();
 
Float delta = 5.f;
If (: abs (mBeginPos. x-endPos. x)> delta
|: Abs (mBeginPos. y-endPos. y)> delta ){
Break;
}
 
// Check whether the editing box is clicked
CCPoint point = mTelNumber-> getParent ()-> convertTouchToNodeSpaceAR (pTouch );
 
// Anchor (0.f, 0.5f)
// Int x = mTextField-> getParent ()-> getPosition (). x;
// Int y = mTextField-> getParent ()-> getPosition (). y;
Int w = mTelNumber-> getContentSize (). width;
Int h = mTelNumber-> getContentSize (). height;
CCRect rect = CCRect (0,-h/2, w, h );
 
OnClickedTextField (rect. containsPoint (point ));
}
} While (0 );
 
DialogLayer: ccTouchEnded (pTouch, pEvent );
}
 
/** Click the promotion code input box */
Void onClickedTextField (bool B)
{
If (B ){
MTelNumber-> attachWithIME ();
} Else {
MTelNumber-> detachWithIME ();
}
}

Void ccTouchEnded (cocos2d: CCTouch * pTouch, cocos2d: CCEvent * pEvent)
{
Do
{
If (mTelNumber ){
CCPoint endPos = pTouch-> getLocation ();

Float delta = 5.f;
If (: abs (mBeginPos. x-endPos. x)> delta
|: Abs (mBeginPos. y-endPos. y)> delta ){
Break;
}

// Check whether the editing box is clicked
CCPoint point = mTelNumber-> getParent ()-> convertTouchToNodeSpaceAR (pTouch );

// Anchor (0.f, 0.5f)
// Int x = mTextField-> getParent ()-> getPosition (). x;
// Int y = mTextField-> getParent ()-> getPosition (). y;
Int w = mTelNumber-> getContentSize (). width;
Int h = mTelNumber-> getContentSize (). height;
CCRect rect = CCRect (0,-h/2, w, h );

OnClickedTextField (rect. containsPoint (point ));
}
} While (0 );

DialogLayer: ccTouchEnded (pTouch, pEvent );
}

/** Click the promotion code input box */
Void onClickedTextField (bool B)
{
If (B ){
MTelNumber-> attachWithIME ();
} Else {
MTelNumber-> detachWithIME ();
}
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.