onscreenkeyboard.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. On Screen Keyboard
  3. A simple on screen keyboard for touch devices that run linux
  4. (Yes, just in case I need to run ArozOS on embedded linux with browser
  5. this will be helpful in those rare cases)
  6. author: tobychui
  7. */
  8. //Handle ArozOS key event passthrough
  9. if (ao_module_virtualDesktop){
  10. if (!parent.window.ime){
  11. alert("Unsupported viewing mode or version of ArozOS too old!")
  12. }else{
  13. parent.window.ime.handler = handleKeydownInput;
  14. }
  15. //Define this is the ime window
  16. ao_module_ime = true;
  17. }
  18. function handleKeydownInput(e) {
  19. //No need to do anything as the default events will fire
  20. }
  21. //Handle text injection to other iframes
  22. function addtext(text){
  23. if (text.includes('"')){
  24. text = text.replace('"','');
  25. }
  26. var focused = $(':focus');
  27. insertAtCaret(focused[0],text)
  28. preword = text;
  29. if (parent.window.ime && parent.window.ime.focus != null){
  30. if (text == "\n" && parent.window.ime.focus.tagName.toLowerCase() == "input"){
  31. //Enter. If target is INPUT send enter keypress instead
  32. //console.log("Triggered!", $(parent.window.ime.focus));
  33. let event = new Event('keypress');
  34. event.keyCode = 13;
  35. event.which = 13;
  36. event.key = 'enter';
  37. console.log(event);
  38. parent.window.ime.focus.dispatchEvent(event);
  39. //$(parent.window.ime.focus).trigger(jQuery.Event( "keypress", { keyCode: 13 } ));
  40. }else{
  41. insertAtCaret(parent.window.ime.focus, text);
  42. }
  43. }
  44. }
  45. function backSpace(){
  46. if (parent.window.ime && parent.window.ime.focus != null){
  47. backSpaceAtCaret(parent.window.ime.focus);
  48. }
  49. }
  50. function backSpaceAtCaret(target){
  51. var txt = $(target);
  52. var startPos = txt[0].selectionStart;
  53. var endPos = txt[0].selectionEnd;
  54. var scrollPos = txt[0].scrollTop;
  55. //console.log("start: " + startPos + " end: " + endPos + " scrollPos: " + scrollPos);
  56. if (endPos - startPos > 0){
  57. txt.val(txt.val().slice(0, startPos) + txt.val().slice(endPos, 100));
  58. }else if (endPos > 0){
  59. txt.val(txt.val().slice(0, startPos-1) + txt.val().slice(startPos, 100));
  60. }else{
  61. startPos = txt.val().length+1;
  62. }
  63. txt.focus();
  64. txt[0].setSelectionRange(startPos-1,startPos-1);
  65. }
  66. function insertAtCaret(target, text) {
  67. var txtarea = target;
  68. if (txtarea == undefined){
  69. return
  70. }
  71. var scrollPos = txtarea.scrollTop;
  72. var caretPos = txtarea.selectionStart;
  73. var front = (txtarea.value).substring(0, caretPos);
  74. var back = (txtarea.value).substring(txtarea.selectionEnd, txtarea.value.length);
  75. txtarea.value = front + text + back;
  76. caretPos = caretPos + text.length;
  77. txtarea.selectionStart = caretPos;
  78. txtarea.selectionEnd = caretPos;
  79. txtarea.focus();
  80. txtarea.scrollTop = scrollPos;
  81. }
  82. //Overwrite the ao_module close handler
  83. function ao_module_close(){
  84. //Deregister this IME from the window object
  85. if (parent.window.ime.handler == handleKeydownInput){
  86. parent.window.ime.handler = null;
  87. }
  88. //Exit IME
  89. closeThisWindow();
  90. }
  91. function closeThisWindow(){
  92. if (!ao_module_virtualDesktop){
  93. window.close('','_parent','');
  94. window.location.href = ao_root + "SystemAO/closeTabInsturction.html";
  95. return;
  96. }
  97. parent.closeFwProcess(ao_module_windowID);
  98. }