{"id":1981,"date":"2018-05-17T10:33:03","date_gmt":"2018-05-17T10:33:03","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=1981"},"modified":"2021-01-23T16:05:38","modified_gmt":"2021-01-23T16:05:38","slug":"a-javascript-keyboard-with-special-language-symbols-that-appears-when-the-user-starts-typing","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/a-javascript-keyboard-with-special-language-symbols-that-appears-when-the-user-starts-typing\/","title":{"rendered":"A JavaScript Keyboard With Special Language Symbols That Appears When The User Starts Typing"},"content":{"rendered":"<p>This blog post will demonstrate how to create a keyboard that renders only when the user starts typing in a text box. When the user clicks outside the text box, the virtual keyboard disappears. There is another extra \u2013 a selection of languages to the right. When a language is selected, the keyboard renders only the special symbols from the selected language.<\/p>\n<p>You can test the JS Hidden Keyboard sample <a href=\"https:\/\/mindfusion.dev\/samples\/javascript\/keyboard\/Hidden-Keyboard.htm\">online from this link<\/a>.<\/p>\n<p>The application uses MindFusion Virtual Keyboard for JavaScript. Here is a screenshot of the application:<\/p>\n<div id=\"attachment_1993\" style=\"width: 669px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/hidden-keyboard.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1993\" class=\"size-full wp-image-1993\" src=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/hidden-keyboard.png\" alt=\"Invisible keyboard in JavaScript\" width=\"659\" height=\"475\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/hidden-keyboard.png 659w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/hidden-keyboard-300x216.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/hidden-keyboard-416x300.png 416w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><\/a><p id=\"caption-attachment-1993\" class=\"wp-caption-text\">Virtual keyboard in JavaScript that renders only when the user starts typing. The keyboard renders special symbols for several languages.<\/p><\/div>\n<p><strong>I. The Keyboards for Spanish, German and French<\/strong><\/p>\n<p>The three keyboard layouts with special symbols for the three languages are created with the Virtual Keyboard Layout Creator tool, that is packed with the JavaScript Keyboard library. We create a new compact keyboard and delete most of the keys. We type the desired key as character for the rest and use the Properties tab to set the size of the keyboard, as well the layout and the key alignment. Finally, we export the new keyboard layout as a json file (File \u2192 Export JavaScript. It is a good idea to save the keyboard layout as xml as well (File \u2192 Save As). This way you can load the keyboard gain, when you need to create another layout based on it or correct this one.<\/p>\n<p>We have created now three json files:<\/p>\n<ul>\n<li>french-symbols.js<\/li>\n<li>german-symbols.js<\/li>\n<li>spanish-symbols.js<\/li>\n<\/ul>\n<p>Let\u2019s look at the contents of one of the json files:<\/p>\n<pre>var layoutDef =\n{\n\twidth: 565,\n\theight: 115,\n\tkeys:\n[\t{\t\n\t\tcode: 162,\n\t\ttype: \"regular\",\n\t\tcontent: \"\u00f9\",\n\t\tleft: 9,\n\t\ttop: 10,\n\t\twidth: 40,\n\t\theight: 40\n\t},\n\t{\t\n\t\tcode: 162,\n\t\ttype: \"regular\",\n\t\tcontent: \"\u00fb\",\n\t\tleft: 56,\n\t\ttop: 10,\n\t\twidth: 40,\n\t\theight: 40\n\t},\n\u2026\u2026\u2026\n{\t\n\t\tcode: 162,\n\t\ttype: \"regular\",\n\t\tcontent: \"\u0153\",\n\t\tleft: 507,\n\t\ttop: 58,\n\t\twidth: 40,\n\t\theight: 40\n\t}\n]\n};\n<\/pre>\n<p>Each file contains an array named \u201cnumpadDef\u201d. We need to change the name, because we will load each list when a certain language is selected, which means we must be able to differentiate among the languages. We rename the lists to layoutDefFr, layoutDefDe and layoutDefEs.<\/p>\n<p>Here is what the Virtual Keyboard Layout Creator Tool looks like:<\/p>\n<div id=\"attachment_1994\" style=\"width: 963px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/virtual-keyboard-layout-creator.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1994\" class=\"size-full wp-image-1994\" src=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/virtual-keyboard-layout-creator.png\" alt=\"Keyboard layout creator tool\" width=\"953\" height=\"820\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/virtual-keyboard-layout-creator.png 953w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/virtual-keyboard-layout-creator-300x258.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/virtual-keyboard-layout-creator-768x661.png 768w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2018\/05\/virtual-keyboard-layout-creator-349x300.png 349w\" sizes=\"auto, (max-width: 953px) 100vw, 953px\" \/><\/a><p id=\"caption-attachment-1994\" class=\"wp-caption-text\">The virtual keyboard layout creator tool that is part of the JavaScript Keyboard library by MindFusion<\/p><\/div>\n<p><strong>II. The HTML Web Page<\/strong><\/p>\n<p>We create a simple web page, where we add a reference to the JavaScript file of the Virtual Keyboard library:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">a<\/span> <span class=\"attribute-name\">href<\/span>=\"<a class=\"attribute-value\" href=\"view-source:http:\/\/keyboard.js\/\">http:\/\/Keyboard.js<\/a>\"&gt;http:\/\/Keyboard.js&lt;\/<span class=\"end-tag\">a<\/span>&gt;<\/pre>\n<p>Note that we place this reference at the end of the file, after the closing tag. The we need reference to the JavaScript files with the definitions of the virtual keyboards that we created in step I.<\/p>\n<pre id=\"line1\"><span id=\"line381\"><\/span>\n<span id=\"line382\"><\/span>&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/a-javascript-keyboard-with-special-language-symbols-that-appears-when-the-user-starts-typing\/french-symbols.js\">french-symbols.js<\/a>\" <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;\n<span id=\"line383\"><\/span>&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/a-javascript-keyboard-with-special-language-symbols-that-appears-when-the-user-starts-typing\/spanish-symbols.js\">spanish-symbols.js<\/a>\" <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;\n<span id=\"line384\"><\/span>&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/a-javascript-keyboard-with-special-language-symbols-that-appears-when-the-user-starts-typing\/german-symbols.js\">german-symbols.js<\/a>\" <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;<\/pre>\n<p>Finally, we add a reference to a JavaScript file that will hold the code for our sample. It will be empty for now.<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/a-javascript-keyboard-with-special-language-symbols-that-appears-when-the-user-starts-typing\/HiddenKeyboard.js\">HiddenKeyboard.js<\/a>\" <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;<\/pre>\n<p>The next step is to load the CSS file with the styling for the JS keyboard. We have chosen the Silver theme, you can choose whichever theme you want or create a custom one.<\/p>\n<pre id=\"line1\"><span id=\"line396\"><\/span>&lt;<span class=\"start-tag\">link<\/span> <span class=\"attribute-name\">href<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/a-javascript-keyboard-with-special-language-symbols-that-appears-when-the-user-starts-typing\/css\/VirtualKeyboard.Silver.css\">css\/VirtualKeyboard.Silver.css<\/a>\" <span class=\"attribute-name\">rel<\/span>=\"<a class=\"attribute-value\">Stylesheet<\/a>\" \/&gt;<\/pre>\n<p>We have added also a reference to a a stylesheet that aligns correctly the<\/p>\n<div>\n<p>elements with the keyboard, the text area and the select list.<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">link<\/span> <span class=\"attribute-name\">href<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/a-javascript-keyboard-with-special-language-symbols-that-appears-when-the-user-starts-typing\/style.css\">style.css<\/a>\" <span class=\"attribute-name\">rel<\/span>=\"<a class=\"attribute-value\">Stylesheet<\/a>\"&gt;<\/pre>\n<p>It contains code purely to align correctly the elements and we will not examine its contents.<\/p>\n<p>Now, let\u2019s create the text area:<\/p>\n<pre>&lt;form id=\"f1\"&gt;\n    &lt;textarea id=\"text\" cols=\"55\" rows=\"15\"&gt;\n    &lt;\/textarea&gt;\n&lt;\/form&gt;<\/pre>\n<p>We create a form, called f1 and there we create the HTML textarea element. It handles to events: onfocus and onblur.<\/p>\n<p>Next, the form contains a select with the three languages, whose keyboards with special symbols we have prepared:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">select<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">selectedLang<\/a>\" <span class=\"attribute-name\">size<\/span>=\"<a class=\"attribute-value\">3<\/a>\"&gt;\n<span id=\"line422\"><\/span>&lt;<span class=\"start-tag\">option<\/span> <span class=\"attribute-name\">value<\/span>=\"<a class=\"attribute-value\">Fr<\/a>\"&gt;French&lt;\/<span class=\"end-tag\">option<\/span>&gt;\n<span id=\"line423\"><\/span>&lt;<span class=\"start-tag\">option<\/span> <span class=\"attribute-name\">value<\/span>=\"<a class=\"attribute-value\">De<\/a>\"&gt;German&lt;\/<span class=\"end-tag\">option<\/span>&gt;\n<span id=\"line424\"><\/span>&lt;<span class=\"start-tag\">option<\/span> <span class=\"attribute-name\">value<\/span>=\"<a class=\"attribute-value\">Es<\/a>\"&gt;Spanish&lt;\/<span class=\"end-tag\">option<\/span>&gt;<\/pre>\n<p>Outside the form, we place a dive with the keyboard:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">div<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">keyboard<\/a>\" <span class=\"attribute-name\">style<\/span>=\"<a class=\"attribute-value\">width: 565px;height: 115px<\/a>\" <span class=\"error\" title=\"Self-closing syntax (\u201c\/&gt;\u201d) used on a non-void HTML element. Ignoring the slash and treating as a start tag.\">\/<\/span>&gt;<\/pre>\n<p>The virtual keyboard will be rendered with fixed size and by default is not rendered e.g. it\u2019s display is set to \u201cnone\u201d.<\/p>\n<p><strong>III. The JavaScript Code-Behind File<\/strong><\/p>\n<p>First, we add two namespace mappings:<\/p>\n<pre>var VirtualKeyboard = MindFusion.VirtualKeyboard;\nvar KeyboardLayout = MindFusion.KeyboardLayout;\n\n<\/pre>\n<p>Then we handle the DOMContentLoaded event to create the Virtual Keyboard object. We use the div element that represents the keyboard as a parameter to the VirtualKeyboard constructor. We have provided the div element with an id in the HTML page.<\/p>\n<p>We also set the selected index of the elements in the language list to -1 e.g. by default no language is selected and the keyboard will show the standard English typing keyboard.<\/p>\n<pre>document.addEventListener(\"DOMContentLoaded\", function (event)\n{\n\tdocument.getElementById(\"selectedLang\").selectedIndex = -1;\n\t\n\tvar vk = VirtualKeyboard.create(\n\t\tdocument.getElementById(\"keyboard\"));\n\tvk.setScaleToFitParent(false);\t\n\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\n\n});\n\n<\/pre>\n<p>Then we handle changes in the selection of the language:<\/p>\n<pre> \n\nf1.selectedLang.onchange = function (event)\n\t{\n\t\t\n\tswitch (event.target.value) {\n  case 'Fr':\n    vk.setLayout(KeyboardLayout.create(layoutDefFr));\n    break;\n  case 'De':\n  vk.setLayout(KeyboardLayout.create(layoutDefDe));\n  break;\n  case 'Es':\n    vk.setLayout(KeyboardLayout.create(layoutDefEs));\n    break;\t\n    } \n }\n\n<\/pre>\n<p>When the user chooses one of our custom layouts we create a keyboard layout with the appropriate layoutDef object.<\/p>\n<p>Next we handle the onfocus and onblur events with the methods showKeyboard() and hideKeyboard(). They just make the div with the keyboard visible or invisible:<\/p>\n<pre>function showKeyboard() { \n\t\t\n\tvar x = document.getElementById(\"keyboard\"); \n\t\tx.style.display = \"block\";\t\t\t\n\t}\n\t\n\tfunction hideKeyboard() {\n    var x = document.getElementById(\"keyboard\");    \n        x.style.display = \"none\";\n    \n\t}\n\n<\/pre>\n<p>And that\u2019s all. As you can see creating and loading a custom keyboard with MindFusion JS library is really as easy as 1, 2 3. You can download the complete source code of the sample from this link:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/javascript\/keyboard\/HiddenKeyboard.zip\">Download the Hidden Keyboard With Special Language Symbols JavaScript Sample<\/a><\/p>\n<p><em>About MindFusion Virtual Keyboard for JavaScript:<\/em> A highly interactive, highly customizable library written in pure JavaScript that provides your web application with screen typing capabilities similar to those on mobile devices. The keyboard supports three predefined layouts: compact, default and extended. You can also use the keyboard layout creator tool that provides a convenient GUI to create and arrange your own keyboards. The library offers a well documented API with self-explanatory API members and tutorials that guarantee flat learning curve. You can choose among 8 predefined keyboard styles or customize them to create your own styles. Licenses are royalty-free, perpetual with discounts for freelancers, startups, academic institutions to name just a few. Learn more at <a href=\"https:\/\/mindfusion.dev\/javascript-keyboard.html\">https:\/\/mindfusion.dev\/javascript-keyboard.html<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This blog post will demonstrate how to create a keyboard that renders only when the user starts typing in a text box. When the user clicks outside the text box, the virtual keyboard disappears. There is another extra \u2013 a &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/a-javascript-keyboard-with-special-language-symbols-that-appears-when-the-user-starts-typing\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[513,74],"tags":[520,517,518,521,514,515,519,516],"class_list":["post-1981","post","type-post","status-publish","format-standard","hentry","category-javascript","category-sample-code","tag-french-symbols-keyboard","tag-german-symbols-keyboard","tag-hidden-keyboard","tag-interactive-js-keybaord","tag-javascript-keyboard","tag-js-keyboard","tag-js-virtual-keyboard","tag-spanish-symbols-keyboard"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-vX","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=1981"}],"version-history":[{"count":15,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1981\/revisions"}],"predecessor-version":[{"id":2626,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1981\/revisions\/2626"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}