Android Cordova 3.4 - 检测键盘事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23477386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:11:40  来源:igfitidea点击:

Cordova 3.4 - Detect keyboard event

androidjqueryeventscordovakeyboard

提问by Schnapse

I'm trying to detect the showkeyboardand hidekeyboardevents in my application running thanks to Cordova 3.4.0 and JQuery Mobile 1.4.2. In the configuration file, the fullscreen attribute is set to true (I need it).

由于 Cordova 3.4.0 和 JQuery Mobile 1.4.2,我正在尝试检测正在运行的应用程序中的showkeyboardhidekeyboard事件。在配置文件中,fullscreen 属性设置为 true(我需要它)。

The fact is, in LogCat, I can't read (apprently it's due to the fullscreen mode) :

事实是,在 LogCat 中,我无法阅读(显然是由于全屏模式):

SoftKeyboardDetect : Ignore this event

SoftKeyboardDetect : 忽略此事件

Is there any solution to detect these two events? I tried an alternative way by detecting blur and focus events on my input field. It works, but when the keyboard is closed by the back button, those events are not called.

有没有办法检测这两个事件?我通过检测输入字段上的模糊和焦点事件尝试了另一种方法。它可以工作,但是当键盘被后退按钮关闭时,不会调用这些事件。

So, I tried to detect the backbutton event, but it doesn't work (http://simonmacdonald.blogspot.fr/2011/05/overriding-back-button-in-phonegap.html).

所以,我试图检测 backbutton 事件,但它不起作用(http://simonmacdonald.blogspot.fr/2011/05/overriding-back-button-in-phonegap.html)。

回答by Ross

I think this will work for your needs -

我认为这将满足您的需求-

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady () {
    document.addEventListener('hidekeyboard', onKeyboardHide, false);
    document.addEventListener('showkeyboard', onKeyboardShow, false);
}

function onKeyboardHide() {
    console.log('onKeyboardHide');
}

function onKeyboardShow() {
    console.log('onKeyboardShow');
}

// edit

// 编辑

Since you cannot hook into those events you need a plugin. This one here will do the trick.

由于您无法连接到这些事件,因此您需要一个插件。 这里的这个可以解决问题

To install the plugin perform cordova plugin add com.ionic.keyboard

安装插件执行 cordova plugin add com.ionic.keyboard

// This event fires when the keyboard will be shown

window.addEventListener('native.keyboardshow', keyboardShowHandler);

function keyboardShowHandler(e){
    console.log('Keyboard height is: ' + e.keyboardHeight);
}

// This event fires when the keyboard will hide

window.addEventListener('native.keyboardhide', keyboardHideHandler);

function keyboardHideHandler(e){
    console.log('Goodnight, sweet prince');
}

回答by Andreas

The Ionic keyboard plugingives you native.showkeyboard and native.hidekeyboard events which can be used this way: After adding it to you project:

离子键盘插件让你native.showkeyboard并且可以使用这种方式native.hidekeyboard事件:将它添加到你后项目:

cordova plugin add https://github.com/driftyco/ionic-plugins-keyboard.git

use it this way:

这样使用它:

window.addEventListener('native.hidekeyboard', keyboardHideHandler);
window.addEventListener('native.showkeyboard', keyboardShowHandler);
function keyboardHideHandler(e){
    alert('Goodnight, sweet prince');
}
function keyboardShowHandler(e){
    alert('Keyboard height is: ' + e.keyboardHeight);
}

Extra description and features can be found on githubThis worked for me in Cordova 3.4 with full-screen mode configured in the config.xml file . The fact that it has 15036downloads says a lot but as I said I have also used it my self with full-screen on the exact Cordova version.(and it was actually the only thing that worked for me plus it supports ios as well)

可以在github上找到额外的描述和功能 这在 Cordova 3.4 中对我有用,在 config.xml 文件中配置了全屏模式。它有15036 次下载的事实说明了很多,但正如我所说,我也使用它自己在确切的 Cordova 版本上全屏显示。(它实际上是唯一对我有用的东西,而且它也支持 ios)

回答by Arjun T Raj

Hi if you need showkeyboardand hidekeyboardevents in phonegap based application you need to remove fullscreen option , then only these events will trigger.

嗨,如果您需要 在基于 phonegap 的应用程序中显示键盘隐藏键盘事件,您需要删除全屏选项,那么只会触发这些事件。

回答by aaronbartell

I couldn't get any of the answers here to work so I thought I'd share my solution.

我无法在这里得到任何答案,所以我想我会分享我的解决方案。

My scenario is I use a Bootstrap button group to navigate between screens and certain screens need to have <input/>fields with default focus. Well, when I put focus on a field it would bring up the soft keyboard. I tried to hide the keyboard when the new <input/>was displayed but it seems the Android keyboard is being shown immediately after the page has finished rendering (which is after my call to Keyboard.hide();is run)

我的场景是我使用 Bootstrap 按钮组在屏幕之间导航,某些屏幕需要<input/>具有默认焦点的字段。好吧,当我将焦点放在一个领域时,它会调出软键盘。我试图<input/>在显示新键盘时隐藏键盘,但似乎在页面完成渲染后立即显示 Android 键盘(这是在我调用 to 之后Keyboard.hide();

My work around is to use a setTimeout, as shown below.

我的解决方法是使用 a setTimeout,如下所示。

$("#my_input").focus();
window.setTimeout(function(){
  Keyboard.hide();
}, 1);

Why does this work? I believe because it places my callback far enough back in the callback queue.

为什么这样做?我相信是因为它将我的回调放在回调队列中足够远的地方。

NOTE:You might still see the soft keyboard quickly show and then hide. Haven't found a way around that.

注意:您可能仍会看到软键盘快速显示然后隐藏。还没有找到解决办法。