Use new Guacamole.Mouse.Touchpad.
[guacamole.git] / src / main / webapp / scripts / interface.js
1
2 // UI Definition
3 var GuacamoleUI = {
4
5     "LOGOUT_PROMPT" :   "Logging out will disconnect all of your active "
6                       + "Guacamole sessions. Are you sure you wish to log out?",
7
8     /* Detection Constants */
9     
10     "LONG_PRESS_DETECT_TIMEOUT"     : 800, /* milliseconds */
11     "LONG_PRESS_MOVEMENT_THRESHOLD" : 10,  /* pixels */
12     "MENU_CLOSE_DETECT_TIMEOUT"     : 500, /* milliseconds */
13     "MENU_OPEN_DETECT_TIMEOUT"      : 325, /* milliseconds */
14     "KEYBOARD_AUTO_RESIZE_INTERVAL" : 30,  /* milliseconds */
15
16     /* Animation Constants */
17
18     "MENU_SHADE_STEPS"    : 10, /* frames */
19     "MENU_SHADE_INTERVAL" : 30, /* milliseconds */
20     "MENU_SHOW_STEPS"     : 5,  /* frames */
21     "MENU_SHOW_INTERVAL"  : 30, /* milliseconds */
22
23     /* OSK Mode Constants */
24     "OSK_MODE_NATIVE" : 1, /* "Show Keyboard" will show the platform's native OSK */
25     "OSK_MODE_GUAC"   : 2, /* "Show Keyboard" will show Guac's built-in OSK */
26
27     /* UI Elements */
28
29     "viewport"    : document.getElementById("viewportClone"),
30     "display"     : document.getElementById("display"),
31     "menu"        : document.getElementById("menu"),
32     "menuControl" : document.getElementById("menuControl"),
33     "touchMenu"   : document.getElementById("touchMenu"),
34     "logo"        : document.getElementById("status-logo"),
35     "eventTarget" : document.getElementById("eventTarget"),
36
37     "buttons": {
38
39         "showClipboard": document.getElementById("showClipboard"),
40         "showKeyboard" : document.getElementById("showKeyboard"),
41         "ctrlAltDelete": document.getElementById("ctrlAltDelete"),
42         "reconnect"    : document.getElementById("reconnect"),
43         "logout"       : document.getElementById("logout"),
44
45         "touchShowClipboard" : document.getElementById("touchShowClipboard"),
46         "touchShowKeyboard"  : document.getElementById("touchShowKeyboard"),
47         "touchLogout"        : document.getElementById("touchLogout")
48
49     },
50
51     "containers": {
52         "state"         : document.getElementById("statusDialog"),
53         "clipboard"     : document.getElementById("clipboardDiv"),
54         "touchClipboard": document.getElementById("touchClipboardDiv"),
55         "keyboard"      : document.getElementById("keyboardContainer")
56     },
57     
58     "state"          : document.getElementById("statusText"),
59     "clipboard"      : document.getElementById("clipboard"),
60     "touchClipboard" : document.getElementById("touchClipboard")
61
62 };
63
64 // Constant UI initialization and behavior
65 (function() {
66
67     var menu_shaded = false;
68
69     var shade_interval = null;
70     var show_interval = null;
71
72     // Cache error image (might not be available when error occurs)
73     var guacErrorImage = new Image();
74     guacErrorImage.src = "images/noguacamole-logo-24.png";
75
76     // Function for adding a class to an element
77     var addClass;
78
79     // Function for removing a class from an element
80     var removeClass;
81
82     // If Node.classList is supported, implement addClass/removeClass using that
83     if (Node.classList) {
84
85         addClass = function(element, classname) {
86             element.classList.add(classname);
87         };
88         
89         removeClass = function(element, classname) {
90             element.classList.remove(classname);
91         };
92         
93     }
94
95     // Otherwise, implement own
96     else {
97
98         addClass = function(element, classname) {
99
100             // Simply add new class
101             element.className += " " + classname;
102
103         };
104         
105         removeClass = function(element, classname) {
106
107             // Filter out classes with given name
108             element.className = element.className.replace(/([^ ]+)[ ]*/g,
109                 function(match, testClassname, spaces, offset, string) {
110
111                     // If same class, remove
112                     if (testClassname == classname)
113                         return "";
114
115                     // Otherwise, allow
116                     return match;
117                     
118                 }
119             );
120
121         };
122         
123     }
124
125
126     GuacamoleUI.hideStatus = function() {
127         removeClass(document.body, "guac-error");
128         GuacamoleUI.containers.state.style.visibility = "hidden";
129         GuacamoleUI.display.style.opacity = "1";
130     };
131     
132     GuacamoleUI.showStatus = function(text) {
133         removeClass(document.body, "guac-error");
134         GuacamoleUI.containers.state.style.visibility = "visible";
135         GuacamoleUI.state.textContent = text;
136         GuacamoleUI.display.style.opacity = "1";
137     };
138     
139     GuacamoleUI.showError = function(error) {
140         addClass(document.body, "guac-error");
141         GuacamoleUI.state.textContent = error;
142         GuacamoleUI.display.style.opacity = "0.1";
143     };
144
145     GuacamoleUI.hideTouchMenu = function() {
146         GuacamoleUI.touchMenu.style.visibility = "hidden";
147     };
148
149     function positionCentered(element) {
150         element.style.left =
151             ((GuacamoleUI.viewport.offsetWidth - element.offsetWidth) / 2
152             + window.pageXOffset)
153             + "px";
154
155         element.style.top =
156             ((GuacamoleUI.viewport.offsetHeight - element.offsetHeight) / 2
157             + window.pageYOffset)
158             + "px";
159     }
160
161     GuacamoleUI.showTouchMenu = function() {
162         positionCentered(GuacamoleUI.touchMenu);
163         GuacamoleUI.touchMenu.style.visibility = "visible";
164     };
165
166     GuacamoleUI.hideTouchClipboard = function() {
167         GuacamoleUI.touchClipboard.blur();
168         GuacamoleUI.containers.touchClipboard.style.visibility = "hidden";
169     };
170
171     GuacamoleUI.showTouchClipboard = function() {
172         positionCentered(GuacamoleUI.containers.touchClipboard);
173         GuacamoleUI.containers.touchClipboard.style.visibility = "visible";
174     };
175
176     GuacamoleUI.shadeMenu = function() {
177
178         if (!menu_shaded) {
179
180             var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHADE_STEPS) + 1;
181             var offset = 0;
182             menu_shaded = true;
183
184             window.clearInterval(show_interval);
185             shade_interval = window.setInterval(function() {
186
187                 offset -= step;
188
189                 GuacamoleUI.menu.style.transform =
190                 GuacamoleUI.menu.style.WebkitTransform =
191                 GuacamoleUI.menu.style.MozTransform =
192                 GuacamoleUI.menu.style.OTransform =
193                 GuacamoleUI.menu.style.msTransform =
194
195                     "translateY(" + offset + "px)";
196
197                 if (offset <= -GuacamoleUI.menu.offsetHeight) {
198                     window.clearInterval(shade_interval);
199                     GuacamoleUI.menu.style.visiblity = "hidden";
200                 }
201
202             }, GuacamoleUI.MENU_SHADE_INTERVAL);
203         }
204
205     };
206
207     GuacamoleUI.showMenu = function() {
208
209         if (menu_shaded) {
210
211             var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHOW_STEPS) + 1;
212             var offset = -GuacamoleUI.menu.offsetHeight;
213             menu_shaded = false;
214             GuacamoleUI.menu.style.visiblity = "";
215
216             window.clearInterval(shade_interval);
217             show_interval = window.setInterval(function() {
218
219                 offset += step;
220
221                 if (offset >= 0) {
222                     offset = 0;
223                     window.clearInterval(show_interval);
224                 }
225
226                 GuacamoleUI.menu.style.transform =
227                 GuacamoleUI.menu.style.WebkitTransform =
228                 GuacamoleUI.menu.style.MozTransform =
229                 GuacamoleUI.menu.style.OTransform =
230                 GuacamoleUI.menu.style.msTransform =
231
232                     "translateY(" + offset + "px)";
233
234             }, GuacamoleUI.MENU_SHOW_INTERVAL);
235         }
236
237     };
238
239     // Show/Hide clipboard
240     GuacamoleUI.buttons.showClipboard.onclick = function() {
241
242         var displayed = GuacamoleUI.containers.clipboard.style.display;
243         if (displayed != "block") {
244             GuacamoleUI.containers.clipboard.style.display = "block";
245             GuacamoleUI.buttons.showClipboard.innerHTML = "Hide Clipboard";
246         }
247         else {
248             GuacamoleUI.containers.clipboard.style.display = "none";
249             GuacamoleUI.buttons.showClipboard.innerHTML = "Show Clipboard";
250             GuacamoleUI.clipboard.onchange();
251         }
252
253     };
254
255     GuacamoleUI.buttons.touchShowClipboard.onclick = function() {
256         GuacamoleUI.hideTouchMenu();
257         GuacamoleUI.showTouchClipboard();
258     };
259
260     // Show/Hide keyboard
261     var keyboardResizeInterval = null;
262     GuacamoleUI.buttons.showKeyboard.onclick = function() {
263
264         // If Guac OSK shown, hide it.
265         var displayed = GuacamoleUI.containers.keyboard.style.display;
266         if (displayed == "block") {
267             GuacamoleUI.containers.keyboard.style.display = "none";
268             GuacamoleUI.buttons.showKeyboard.textContent = "Show Keyboard";
269
270             window.onresize = null;
271             window.clearInterval(keyboardResizeInterval);
272         }
273
274         // Otherwise, show it
275         else {
276
277             // Ensure event target is NOT focused if we are using the Guac OSK.
278             GuacamoleUI.eventTarget.blur();
279
280             GuacamoleUI.containers.keyboard.style.display = "block";
281             GuacamoleUI.buttons.showKeyboard.textContent = "Hide Keyboard";
282
283             // Automatically update size
284             window.onresize = updateKeyboardSize;
285             keyboardResizeInterval = window.setInterval(updateKeyboardSize,
286                 GuacamoleUI.KEYBOARD_AUTO_RESIZE_INTERVAL);
287
288             updateKeyboardSize();
289
290         }
291
292     };
293
294     // Touch-specific keyboard show
295     GuacamoleUI.buttons.touchShowKeyboard.onclick = 
296         function(e) {
297
298             // Center event target in case browser automatically centers
299             // input fields on focus.
300             GuacamoleUI.eventTarget.style.left =
301                 (window.pageXOffset + GuacamoleUI.viewport.offsetWidth / 2) + "px";
302
303             GuacamoleUI.eventTarget.style.top =
304                 (window.pageYOffset + GuacamoleUI.viewport.offsetHeight / 2) + "px";
305
306             GuacamoleUI.eventTarget.focus();
307             GuacamoleUI.hideTouchMenu();
308
309         };
310
311     // Logout
312     GuacamoleUI.buttons.logout.onclick =
313     GuacamoleUI.buttons.touchLogout.onclick =
314         function() {
315
316             // Logout after warning user about session disconnect
317             if (confirm(GuacamoleUI.LOGOUT_PROMPT)) {
318                 window.location.href = "logout";
319                 GuacamoleUI.hideTouchMenu();
320             }
321             
322         };
323
324     // Timeouts for detecting if users wants menu to open or close
325     var detectMenuOpenTimeout = null;
326     var detectMenuCloseTimeout = null;
327
328     // Clear detection timeouts
329     GuacamoleUI.resetMenuDetect = function() {
330
331         if (detectMenuOpenTimeout != null) {
332             window.clearTimeout(detectMenuOpenTimeout);
333             detectMenuOpenTimeout = null;
334         }
335
336         if (detectMenuCloseTimeout != null) {
337             window.clearTimeout(detectMenuCloseTimeout);
338             detectMenuCloseTimeout = null;
339         }
340
341     };
342
343     // Initiate detection of menu open action. If not canceled through some
344     // user event, menu will open.
345     GuacamoleUI.startMenuOpenDetect = function() {
346
347         if (!detectMenuOpenTimeout) {
348
349             // Clear detection state
350             GuacamoleUI.resetMenuDetect();
351
352             // Wait and then show menu
353             detectMenuOpenTimeout = window.setTimeout(function() {
354
355                 // If menu opened via mouse, do not show native OSK
356                 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
357
358                 GuacamoleUI.showMenu();
359                 detectMenuOpenTimeout = null;
360             }, GuacamoleUI.MENU_OPEN_DETECT_TIMEOUT);
361
362         }
363
364     };
365
366     // Initiate detection of menu close action. If not canceled through some
367     // user mouse event, menu will close.
368     GuacamoleUI.startMenuCloseDetect = function() {
369
370         if (!detectMenuCloseTimeout) {
371
372             // Clear detection state
373             GuacamoleUI.resetMenuDetect();
374
375             // Wait and then shade menu
376             detectMenuCloseTimeout = window.setTimeout(function() {
377                 GuacamoleUI.shadeMenu();
378                 detectMenuCloseTimeout = null;
379             }, GuacamoleUI.MENU_CLOSE_DETECT_TIMEOUT);
380
381         }
382
383     };
384
385     // Show menu if mouseover any part of menu
386     GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.showMenu, true);
387
388     // Stop detecting menu state change intents if mouse is over menu
389     GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.resetMenuDetect, true);
390
391     // When mouse hovers over top of screen, start detection of intent to open menu
392     GuacamoleUI.menuControl.addEventListener('mousemove', GuacamoleUI.startMenuOpenDetect, true);
393
394     var long_press_start_x = 0;
395     var long_press_start_y = 0;
396     var menuShowLongPressTimeout = null;
397
398     GuacamoleUI.startLongPressDetect = function() {
399
400         if (!menuShowLongPressTimeout) {
401
402             menuShowLongPressTimeout = window.setTimeout(function() {
403                 
404                 menuShowLongPressTimeout = null;
405
406                 // Assume native OSK if menu shown via long-press
407                 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_NATIVE;
408                 GuacamoleUI.showTouchMenu();
409
410             }, GuacamoleUI.LONG_PRESS_DETECT_TIMEOUT);
411
412         }
413     };
414
415     GuacamoleUI.stopLongPressDetect = function() {
416         window.clearTimeout(menuShowLongPressTimeout);
417         menuShowLongPressTimeout = null;
418     };
419
420     // Detect long-press at bottom of screen
421     GuacamoleUI.display.addEventListener('touchstart', function(e) {
422         
423         // Close menu if shown
424         GuacamoleUI.shadeMenu();
425         GuacamoleUI.hideTouchMenu();
426         GuacamoleUI.hideTouchClipboard();
427         
428         // Record touch location
429         if (e.touches.length == 1) {
430             var touch = e.touches[0];
431             long_press_start_x = touch.screenX;
432             long_press_start_y = touch.screenY;
433         }
434         
435         // Start detection
436         GuacamoleUI.startLongPressDetect();
437         
438     }, true);
439
440     // Stop detection if touch moves significantly
441     GuacamoleUI.display.addEventListener('touchmove', function(e) {
442         
443         // If touch distance from start exceeds threshold, cancel long press
444         var touch = e.touches[0];
445         if (Math.abs(touch.screenX - long_press_start_x) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD
446             || Math.abs(touch.screenY - long_press_start_y) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD)
447             GuacamoleUI.stopLongPressDetect();
448         
449     }, true);
450
451     // Stop detection if press stops
452     GuacamoleUI.display.addEventListener('touchend', GuacamoleUI.stopLongPressDetect, true);
453
454     // Close menu on mouse movement
455     GuacamoleUI.display.addEventListener('mousemove', GuacamoleUI.startMenuCloseDetect, true);
456     GuacamoleUI.display.addEventListener('mousedown', GuacamoleUI.startMenuCloseDetect, true);
457
458     // Reconnect button
459     GuacamoleUI.buttons.reconnect.onclick = function() {
460         window.location.reload();
461     };
462
463     // On-screen keyboard
464     GuacamoleUI.keyboard = new Guacamole.OnScreenKeyboard("layouts/en-us-qwerty.xml");
465     GuacamoleUI.containers.keyboard.appendChild(GuacamoleUI.keyboard.getElement());
466
467     // Function for automatically updating keyboard size
468     var lastKeyboardWidth;
469     function updateKeyboardSize() {
470         var currentSize = GuacamoleUI.keyboard.getElement().offsetWidth;
471         if (lastKeyboardWidth != currentSize) {
472             GuacamoleUI.keyboard.resize(currentSize);
473             lastKeyboardWidth = currentSize;
474         }
475     };
476
477     // Turn off autocorrect and autocapitalization on eventTarget
478     GuacamoleUI.eventTarget.setAttribute("autocorrect", "off");
479     GuacamoleUI.eventTarget.setAttribute("autocapitalize", "off");
480
481     // Automatically reposition event target on scroll
482     window.addEventListener("scroll", function() {
483         GuacamoleUI.eventTarget.style.left = window.pageXOffset + "px";
484         GuacamoleUI.eventTarget.style.top = window.pageYOffset + "px";
485     });
486
487 })();
488
489 // Tie UI events / behavior to a specific Guacamole client
490 GuacamoleUI.attach = function(guac) {
491
492     var title_prefix = null;
493     var connection_name = "Guacamole"; 
494     
495     var guac_display = guac.getDisplay();
496
497     // Set document title appropriately, based on prefix and connection name
498     function updateTitle() {
499
500         // Use title prefix if present
501         if (title_prefix) {
502             
503             document.title = title_prefix;
504
505             // Include connection name, if present
506             if (connection_name)
507                 document.title += " " + connection_name;
508
509         }
510
511         // Otherwise, just set to connection name
512         else if (connection_name)
513             document.title = connection_name;
514
515     }
516
517     // When mouse enters display, start detection of intent to close menu
518     guac_display.addEventListener('mouseover', GuacamoleUI.startMenuCloseDetect, true);
519
520     guac_display.onclick = function(e) {
521         e.preventDefault();
522         return false;
523     };
524
525     // Mouse
526     var mouse = new Guacamole.Mouse(guac_display);
527     var touch = new Guacamole.Mouse.Touchpad(guac_display);
528     touch.onmousedown = touch.onmouseup = touch.onmousemove =
529     mouse.onmousedown = mouse.onmouseup = mouse.onmousemove =
530         function(mouseState) {
531        
532             // Determine mouse position within view
533             var mouse_view_x = mouseState.x + guac_display.offsetLeft - window.pageXOffset;
534             var mouse_view_y = mouseState.y + guac_display.offsetTop  - window.pageYOffset;
535
536             // Determine viewport dimensioins
537             var view_width  = GuacamoleUI.viewport.offsetWidth;
538             var view_height = GuacamoleUI.viewport.offsetHeight;
539
540             // Determine scroll amounts based on mouse position relative to document
541
542             var scroll_amount_x;
543             if (mouse_view_x > view_width)
544                 scroll_amount_x = mouse_view_x - view_width;
545             else if (mouse_view_x < 0)
546                 scroll_amount_x = mouse_view_x;
547             else
548                 scroll_amount_x = 0;
549
550             var scroll_amount_y;
551             if (mouse_view_y > view_height)
552                 scroll_amount_y = mouse_view_y - view_height;
553             else if (mouse_view_y < 0)
554                 scroll_amount_y = mouse_view_y;
555             else
556                 scroll_amount_y = 0;
557
558             // Scroll (if necessary) to keep mouse on screen.
559             window.scrollBy(scroll_amount_x, scroll_amount_y);
560        
561             // Send mouse event
562             guac.sendMouseState(mouseState);
563             
564         };
565
566     // Keyboard
567     var keyboard = new Guacamole.Keyboard(document);
568
569     // Monitor whether the event target is focused
570     var eventTargetFocused = false;
571
572     // Save length for calculation of changed value
573     var currentLength = GuacamoleUI.eventTarget.value.length;
574
575     GuacamoleUI.eventTarget.onfocus = function() {
576         eventTargetFocused = true;
577         GuacamoleUI.eventTarget.value = "";
578         currentLength = 0;
579     };
580
581     GuacamoleUI.eventTarget.onblur = function() {
582         eventTargetFocused = false;
583     };
584
585     // If text is input directly into event target without typing (as with
586     // voice input, for example), type automatically.
587     GuacamoleUI.eventTarget.oninput = function(e) {
588
589         // Calculate current length and change in length
590         var oldLength = currentLength;
591         currentLength = GuacamoleUI.eventTarget.value.length;
592         
593         // If deleted or replaced text, ignore
594         if (currentLength <= oldLength)
595             return;
596
597         // Get changed text
598         var text = GuacamoleUI.eventTarget.value.substring(oldLength);
599
600         // Send each character
601         for (var i=0; i<text.length; i++) {
602
603             // Get char code
604             var charCode = text.charCodeAt(i);
605
606             // Convert to keysym
607             var keysym = 0x003F; // Default to a question mark
608             if (charCode >= 0x0000 && charCode <= 0x00FF)
609                 keysym = charCode;
610             else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
611                 keysym = 0x01000000 | charCode;
612
613             // Send keysym only if not already pressed
614             if (!keyboard.pressed[keysym]) {
615
616                 // Press and release key
617                 guac.sendKeyEvent(1, keysym);
618                 guac.sendKeyEvent(0, keysym);
619
620             }
621
622         }
623
624     }
625
626     function isTypableCharacter(keysym) {
627         return (keysym & 0xFFFF00) != 0xFF00;
628     }
629
630     function disableKeyboard() {
631         keyboard.onkeydown = null;
632         keyboard.onkeyup = null;
633     }
634
635     function enableKeyboard() {
636
637         keyboard.onkeydown = function (keysym) {
638             guac.sendKeyEvent(1, keysym);
639             return eventTargetFocused && isTypableCharacter(keysym);
640         };
641
642         keyboard.onkeyup = function (keysym) {
643             guac.sendKeyEvent(0, keysym);
644             return eventTargetFocused && isTypableCharacter(keysym);
645         };
646
647     }
648
649     // Enable keyboard by default
650     enableKeyboard();
651
652     // Handle client state change
653     guac.onstatechange = function(clientState) {
654
655         switch (clientState) {
656
657             // Idle
658             case 0:
659                 GuacamoleUI.showStatus("Idle.");
660                 title_prefix = "[Idle]";
661                 break;
662
663             // Connecting
664             case 1:
665                 GuacamoleUI.shadeMenu();
666                 GuacamoleUI.showStatus("Connecting...");
667                 title_prefix = "[Connecting...]";
668                 break;
669
670             // Connected + waiting
671             case 2:
672                 GuacamoleUI.showStatus("Connected, waiting for first update...");
673                 title_prefix = "[Waiting...]";
674                 break;
675
676             // Connected
677             case 3:
678                 GuacamoleUI.hideStatus();
679                 title_prefix = null;
680                 break;
681
682             // Disconnecting
683             case 4:
684                 GuacamoleUI.showStatus("Disconnecting...");
685                 title_prefix = "[Disconnecting...]";
686                 break;
687
688             // Disconnected
689             case 5:
690                 GuacamoleUI.showStatus("Disconnected.");
691                 title_prefix = "[Disconnected]";
692                 break;
693
694             // Unknown status code
695             default:
696                 GuacamoleUI.showStatus("[UNKNOWN STATUS]");
697
698         }
699
700         updateTitle();
701     };
702
703     // Name instruction handler
704     guac.onname = function(name) {
705         connection_name = name;
706         updateTitle();
707     };
708
709     // Error handler
710     guac.onerror = function(error) {
711
712         // Disconnect, if connected
713         guac.disconnect();
714
715         // Display error message
716         GuacamoleUI.showError(error);
717         
718     };
719
720     // Disconnect on close
721     window.onunload = function() {
722         guac.disconnect();
723     };
724
725     // Handle clipboard events
726     GuacamoleUI.clipboard.onchange = function() {
727
728         var text = GuacamoleUI.clipboard.value;
729         GuacamoleUI.touchClipboard.value = text;
730         guac.setClipboard(text);
731
732     };
733
734     GuacamoleUI.touchClipboard.onchange = function() {
735
736         var text = GuacamoleUI.touchClipboard.value;
737         GuacamoleUI.clipboard.value = text;
738         guac.setClipboard(text);
739
740     };
741
742     // Ignore keypresses when clipboard is focused
743     GuacamoleUI.clipboard.onfocus =
744     GuacamoleUI.touchClipboard.onfocus = function() {
745         disableKeyboard();
746     };
747
748     // Capture keypresses when clipboard is not focused
749     GuacamoleUI.clipboard.onblur =
750     GuacamoleUI.touchClipboard.onblur = function() {
751         enableKeyboard();
752     };
753
754     // Server copy handler
755     guac.onclipboard = function(data) {
756         GuacamoleUI.clipboard.value = data;
757         GuacamoleUI.touchClipboard.value = data;
758     };
759
760     GuacamoleUI.keyboard.onkeydown = function(keysym) {
761         guac.sendKeyEvent(1, keysym);
762     };
763
764     GuacamoleUI.keyboard.onkeyup = function(keysym) {
765         guac.sendKeyEvent(0, keysym);
766     };
767
768     // Send Ctrl-Alt-Delete
769     GuacamoleUI.buttons.ctrlAltDelete.onclick = function() {
770
771         var KEYSYM_CTRL   = 0xFFE3;
772         var KEYSYM_ALT    = 0xFFE9;
773         var KEYSYM_DELETE = 0xFFFF;
774
775         guac.sendKeyEvent(1, KEYSYM_CTRL);
776         guac.sendKeyEvent(1, KEYSYM_ALT);
777         guac.sendKeyEvent(1, KEYSYM_DELETE);
778         guac.sendKeyEvent(0, KEYSYM_DELETE);
779         guac.sendKeyEvent(0, KEYSYM_ALT);
780         guac.sendKeyEvent(0, KEYSYM_CTRL);
781     };
782
783 };