You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.6 KiB

  1. // insert characters in a textarea or text input field
  2. // special characters are enclosed in {}; use {{} for the { character itself
  3. // documentation: http://bililite.com/blog/2008/08/20/the-fnsendkeys-plugin/
  4. // source: https://github.com/dwachss/bililiteRange/blob/master/jquery.sendkeys.js
  5. // Version: 4
  6. // Copyright (c) 2013 Daniel Wachsstock
  7. // MIT license:
  8. // Permission is hereby granted, free of charge, to any person
  9. // obtaining a copy of this software and associated documentation
  10. // files (the "Software"), to deal in the Software without
  11. // restriction, including without limitation the rights to use,
  12. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the
  14. // Software is furnished to do so, subject to the following
  15. // conditions:
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. // OTHER DEALINGS IN THE SOFTWARE.
  26. (function($){
  27. $.fn.sendkeys = function (x){
  28. x = x.replace(/([^{])\n/g, '$1{enter}'); // turn line feeds into explicit break insertions, but not if escaped
  29. return this.each( function(){
  30. bililiteRange(this).bounds('selection').sendkeys(x).select();
  31. this.focus();
  32. });
  33. }; // sendkeys
  34. // add a default handler for keydowns so that we can send keystrokes, even though code-generated events
  35. // are untrusted (http://www.w3.org/TR/DOM-Level-3-Events/#trusted-events)
  36. // documentation of special event handlers is at http://learn.jquery.com/events/event-extensions/
  37. $.event.special.keydown = $.event.special.keydown || {};
  38. $.event.special.keydown._default = function (evt){
  39. if (evt.isTrusted) return false;
  40. if (evt.ctrlKey || evt.altKey || evt.metaKey) return false; // only deal with printable characters. This may be a false assumption
  41. if (evt.key == null) return false; // nothing to print. Use the keymap plugin to set this
  42. var target = evt.target;
  43. if (target.isContentEditable || target.nodeName == 'INPUT' || target.nodeName == 'TEXTAREA') {
  44. // only insert into editable elements
  45. var key = evt.key;
  46. if (key.length > 1 && key.charAt(0) != '{') key = '{'+key+'}'; // sendkeys notation
  47. $(target).sendkeys(key);
  48. return true;
  49. }
  50. return false;
  51. }
  52. })(jQuery)