0

I am using the ngx-material-keyboard in my Angular app.
I noticed that the shift key behaves strange, when tapped/clicked:

  • expected: after changing the lower case representation of letters to uppercase, on clicking one of those uppercase letters the keyboard switches automatically back to lowercase
  • observed: after changing the lower case representation of letters to uppercase, on clicking one of those uppercase letters the keyboard stays in uppercase (shift key behaves identical to caps lock key)

My expectation comes from the onscreen keyboards on my phone (e.g. in chat apps). Is my expectation valid and is my irritation justified?

Reproduce it yourself here: ngx-material-keyboard demo

As far as I can see, no one created an issue, which I find strange, so I am asking here, first (and no one seems to have addressed it in SO, as well).

Anybody knows how to fix this? (I forked the repo, hence I can change the code, but unfortunately, I can't figure out how the letter case switching is done and how to get the shift handler to switch to uppercase only for one click instead of permanently.)

Here are the code parts that I consider revelant:

In keyboard.compontents.ts:

public onShiftClick(): void {
    this.modifier = MatKeyboardComponent.invertShiftModifier(this.modifier);
    this.shiftClick.next();
}

private static invertShiftModifier(modifier: KeyboardModifier): KeyboardModifier {
    switch (modifier) {
        case KeyboardModifier.None:
            return KeyboardModifier.Shift;

        case KeyboardModifier.Alt:
            return KeyboardModifier.ShiftAlt;

        case KeyboardModifier.ShiftAlt:
            return KeyboardModifier.Alt;

        case KeyboardModifier.Shift:
            return KeyboardModifier.None;
    }
}

And in keyboard-key-component.ts:

public onClick(event: MouseEvent): void {
[...]

// TODO: if current modifier is KeyboardModifier.Shift
// do the invert
}
jasie
  • 2,192
  • 10
  • 39
  • 54
  • Btw: the virtual shift key behaves normal (as expected), when you use the shift key on a real keyboard while the ngx-material-keyboard is open (try it with the linked demo). – jasie Feb 12 '19 at 15:43

1 Answers1

0

I was able to find a working solution, myself.
The onClick() function already provides this.genericClick.emit(event). This emitter can be used to reset the shift after a click on one key. The solution also works correctly, if caps lock is active.

In keyboard.component.html, add the genericClick:

<vwlmz-keyboard-key class="mat-keyboard-col"
    [...]
    (enterClick)="onEnterClick()"
    (genericClick)="onKeyClick()" // add this line
    (capsClick)="onCapsClick()"
    (altClick)="onAltClick()"
    (shiftClick)="onShiftClick()"></vwlmz-keyboard-key>

In keyboard.component.ts, add this:

public enterClick: EventEmitter<void> = new EventEmitter<void>();

public onKeyClick(): void {
    this.genericClick.next();

    // reset shift after clicking a key
    if (this.modifier === KeyboardModifier.Shift) {
        this.modifier = MatKeyboardComponent.invertShiftModifier(KeyboardModifier.Shift);
    }
}
jasie
  • 2,192
  • 10
  • 39
  • 54
  • 1
    You will most likely want to conditionally simulate the `onKeyClick` inside of the [`onKeyUp`](https://github.com/ngx-material-keyboard/core/blob/master/src/core/src/components/keyboard/keyboard.component.ts#L175) function. Otherwise the _shift_ modifier wont be inverted on native keyboard input. Furthermore, you might want to expand this "umschalten" logic to consider the _alt_ modifier as well. – Jota.Toledo Feb 13 '19 at 08:53
  • @Jota.Toledo Regarding you secong point: do you mean a change like this? if (this.modifier === KeyboardModifier.Shift || this.modifier === KeyboardModifier.ShiftAlt) – jasie Feb 13 '19 at 11:35
  • @Jota.Toledo Thanks for you suggestions. Regarding your first point: they real keyboard shift behaviour already worked correctly. I cannot produce a problem when mixing inputs from the real keyboard and those from the virtual keys. – jasie Feb 13 '19 at 11:38
  • If I correctly understood the source, in the case that you activate the _shift_ modifier through the virtual keyboard, it wont be deactivated after a keystroke on the native one. Not sure if this is an issue for you though. – Jota.Toledo Feb 13 '19 at 17:24
  • Depending on your requirements, you might want to deactivate the _alt_ modifier after a keystroke. Basically, duplicate the "release modifier" logic inside of `onKeyUp` inside of your `onKeyClick` – Jota.Toledo Feb 13 '19 at 17:26