keyboard input support for wechat program (#14836)

* add keyboard input adapter for wechat program

* refine

* refine
This commit is contained in:
mmyduckx
2023-04-20 18:53:56 +08:00
committed by GitHub
parent 73412caf59
commit 097c3cead0
6 changed files with 87 additions and 17 deletions
+19 -17
View File
@@ -57,7 +57,7 @@
return;
}
this._ensureKeyboardHide(() => {
let delegate = this._delegate;
const delegate = this._delegate;
this._showKeyboard();
this._registerKeyboardEvent();
this._editing = true;
@@ -68,33 +68,36 @@
endEditing () {
this._hideKeyboard();
let cbs = this._eventListeners;
const cbs = this._eventListeners;
cbs.onKeyboardComplete && cbs.onKeyboardComplete();
},
_registerKeyboardEvent () {
let self = this;
let delegate = this._delegate;
let cbs = this._eventListeners;
const self = this;
const delegate = this._delegate;
const cbs = this._eventListeners;
cbs.onKeyboardInput = function (res) {
if (delegate._string !== res.value) {
delegate._editBoxTextChanged(res.value);
}
}
};
cbs.onKeyboardConfirm = function (res) {
res && res.value ? delegate._editBoxEditingReturn(res.value) : delegate._editBoxEditingReturn();
let cbs = self._eventListeners;
const cbs = self._eventListeners;
cbs.onKeyboardComplete && cbs.onKeyboardComplete();
}
};
cbs.onKeyboardComplete = function (res) {
self._editing = false;
_currentEditBoxImpl = null;
self._unregisterKeyboardEvent();
// wechat program do not have offKeyboard related callback
if (cc.sys.platform !== cc.sys.Platform.WECHAT_MINI_PROGRAM) {
self._unregisterKeyboardEvent();
}
res && res.value ? delegate._editBoxEditingDidEnded(res.value) : delegate._editBoxEditingDidEnded();
}
};
__globalAdapter.onKeyboardInput(cbs.onKeyboardInput);
__globalAdapter.onKeyboardConfirm(cbs.onKeyboardConfirm);
@@ -102,7 +105,7 @@
},
_unregisterKeyboardEvent () {
let cbs = this._eventListeners;
const cbs = this._eventListeners;
if (cbs.onKeyboardInput) {
__globalAdapter.offKeyboardInput(cbs.onKeyboardInput);
@@ -123,7 +126,7 @@
},
_ensureKeyboardHide (cb) {
let otherEditing = this._otherEditing();
const otherEditing = this._otherEditing();
if (!otherEditing && !_hideKeyboardTimeout) {
return cb();
}
@@ -140,8 +143,8 @@
},
_showKeyboard () {
let delegate = this._delegate;
let multiline = (delegate.inputMode === EditBoxComp.InputMode.ANY);
const delegate = this._delegate;
const multiline = (delegate.inputMode === EditBoxComp.InputMode.ANY);
__globalAdapter.showKeyboard({
defaultValue: delegate.string,
maxLength: delegate.maxLength < 0 ? MAX_VALUE : delegate.maxLength,
@@ -153,7 +156,7 @@
},
fail (res) {
cc.warn(res.errMsg);
}
},
});
},
@@ -168,5 +171,4 @@
});
},
});
})();
}());
+5
View File
@@ -23,6 +23,11 @@ App({
_touchMove: undefined,
_touchEnd: undefined,
_touchCancel: undefined,
showInput: false,
indexThis: undefined,
_onKeyboardInput: undefined,
_onKeyboardConfirm: undefined,
_onKeyboardComplete: undefined,
},
onLaunch() {
},
+15
View File
@@ -2,6 +2,7 @@ globalThis.__wxRequire = require;
var GameGlobal = getApp().GameGlobal;
Page({
onReady() {
GameGlobal.indexThis = this;
const query = wx.createSelectorQuery()
query.select('#myCanvas').node().exec(async (res) => {
const canvas = res[0].node;
@@ -67,6 +68,20 @@ Page({
})
},
onKeyboardInput: function (event) {
GameGlobal._onKeyboardInput && GameGlobal._onKeyboardInput(event);
},
onKeyboardConfirm: function (event) {
console.log(event.detail.value);
GameGlobal._onKeyboardConfirm && GameGlobal._onKeyboardConfirm(event);
},
onKeyboardComplete: function (event) {
console.log(event.detail.value);
GameGlobal._onKeyboardComplete && GameGlobal._onKeyboardComplete(event);
},
touchStart(event) {
GameGlobal._touchStart && GameGlobal._touchStart(event);
},
+12
View File
@@ -1,5 +1,17 @@
<!--index.wxml-->
<view class="container">
<view class="weui-cell weui-cell_input">
<input class="weui-input"
auto-focus
bindinput="onKeyboardInput"
bindblur="onKeyboardComplete"
bindconfirm="onKeyboardConfirm"
confirm-type="{{confirmType}}"
password="{{isPassword}}"
maxLength="{{maxLength}}"
wx:if="{{showInput}}"
/>
</view>
<canvas
type="webgl"
id="myCanvas"
+6
View File
@@ -6,3 +6,9 @@
top: 0px;
bottom: 0px;
}
input {
color: transparent;
text-shadow: 0 0 0 #000;
opacity: 0;
}
@@ -52,6 +52,36 @@ exports.adapt = function (canvas, width, height) {
};
};
wx.onKeyboardInput = function (cb) {
GameGlobal._onKeyboardInput = function (event) {
cb(event.detail);
};
};
wx.onKeyboardConfirm = function (cb) {
GameGlobal._onKeyboardConfirm = function (event) {
cb(event.detail);
};
};
wx.onKeyboardComplete = function (cb) {
GameGlobal._onKeyboardComplete = function (event) {
GameGlobal.indexThis.setData({
showInput: false,
});
cb(event.detail);
};
};
wx.showKeyboard = function (obj) {
GameGlobal.indexThis.setData({
showInput: true,
isPassword: false,
maxLength: obj.maxLength,
confirmType: obj.confirmType
});
};
function handleTouchEvent (event) {
let changedTouches = event.changedTouches;
if (changedTouches) {