{"id":2752,"date":"2013-01-30T11:30:19","date_gmt":"2013-01-30T17:30:19","guid":{"rendered":"http:\/\/bililite.com\/blog\/?p=2752"},"modified":"2013-12-26T00:32:06","modified_gmt":"2013-12-26T06:32:06","slug":"jsvk-a-jquery-plugin-for-virtualkeyboard","status":"publish","type":"post","link":"https:\/\/bililite.com\/blog\/2013\/01\/30\/jsvk-a-jquery-plugin-for-virtualkeyboard\/","title":{"rendered":"<code>jsvk<\/code>, a jQuery Plugin for VirtualKeyboard"},"content":{"rendered":"<p>I've been using Ilya Lebedev's <a href=\"http:\/\/www.allanguages.info\/\">JavaScript VirtualKeyboard<\/a> <a href=\"\/blog\/2010\/12\/26\/using-javascript-virtualkeyboard-with-jquery\/\" title=\"Using JavaScript VirtualKeyboard with jQuery\">for a while<\/a>, and even created a small plugin to integrate it with jQuery. But I wanted to make it more jQuery-ish, so I made a new version of <code>jsvk<\/code>, the plugin to wrap Ilya's code.<\/p>\r\n<p>See a <a href=\"\/blog\/blogfiles\/jsvk\/jsvkdemo1.html\">demo that allows dynamic control of keyboard layout and skin (CSS)<\/a>, and a <a href=\"\/blog\/blogfiles\/jsvk\/jsvkdemo2.html\">demo that shows multiple inputs, including <code class=\"language-html\" >contenteditable<\/code> and jQuery UI's draggable<\/a>.<\/p>\r\n<p><a href=\"\/inc\/jquery.jsvk.js\">Download the code<\/a>.<\/p>\r\n<h3>Usage<\/h3>\r\n<p><code class=\"language-javascript\" >$(element).jsvk(opts)<\/code> to initialize a text-inputing element (either <code class=\"language-html\" >&lt;input&gt;<\/code>, <code class=\"language-html\" >&lt;textarea&gt;<\/code> or <code class=\"language-html\" >&lt;div contenteditable&gt;<\/code>), <code class=\"language-javascript\" >$(element).jsvk('off')<\/code> to detach the keyboard, and <code class=\"language-javascript\" >$(element).jsvk()<\/code> reattach the keyboard leaving the options the same. Note that the <code>VirtualKeyboard<\/code> itself is a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Singleton_pattern\">Singleton<\/a>, so there can only be one open at a time (attaching to one element removes it from another) and that setting <code>opts<\/code> changes the appearance for every attached keyboard. You can do <code class=\"language-javascript\" >$(element).jsvk(opts)<\/code> on an existing attached keyboard to change the options.<\/p>\r\n\r\n<p>It requires <a href=\"http:\/\/bililite.com\/blog\/2011\/01\/17\/cross-browser-text-ranges-and-selections\/\" title=\"Cross-Browser Text Ranges and Selections\">bililiteRange<\/a> to set the text and selections (the original code does not handle <code class=\"language-html\" >&lt;div contenteditable&gt;<\/code> and does not trigger <code class=\"language-javascript\" >input<\/code> events).<\/p>\r\n\r\n<p>For <code class=\"language-html\" >&lt;div contenteditable&gt;<\/code>, the enter key just inserts a newline, which is treated as white space and thus does not go to a new line. You could try to listen to the <code class=\"language-javascript\" >input<\/code> event and insert a <code class=\"language-html\" >&lt;br&gt;<\/code>, or do what I do in the demo and set the style to <code class=\"language-css\" >white-space: pre-wrap<\/code>. This makes all white space significant, which is generally what I want anyway.<\/p>\r\n<h3>Options<\/h3>\r\n<dl>\r\n<dt><code>dir<\/code><\/dt><dd>{String} URL of the directory (without a trailing slash) where the VirtualKeyboard is found. It expects a fixed structure of the directory, so just install it as is from <a href=\"http:\/\/sourceforge.net\/projects\/jsvk\/\">the download<\/a>. This is used exactly once per page; until it is set in some call of <code>jsvk<\/code> all other calls are held and once it's set any future uses of this option is ignored.<\/dd> \r\n<dt><code>kbdiv<\/code><\/dt><dd>{jQuery object | Element | String } The container that the keyboard will be placed into (actually <code class=\"language-javascript\" >$(kbdiv)[0]<\/code>). If not set, creates a new element with <code class=\"language-javascript\" >$('&lt;div&gt;').insertAfter(this)<\/code>.<\/dd>\r\n<dt><code>skin<\/code><\/dt><dd>{String} Name of the CSS directory for styling. The choices are the names of the folders in the <code>css<\/code> directory in <code>opts.dir<\/code>; they are listed in <a href=\"http:\/\/svn.debugger.ru\/repos\/jslibs\/Virtual%20Keyboard\/trunk\/css\/\">the subversion directory<\/a> and <a href=\"http:\/\/debugger.ru\/demo\/projects\/virtualkeyboard\/#skins\">the right of the online demo<\/a>.<\/dd>\r\n<dt><code>layout<\/code><\/dt><dd>{String} Name of language\/layout, like 'IL Hebrew' (see <a href=\"http:\/\/debugger.ru\/demo\/projects\/virtualkeyboard\/#layouts\">the bottom of the online demo<\/a> for choices).<\/dd>\r\n<dt><code>langfilter<\/code><dt><dd>{Array of String} list of language codes (the two-letter codes; must be uppercase) to allow in the drop-down menu for language choices (see the <a href=\"http:\/\/debugger.ru\/demo\/projects\/virtualkeyboard\/#lfilter\">right of the online demo for choices<\/a>). Note that this overrides <code>opts.layout<\/code>; if the selected layout is not in the filter list the first layout that is will be chosen.<\/dd>\r\n<dt><code>nokeyevents<\/code><\/dt><dd>{Boolean} The Virtual Keyboard normally captures keystroke events to translate them into the virtual keys. Set this to true to detach the event capturing (the on-screen keyboard still works with clicking the buttons). This uses a very hacky way of grabbing the event handler, so it may be buggy, though it seems to work for me.<\/dd>\r\n<\/dl>\r\n<h3>Examples<\/h3>\r\n<ul>\r\n\t<li><code class=\"language-javascript\" >$('#input').jsvk({ dir: '\/inc\/VirtualKeyboard', skin: 'air_mid' })<\/code>. Open a <code>VirtualKeyboard<\/code> with a specific skin but the default layout.<\/li>\r\n\t<li><code class=\"language-javascript\" >$('#input').jsvk('off')<\/code>. Hide and detach the <code>VirtualKeyboard<\/code>.<\/li>\r\n\t<li><code class=\"language-javascript\" >$('#input').jsvk()<\/code>. Show the <code>VirtualKeyboard<\/code>, with skin and layout unchanged.<\/li>\r\n\t<li><code class=\"language-javascript\" >$('#input').jsvk({layout: 'US US'})<\/code>. Show the <code>VirtualKeyboard<\/code>, with skin unchanged but set the language and layout.<\/li>\r\n\t<li><code class=\"language-javascript\" >$('#input').jsvk({langfilter: ['GR', 'BA']})<\/code>. Show the <code>VirtualKeyboard<\/code>, but only allow Greek and Bosnian layouts.<\/li>\r\n\t<li>The <a href=\"http:\/\/bililite.com\/blog\/blogfiles\/HebrewKeyboard.php\">Bililite Hebrew Keyboard<\/a> uses this plugin, including the <code>nokeyevents<\/code> option.<\/li>\r\n<\/ul>","protected":false},"excerpt":{"rendered":"I've been using Ilya Lebedev's JavaScript VirtualKeyboard for a while, and even created a small plugin to integrate it with jQuery. But I wanted to make it more jQuery-ish, so I made a new version of jsvk, the plugin to wrap Ilya's code. See a demo that allows dynamic control of keyboard layout and skin [&hellip;]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,5],"tags":[],"_links":{"self":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/2752"}],"collection":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/comments?post=2752"}],"version-history":[{"count":13,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/2752\/revisions"}],"predecessor-version":[{"id":3110,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/2752\/revisions\/3110"}],"wp:attachment":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/media?parent=2752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/categories?post=2752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/tags?post=2752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}