简体   繁体   English

不能将虚拟键盘jQuery插件与Angular 4 + Webpack一起使用

[英]Can't use Virtual Keyboard jQuery plugin with Angular 4 +Webpack

I'm trying to use virtual keyboard on Angular 4 template from Asp Core +webpack I've installed with npm install --save-dev @types/virtual-keyboard in webpack.config.vendor.js I put this line: 我试图在我用npm install --save-dev @ types / virtual-keyboard安装的webpack.config.vendor.js中的Asp Core + webpack的Angular 4模板上使用虚拟键盘,我把这行代码放到了:

const treeShakableModules = [
    .....
    'virtual-keyboard',
   ...
];

in my component template: 在我的组件模板中:

<input id="mycontrol" type="text">

in .ts: 在.ts中:

import * as jQuery from "jquery";
import { KeyboardOptions, NavigateOptions } from "virtual-keyboard";

const kbOptions: KeyboardOptions = {
    display: {
        bksp: "\u2190",
        accept: `Next`,
        cancel: `Back`,
        normal: "ABC",
        meta1: "#+-",
        space: "Space",
        alt: `Alt`,
        s: `ABC`,
    },
    acceptValid: true,
    type: "input",
    layout: "custom",
    customLayout: {
        normal: [
            `a b c d e f g h i j k l m`,
            `n o p q r s t u v x y z w`,
            `1 2 3 4 5 6 7 8 9 0 . _ @`,
            `{alt} {s} {space} {meta1} {s} {bksp} `,
            `{cancel}  {accept}`
        ],
        shift: [
            `A B C D E F G H I J K L M`,
            `N O P Q R S T U V X Y Z W`,
            `1 2 3 4 5 6 7 8 9 0 . _ @`,
            `{alt} {s} {space} {meta1} {s} {bksp} `,
            `{cancel}  {accept}`
        ],
        meta1: [
            `- / : ; ( ) \u20ac & \" ! ? ' \``,
            `[ ] { } # % ^ * + = ° ´ §`,
            ` \\ | ~ < > $ \u00a3 \u00a5 , ' ² ³`,
            `{space} {meta1} {bksp}`,
            `{cancel}  {accept}`
        ],
        "alt-shift": [
            `A B C D E F G H I J K L M N O`,
            `P Q R S T U V X Y Z W \u00df \u00dc \u00d6 \u00c4`,
            `1 2 3 4 5 6 7 8 9 0 . _ @ \u0301`,
            `{alt} {s} {space} {meta1} {s} {bksp} `,
            `{cancel}  {accept}`
        ],
        alt: [
            `a b c d e f g h i j k l m n o`,
            `p q r s t u v x y z w \u00df \u00fc \u00f6 \u00e4`,
            `1 2 3 4 5 6 7 8 9 0 . _ @ \u0301`,
            `{alt} {s} {space} {meta1} {s} {bksp} `,
            `{cancel}  {accept}`
        ],
    },
    lockInput: true,
    alwaysOpen: true,
    appendLocally: true,
    color: "light",
    class: "sxcycx",
    updateOnChange: true,
    usePreview: false,
    tabNavigation: false,
    canceled: () => { console.log("cancelled"); }
};

const navOptions: NavigateOptions = {
    position: [0, 0],
    toggleMode: true,
    focusClass: "hasFocus",
    rowLooping: true,
};

then 然后

ngAfterViewInit(): any {
             try {



jQuery('#mycontrol').css("background-color", "red");  //I see the change
jQuery('#mycontrol').keyboard(kbOptions).addNavigation(navOptions);

....

in console there is no error but focusing textbox there is no keyboard shown I think there must be things to be put in webpack.config.vendor.js 在控制台中没有错误,但聚焦文本框没有显示键盘,我认为必须在webpack.config.vendor.js中放一些东西

thanks 谢谢

Install jquery and virtual-keyboard via npm. 通过npm安装jquery和virtual-keyboard。 And try this: 尝试一下:

// In *.ts file:
import * as $ from 'jquery';
import 'virtual-keyboard';

$('.vk').keyboard({
  change: (e, keyboard, el) => {
    // Dispatch input event to notify angular about changes
    const inputEvent: any = document.createEvent('CustomEvent');
    inputEvent.initEvent('input', true, true);
    el.dispatchEvent(inputEvent);
  }
});

// In scss: //在scss中:

@import "../../node_modules/virtual-keyboard/dist/css/keyboard-dark.min";

BTW, to be able to subscribe to virtual keyboard events it's better to create directive and initialize keyboard there. 顺便说一句,为了能够订阅虚拟键盘事件,最好在其中创建指令并初始化键盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM