jQuery虚拟键盘插件 键盘

  • 源码大小:1.29MB
  • 所需积分:1积分
  • 源码编号:19JP-3731
  • 浏览次数:758次
  • 最后更新:2023年07月16日
  • 所属栏目:其他
本站默认解压密码:19jp.com 或 19jp_com

简介

Keyboard是一个使用jQuery UI构建的jQuery插件,它为您的项目添加了一个屏幕上的虚拟键盘,当指定的输入字段聚焦时,它将弹出。它可以移动,也可以用作科学计算器。

您可能还喜欢:

  • 虚拟键盘模拟器
  • jQuery屏幕虚拟键盘插件-OAK
  • 用于简单虚拟键盘的jQuery插件-键盘

目录:

  • 安装
  • 选项
  • 方法
  • 道具
  • 事件

安装:

  1. # NPM
  2. $ npm install virtual-keyboard
  3.  
  4. # Bower
  5. $ bower install virtual-keyboard

基本用法:

1.在页面上包括jQuery库和jQuery键盘插件的文件。

  1. <!-- Core Stylesheet -->
  2. <link href="css/keyboard.css" rel="stylesheet" />
  3. <!-- Light Theme -->
  4. <link href="css/keyboard-basic.css" rel="stylesheet" />
  5. <!-- Dark Theme -->
  6. <link href="css/keyboard-dark.css" rel="stylesheet" />
  7. <!-- CSS for the preview keyset extension -->
  8. <link href="css/keyboard-previewkeyset.css" rel="stylesheet" />
  9. <!-- jQuery Is Required -->
  10. <script src="/path/to/cdn/jquery.min.js"></script>
  11. <!-- Core JavaScript -->
  12. <script src="js/jquery.keyboard.js"></script>
  13. <!-- With All Extensions -->
  14. <script src="js/jquery.keyboard.extension-all.js"></script>

2.根据您的需要包括其他可选资源。

  1. <!-- jQuery UI Themes -->
  2. <script src="/path/to/cdn/jquery-ui.min.js"></script>
  3. <link rel="stylesheet" href="/path/to/cdn/jquery-ui.min.css" />
  4.  
  5. <!-- Or Bootstrap Theme -->
  6. <link rel="stylesheet" href="/path/to/cdn/bootstrap.min.css" />
  7.  
  8. <!-- Mousewheel support -->
  9. <script src="/path/to/jquery.mousewheel.js"></script>

3.如果需要,包括键盘插件的扩展。

  1. <!-- Show alternate keys in a popup -->
  2. <script src="js/jquery.keyboard.extension-altkeypopup.js"></script>
  3. <!-- Integrate with jQuery UI's autocomplete widget -->
  4. <script src="js/jquery.keyboard.extension-autocomplete.js"></script>
  5. <!-- Add a caret with custom styling -->
  6. <script src="js/jquery.keyboard.extension-caret.js"></script>
  7. <!-- Add a togglable layout -->
  8. <script src="js/jquery.keyboard.extension-extender.js"></script>
  9. <!-- Use with jQuery Mobile & jQuery Mobile -->
  10. <script src="js/jquery.keyboard.extension-mobile.js"></script>
  11. <!-- Use arrow, home, end & page up/down to navigate inside of the keyboard -->
  12. <script src="js/jquery.keyboard.extension-navigation.js"></script>
  13. <!-- Add a preview of chosen keysets; display & position of the preview is accomplished through css -->
  14. <script src="js/jquery.keyboard.extension-previewkeyset.js"></script>
  15. <!-- Scramble the entire keyset or by row, once or every time the keyboard is opened for added security -->
  16. <script src="js/jquery.keyboard.extension-scramble.js"></script>
  17. <!-- Allows you to simulate typing into the keyboard for demo purposes or to assist user input -->
  18. <script src="js/jquery.keyboard.extension-typing.js"></script>

4.将虚拟键盘连接到您的元素,如文本区域、输入字段或contentEditable元素。

  1. <input id="keyboard" type="text">
  1. $(function(){
  2. $('#keyboard').keyboard({
  3. // options here
  4. });
  5. });

5.可用于自定义虚拟键盘的选项。

  1. // set this to ISO 639-1 language code to override language set by the layout
  2. // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
  3. // language defaults to 'en' if not found
  4. language: null,
  5.  
  6. // RTL or LTR
  7. rtl: false,
  8.  
  9. // 'alpha' - Alphabetical layout.
  10. // 'colemak' - Colemak layout.
  11. // 'custom' - Uses a custom layout as defined by the customLayout option.
  12. // 'dvorak' - Dvorak Simplified layout.
  13. // 'international' - Standard US-international QWERTY layout.
  14. // 'num' - Numerical (ten-key) layout.
  15. // 'numpad' - Numerical layout with left & right caret keys (added by extender extension).
  16. // 'qwerty' - Standard U.S. QWERTY layout.
  17. // or 'custom'. see below
  18. layout: 'qwerty',
  19.  
  20. // custom layout
  21. // {
  22. // 'normal' : ['a b c d e', 'f g h i j', '{shift} {meta1} {a} {c}'],
  23. // 'shift' : ['A B C D E', 'F G H I J', '{shift} {meta1} {a} {c}'],
  24. // 'meta1' : ['1 2 3 4 5', '6 7 8 9 0', '{shift} {meta1} {a} {c}']
  25. // }
  26. customLayout: null,
  27.  
  28. position: {
  29. // optional - null (attach to input/textarea) or a jQuery object (attach elsewhere)
  30. of: null,
  31. my: 'center top',
  32. at: 'center top',
  33. // used when 'usePreview' is false (centers the keyboard at the bottom of the input/textarea)
  34. at2: 'center bottom'
  35. },
  36.  
  37. // allow jQuery position utility to reposition the keyboard on window resize
  38. reposition: true,
  39.  
  40. // preview added above keyboard if true, original input/textarea used if false
  41. usePreview: true,
  42.  
  43. // if true, the keyboard will always be visible
  44. alwaysOpen: false,
  45.  
  46. // give the preview initial focus when the keyboard becomes visible
  47. initialFocus: true,
  48.  
  49. // avoid changing the focus (hardware keyboard probably won't work)
  50. noFocus: false,
  51.  
  52. // if true, keyboard will remain open even if the input loses focus, but closes on escape
  53. // or when another keyboard opens.
  54. stayOpen: false,
  55.  
  56. // Prevents the keyboard from closing when the user clicks or presses outside the keyboard
  57. // the `autoAccept` option must also be set to true when this option is true or changes are lost
  58. userClosed: false,
  59.  
  60. // if true, keyboard will not close if you press escape.
  61. ignoreEsc: false,
  62.  
  63. // if true, keyboard will only closed on click event instead of mousedown and touchstart
  64. closeByClickEvent: false,
  65.  
  66. css: {
  67. // input & preview
  68. input: 'ui-widget-content ui-corner-all',
  69. // keyboard container
  70. container: 'ui-widget-content ui-widget ui-corner-all ui-helper-clearfix',
  71. // keyboard container extra class (same as container, but separate)
  72. popup: '',
  73. // default state
  74. buttonDefault: 'ui-state-default ui-corner-all',
  75. // hovered button
  76. buttonHover: 'ui-state-hover',
  77. // Action keys (e.g. Accept, Cancel, Tab, etc); this replaces 'actionClass' option
  78. buttonAction: 'ui-state-active',
  79. // Active keys (e.g. shift down, meta keyset active, combo keys active)
  80. buttonActive: 'ui-state-active',
  81. // used when disabling the decimal button {dec} when a decimal exists in the input area
  82. buttonDisabled: 'ui-state-disabled',
  83. buttonEmpty: 'ui-keyboard-empty'
  84. },
  85.  
  86. // *** Useability ***
  87. // Auto-accept content when clicking outside the keyboard (popup will close)
  88. autoAccept: false,
  89. // Auto-accept content even if the user presses escape (only works if `autoAccept` is `true`)
  90. autoAcceptOnEsc: false,
  91.  
  92. // Prevents direct input in the preview window when true
  93. lockInput: false,
  94.  
  95. // Prevent keys not in the displayed keyboard from being typed in
  96. restrictInput: false,
  97. // Additional allowed characters while restrictInput is true
  98. restrictInclude: '', // e.g. 'a b foo \ud83d\ude38'
  99.  
  100. // Check input against validate function, if valid the accept button gets a class name of
  101. // 'ui-keyboard-valid-input'. If invalid, the accept button gets a class name of
  102. // 'ui-keyboard-invalid-input'
  103. acceptValid: false,
  104. // Auto-accept when input is valid; requires `acceptValid` set `true` & validate callback
  105. autoAcceptOnValid: false,
  106. // Check validation on keyboard initialization. If false, the "Accept" key state (color)
  107. // will not change to show if the content is valid, or not
  108. checkValidOnInit: true,
  109.  
  110. // if acceptValid is true & the validate function returns a false, this option will cancel
  111. // a keyboard close only after the accept button is pressed
  112. cancelClose: true,
  113.  
  114. // tab to go to next, shift-tab for previous (default behavior)
  115. tabNavigation: false,
  116.  
  117. // enter for next input; shift+enter accepts content & goes to next
  118. // shift + 'enterMod' + enter ('enterMod' is the alt as set below) will accept content and go
  119. // to previous in a textarea
  120. enterNavigation: false,
  121. // mod key options: 'ctrlKey', 'shiftKey', 'altKey', 'metaKey' (MAC only)
  122. enterMod: 'altKey', // alt-enter to go to previous; shift-alt-enter to accept & go to previous
  123.  
  124. // if true, the next button will stop on the last keyboard input/textarea; prev button stops at first
  125. // if false, the next button will wrap to target the first input/textarea; prev will go to the last
  126. stopAtEnd: true,
  127.  
  128. // Set this to append the keyboard after the input/textarea (appended to the input/textarea parent).
  129. // This option works best when the input container doesn't have a set width & when the 'tabNavigation'
  130. // option is true.
  131. appendLocally: false,
  132. // When appendLocally is false, the keyboard will be appended to this object
  133. appendTo: 'body',
  134.  
  135. // Wrap all <br>s inside of a contenteditable in a div; without wrapping, the caret
  136. // position will not be accurate
  137. wrapBRs: true,
  138.  
  139. // If false, the shift key will remain active until the next key is (mouse) clicked on; if true it will
  140. // stay active until pressed again
  141. stickyShift: true,
  142.  
  143. // Prevent pasting content into the area
  144. preventPaste: false,
  145.  
  146. // caret placed at the end of any text when keyboard becomes visible
  147. caretToEnd: false,
  148.  
  149. // caret stays this many pixels from the edge of the input while scrolling left/right;
  150. // use "c" or "center" to center the caret while scrolling
  151. scrollAdjustment: 10,
  152.  
  153. // Set the max number of characters allowed in the input, setting it to false disables this option
  154. maxLength: false,
  155. // allow inserting characters @ caret when maxLength is set
  156. maxInsert: true,
  157.  
  158. // Mouse repeat delay - when clicking/touching a virtual keyboard key, after this delay the key will
  159. // start repeating
  160. repeatDelay: 500,
  161.  
  162. // Mouse repeat rate - after the repeatDelay, this is the rate (characters per second) at which the
  163. // key is repeated Added to simulate holding down a real keyboard key and having it repeat. I haven't
  164. // calculated the upper limit of this rate, but it is limited to how fast the javascript can process
  165. // the keys. And for me, in Firefox, it's around 20.
  166. repeatRate: 20,
  167.  
  168. // resets the keyboard to the default keyset when visible
  169. resetDefault: true,
  170.  
  171. // Event (namespaced) on the input to reveal the keyboard. To disable it, just set it to ''.
  172. openOn: 'focus',
  173.  
  174. // enable the keyboard on readonly inputs
  175. activeOnReadonly: false,
  176.  
  177. // Event (namepaced) for when the character is added to the input (clicking on the keyboard)
  178. keyBinding: 'mousedown touchstart',
  179.  
  180. // enable/disable mousewheel functionality
  181. // enabling still depends on the mousewheel plugin
  182. useWheel: true,
  183.  
  184. // combos (emulate dead keys : http://en.wikipedia.org/wiki/Keyboard_layout#US-International)
  185. // if user inputs `a the script converts it to à, ^o becomes ô, etc.
  186. useCombos: true,
  187.  
  188. /*
  189. // *** Methods ***
  190. // commenting these out to reduce the size of the minified version
  191. // Callbacks - attach a function to any of these callbacks as desired
  192. initialized : function(e, keyboard, el) {},
  193. beforeVisible : function(e, keyboard, el) {},
  194. visible : function(e, keyboard, el) {},
  195. beforeInsert : function(e, keyboard, el, textToAdd) { return textToAdd; },
  196. change : function(e, keyboard, el) {},
  197. beforeClose : function(e, keyboard, el, accepted) {},
  198. accepted : function(e, keyboard, el) {},
  199. canceled : function(e, keyboard, el) {},
  200. restricted : function(e, keyboard, el) {},
  201. hidden : function(e, keyboard, el) {},
  202. // called instead of base.switchInput
  203. switchInput : function(keyboard, goToNext, isAccepted) {},
  204. // used if you want to create a custom layout or modify the built-in keyboard
  205. create : function(keyboard) { return keyboard.buildKeyboard(); },
  206.  
  207. // build key callback
  208. buildKey : function( keyboard, data ) {
  209. / *
  210. data = {
  211. // READ ONLY
  212. isAction : [boolean] true if key is an action key
  213. name : [string] key class name suffix ( prefix = 'ui-keyboard-' );
  214. may include decimal ascii value of character
  215. value : [string] text inserted (non-action keys)
  216. title : [string] title attribute of key
  217. action : [string] keyaction name
  218. html : [string] HTML of the key; it includes a <span> wrapping the text
  219. // use to modify key HTML
  220. $key : [object] jQuery selector of key which is already appended to keyboard
  221. }
  222. * /
  223. return data;
  224. },
  225. */
  226.  
  227. // this callback is called, if the acceptValid is true, and just before the 'beforeClose' to check
  228. // the value if the value is valid, return true and the keyboard will continue as it should
  229. // (close if not always open, etc). If the value is not valid, return false and clear the keyboard
  230. // value ( like this "keyboard.$preview.val('');" ), if desired. The validate function is called after
  231. // each input, the 'isClosing' value will be false; when the accept button is clicked,
  232. // 'isClosing' is true
  233. validate: function (/* keyboard, value, isClosing */) {
  234. return true;
  235. }

6.API方法

  1. // reveal the keyboard
  2. keyboard.reveal();
  3.  
  4. // redraw the keyboard
  5. keyboard.redraw();
  6.  
  7. // accept keyboard content
  8. keyboard.accept();
  9.  
  10. // close the keyboard
  11. keyboard.close();
  12.  
  13. // insert text at the current caret position
  14. keyboard.insertText("text");
  15.  
  16. // param1: start & end numeric value of the caret position
  17. // param2: only used if a numeric caret end value is set to enable text selection
  18. keyboard.caret(param1, param2);
  19.  
  20. // heck the current keyboard contents for letter combinations and convert them
  21. keyboard.checkCombos();
  22.  
  23. // check the length of the current keyboard contents and remove any excess
  24. keyboard.checkMaxLength();
  25.  
  26. // get the current key set
  27. keyboard.getKeySet();
  28.  
  29. // show the key set
  30. keyboard.showKeySet(meta);
  31.  
  32. // reposition the keyboard
  33. keyboard.reposition();
  34.  
  35. // toggle the keyboard
  36. keyboard.toggle();
  37.  
  38. // destroy the instance
  39. keyboard.destroy();
  40.  
  41. // get the current value
  42. keyboard.$preview.val();
  43.  
  44. // access any of the options
  45. keyboard.options.{option};
  46.  
  47. // return true when the keyboard is the current
  48. keyboard.isCurrent();

7.财产。

  1. // the text from the input/textarea before the keyboard opened
  2. keyboard.originalContent
  3.  
  4. // information about the last key pressed
  5. keyboard.last
  6.  
  7. // is the keyboard visible
  8. keyboard.isVisible
  9.  
  10. // true when the shift key is active
  11. keyboard.shiftActive
  12.  
  13. // true when the alt key is active
  14. keyboard.altActive
  15.  
  16. // true when the meta key is active
  17. keyboard.metaActive
  18.  
  19. // true when the caps lock key is active
  20. keyboard.capsLock
  21.  
  22. // true when the keyboard (all keys and preview) is enabled
  23. keyboard.enabled

8.事件。

  1. $('#keyboard').on('initialized', function(e, keyboard, el){
  2. // el.id
  3. // el.value
  4. });
  5.  
  6. $('#keyboard').on('beforeVisible', function(e, keyboard, el){
  7. // do something
  8. });
  9.  
  10. $('#keyboard').on('visible', function(e, keyboard, el){
  11. // do something
  12. });
  13.  
  14. $('#keyboard').on('change', function(e, keyboard, el){
  15. // do something
  16. });
  17.  
  18. $('#keyboard').on('keyboardChange', function(e, keyboard, el){
  19. // do something
  20. });
  21.  
  22. $('#keyboard').on('beforeClose', function(e, keyboard, el, accepted){
  23. // do something
  24. });
  25.  
  26. $('#keyboard').on('accepted', function(e, keyboard, el){
  27. // do something
  28. });
  29.  
  30. $('#keyboard').on('canceled', function(e, keyboard, el){
  31. // do something
  32. });
  33.  
  34. $('#keyboard').on('hidden', function(e, keyboard, el){
  35. // do something
  36. });
  37.  
  38. $('#keyboard').on('inactive', function(e, keyboard, el){
  39. // do something
  40. });
  41.  
  42. $('#keyboard').on('restricted', function(e, keyboard, el){
  43. // do something
  44. });
  45.  
  46. $('#keyboard').on('keysetChange', function(e, keyboard, el){
  47. // do something
  48. });

更新日志:

v1.30.4 (2021-01-25)

  • 保存并恢复对init的关注

v1.30.3 (2021-03-15)

  • 插入符号:修复文本区域中插入符号的位置

v1.30.2 (2020-05-12)

  • 删除从数字到文本的输入类型转换。
  • 在不支持的输入类型上引发错误
  • 修复输入类型检测。
  • 修复损坏的键盘检测。
  • 基于十进制设置更新符号正则表达式
  • 更新了多种布局:奥里亚语、泰米尔语、泰卢固语、马拉雅拉姆语、印地语、卡纳达语、孟加拉语、古吉拉特语、马拉地语、尼泊尔语、旁遮普语和乌尔都语。
  • 修复法语翻译

预览截图