question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keyboard events not triggered

See original GitHub issue

Description of the problem:

I need to get the keyboard height when the keyboard will show (documented here : https://capacitor.ionicframework.com/docs/apis/keyboard).

All keyboard events is not triggered with method Keyboard.addListener('keyboardWillShow', ...) but triggered with window.addEventListener('keyboardWillShow', ...).

Problem, window.addEventListener('keyboardWillShow', ...) do not send that informations …

Affected platform

  • Android
  • iOS
  • electron
  • web

OS of the development machine

  • Windows
  • macOS
  • linux

Other information:

  • Application build with vuecli 3.2.3

Capacitor version: 1.0.0-beta.22

node version: v11.2.0

npm version: 6.9.0

CocoaPods version: 1.6.1

Steps to reproduce:

import { Plugins, Capacitor } from "@capacitor/core";
const { Keyboard } = Plugins;

Keyboard.addListener("keyboardWillShow", function(e) {
    console.log(e); //not logged :(
});
window.addEventListener("keyboardWillShow", function(e) {
    console.log(e); //log {"isTrusted":false}
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
Unkn0wn0xcommented, Apr 18, 2020

@diadal I had the same problem, it seems like the native Capacitor addListener (or the code, which needs to be executed within it) runs outside the Angulars Zone. So we need to let the code run within the Angualars Zone again.

I’m setting a variables value to true or false, when the keyboard will show or hide.

My (shortened) code:

import { NgZone } from '@angular/core';

[...]

constructor(
   private ngZone: NgZone
) {}

[...]

private keyboardIsVisible: boolean = false;

[...]

public myInitFunction(): void {

    try {

        // Listen to `keyboardWillShow` event
        Keyboard.addListener('keyboardWillShow', () => {

            this.ngZone.run(() => {
                this.keyboardIsVisible = true;
            });

        });

        // Listen to `keyboardWillHide` event
        Keyboard.addListener('keyboardWillHide', () => {

            this.ngZone.run(() => {
                this.keyboardIsVisible = false;
            });

        });
        
    } catch (error) {
        
        // Error
        console.error(error);

    } finally {

        // Return
        return;

    }

}

[...]

Could this may be a general bug, when using Ionic 4 / 5 with Capactior(s native Bridge)?

Hope it helps.

Cheers Unkn0wn0x

1reaction
jcesarmobilecommented, May 16, 2019

@solojuve1897 as you are using the window event, be aware that on next release the keyboardHeight will be in the event directly instead of being on the event details, there won’t be details anymore. This is for better Cordova compatibility.

But I would recommend using the new events that can be consumed from the plugin directly instead of using the window events.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simple Keypress Event Not working for textbox - Stack Overflow
Hey you should try keypress event like this $("#howdy").keypress(function() { var keycode = (event.keyCode ? event.keyCode : event.which); ...
Read more >
KeyboardEvent - Web APIs | MDN
A key that normally produces a character value has been pressed. This event was highly device-dependent and is obsolete. You should not use...
Read more >
Key press event not triggering - Unreal Engine Forums
Background - I simply want to execute an event (called “Perform Event” here) when I press the “T” key - that calls an...
Read more >
Keyboard: keydown and keyup - The Modern JavaScript Tutorial
The keydown events happens when a key is pressed down, and then keyup – when it's released. event.code and event.key. The key property...
Read more >
Keyboard Events not triggering scripts in Perspective - Ignition
I am trying to put hotkeys on buttons by putting a script on the onKeyUp keyboard event, and triggering the script from the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found