GUAC-810: Send "synthetic" key events for non-physical input options. Allow components to prevent production/handling of guacKeyup/guacKeydown via guacBeforeKeyup/guacBeforeKeydown.

This commit is contained in:
Michael Jumper
2014-12-20 14:05:54 -08:00
parent b33333da3f
commit eaed778f91
5 changed files with 85 additions and 11 deletions

View File

@@ -529,6 +529,16 @@ angular.module('client').directive('guacClient', [function guacClient() {
event.preventDefault(); event.preventDefault();
} }
}); });
// Universally handle all synthetic keydown events
$scope.$on('guacSyntheticKeydown', function syntheticKeydownListener(event, keysym) {
client.sendKeyEvent(1, keysym);
});
// Universally handle all synthetic keyup events
$scope.$on('guacSyntheticKeyup', function syntheticKeyupListener(event, keysym) {
client.sendKeyEvent(0, keysym);
});
/** /**
* Converts the given bytes to a base64-encoded string. * Converts the given bytes to a base64-encoded string.

View File

@@ -193,19 +193,35 @@ angular.module('index').controller('indexController', ['$scope', '$injector',
// Try to load them now // Try to load them now
$scope.loadBasicPermissions(); $scope.loadBasicPermissions();
// Create event listeners at the global level // Create event listeners at the global level
var keyboard = new Guacamole.Keyboard($document[0]); var keyboard = new Guacamole.Keyboard($document[0]);
// Broadcast keydown events down the scope heirarchy // Broadcast keydown events
keyboard.onkeydown = function onkeydown(keysym) { keyboard.onkeydown = function onkeydown(keysym) {
// Warn of pending keydown
var guacBeforeKeydownEvent = $scope.$broadcast('guacBeforeKeydown', keysym, keyboard);
if (guacBeforeKeydownEvent.defaultPrevented)
return true;
// If not prevented via guacBeforeKeydown, fire corresponding keydown event
var guacKeydownEvent = $scope.$broadcast('guacKeydown', keysym, keyboard); var guacKeydownEvent = $scope.$broadcast('guacKeydown', keysym, keyboard);
return !guacKeydownEvent.defaultPrevented; return !guacKeydownEvent.defaultPrevented;
}; };
// Broadcast keyup events down the scope heirarchy // Broadcast keyup events
keyboard.onkeyup = function onkeyup(keysym) { keyboard.onkeyup = function onkeyup(keysym) {
// Warn of pending keyup
var guacBeforeKeydownEvent = $scope.$broadcast('guacBeforeKeyup', keysym, keyboard);
if (guacBeforeKeydownEvent.defaultPrevented)
return;
// If not prevented via guacBeforeKeyup, fire corresponding keydown event
$scope.$broadcast('guacKeyup', keysym, keyboard); $scope.$broadcast('guacKeyup', keysym, keyboard);
}; };
// Release all keys when window loses focus // Release all keys when window loses focus

View File

@@ -88,12 +88,12 @@ angular.module('osk').directive('guacOsk', [function guacOsk() {
// Broadcast keydown for each key pressed // Broadcast keydown for each key pressed
keyboard.onkeydown = function(keysym) { keyboard.onkeydown = function(keysym) {
$rootScope.$broadcast('guacKeydown', keysym); $rootScope.$broadcast('guacSyntheticKeydown', keysym);
}; };
// Broadcast keydown for each key released // Broadcast keydown for each key released
keyboard.onkeyup = function(keysym) { keyboard.onkeyup = function(keysym) {
$rootScope.$broadcast('guacKeyup', keysym); $rootScope.$broadcast('guacSyntheticKeyup', keysym);
}; };
// Resize keyboard whenever window changes size // Resize keyboard whenever window changes size

View File

@@ -90,8 +90,8 @@ angular.module('textInput').directive('guacKey', [function guacKey() {
// For all non-sticky keys, press and release key immediately // For all non-sticky keys, press and release key immediately
else { else {
$rootScope.$broadcast('guacKeydown', $scope.keysym); $rootScope.$broadcast('guacSyntheticKeydown', $scope.keysym);
$rootScope.$broadcast('guacKeyup', $scope.keysym); $rootScope.$broadcast('guacSyntheticKeyup', $scope.keysym);
} }
}; };
@@ -101,11 +101,11 @@ angular.module('textInput').directive('guacKey', [function guacKey() {
// If the key is pressed now, send keydown // If the key is pressed now, send keydown
if (isPressed) if (isPressed)
$rootScope.$broadcast('guacKeydown', $scope.keysym); $rootScope.$broadcast('guacSyntheticKeydown', $scope.keysym);
// If the key was pressed, but is not pressed any longer, send keyup // If the key was pressed, but is not pressed any longer, send keyup
else if (wasPressed) else if (wasPressed)
$rootScope.$broadcast('guacKeyup', $scope.keysym); $rootScope.$broadcast('guacSyntheticKeyup', $scope.keysym);
}); });

View File

@@ -50,6 +50,42 @@ angular.module('textInput').directive('guacTextInput', [function guacTextInput()
*/ */
var TEXT_INPUT_PADDING_CODEPOINT = 0x200B; var TEXT_INPUT_PADDING_CODEPOINT = 0x200B;
/**
* Keys which should be allowed through to the client when in text
* input mode, providing corresponding key events are received.
* Keys in this set will be allowed through to the server.
*
* @type Object.<Number, Boolean>
*/
var ALLOWED_KEYS = {
0xFF08: true, /* Backspace */
0xFF09: true, /* Tab */
0xFF0D: true, /* Enter */
0xFF1B: true, /* Escape */
0xFF50: true, /* Home */
0xFF51: true, /* Left */
0xFF52: true, /* Up */
0xFF53: true, /* Right */
0xFF54: true, /* Down */
0xFF57: true, /* End */
0xFF64: true, /* Insert */
0xFFBE: true, /* F1 */
0xFFBF: true, /* F2 */
0xFFC0: true, /* F3 */
0xFFC1: true, /* F4 */
0xFFC2: true, /* F5 */
0xFFC3: true, /* F6 */
0xFFC4: true, /* F7 */
0xFFC5: true, /* F8 */
0xFFC6: true, /* F9 */
0xFFC7: true, /* F10 */
0xFFC8: true, /* F11 */
0xFFC9: true, /* F12 */
0xFFE1: true, /* Left shift */
0xFFE2: true, /* Right shift */
0xFFFF: true /* Delete */
};
/** /**
* Recently-sent text, ordered from oldest to most recent. * Recently-sent text, ordered from oldest to most recent.
* *
@@ -151,8 +187,8 @@ angular.module('textInput').directive('guacTextInput', [function guacTextInput()
* @param {Number} keysym The keysym of the key to send. * @param {Number} keysym The keysym of the key to send.
*/ */
var sendKeysym = function sendKeysym(keysym) { var sendKeysym = function sendKeysym(keysym) {
$rootScope.$broadcast('guacKeydown', keysym); $rootScope.$broadcast('guacSyntheticKeydown', keysym);
$rootScope.$broadcast('guacKeyup', keysym); $rootScope.$broadcast('guacSyntheticKeyup', keysym);
}; };
/** /**
@@ -283,6 +319,18 @@ angular.module('textInput').directive('guacTextInput', [function guacTextInput()
e.preventDefault(); e.preventDefault();
}, false); }, false);
// If the text input UI has focus, prevent keydown events
$scope.$on('guacBeforeKeydown', function filterKeydown(event, keysym) {
if (hasFocus && !ALLOWED_KEYS[keysym])
event.preventDefault();
});
// If the text input UI has focus, prevent keyup events
$scope.$on('guacBeforeKeyup', function filterKeyup(event, keysym) {
if (hasFocus && !ALLOWED_KEYS[keysym])
event.preventDefault();
});
}] }]
}; };