Allow user of API to decide whether keyboard events are canceled.

This commit is contained in:
Michael Jumper
2012-03-29 10:05:38 -07:00
parent e8ee79a5b0
commit eca34b24fb

View File

@@ -60,6 +60,9 @@ Guacamole.Keyboard = function(element) {
* *
* @event * @event
* @param {Number} keysym The keysym of the key being pressed. * @param {Number} keysym The keysym of the key being pressed.
* @returns {Boolean} true if the originating event of this keypress should
* be allowed through to the browser, false or undefined
* otherwise.
*/ */
this.onkeydown = null; this.onkeydown = null;
@@ -69,6 +72,9 @@ Guacamole.Keyboard = function(element) {
* *
* @event * @event
* @param {Number} keysym The keysym of the key being released. * @param {Number} keysym The keysym of the key being released.
* @returns {Boolean} true if the originating event of this key release
* should be allowed through to the browser, false or
* undefined otherwise.
*/ */
this.onkeyup = null; this.onkeyup = null;
@@ -208,13 +214,15 @@ Guacamole.Keyboard = function(element) {
// Sends a single keystroke over the network // Sends a single keystroke over the network
function sendKeyPressed(keysym) { function sendKeyPressed(keysym) {
if (keysym != null && guac_keyboard.onkeydown) if (keysym != null && guac_keyboard.onkeydown)
guac_keyboard.onkeydown(keysym); return guac_keyboard.onkeydown(keysym) != false;
return true;
} }
// Sends a single keystroke over the network // Sends a single keystroke over the network
function sendKeyReleased(keysym) { function sendKeyReleased(keysym) {
if (keysym != null && guac_keyboard.onkeyup) if (keysym != null && guac_keyboard.onkeyup)
guac_keyboard.onkeyup(keysym); return guac_keyboard.onkeyup(keysym) != false;
return true;
} }
@@ -269,11 +277,13 @@ Guacamole.Keyboard = function(element) {
// Send key event here // Send key event here
if (keySymSource == KEYDOWN) { if (keySymSource == KEYDOWN) {
var returnValue = true;
if (keydownChar[keynum] != keysym) { if (keydownChar[keynum] != keysym) {
// Send event // Send event
keydownChar[keynum] = keysym; keydownChar[keynum] = keysym;
sendKeyPressed(keysym); returnValue = sendKeyPressed(keysym);
// Clear old key repeat, if any. // Clear old key repeat, if any.
stopRepeat(); stopRepeat();
@@ -283,7 +293,7 @@ Guacamole.Keyboard = function(element) {
repeatKeyTimeoutId = setTimeout(function() { startRepeat(keysym); }, 500); repeatKeyTimeoutId = setTimeout(function() { startRepeat(keysym); }, 500);
} }
return false; return returnValue;
} }
return true; return true;
@@ -302,6 +312,7 @@ Guacamole.Keyboard = function(element) {
if (window.event) keynum = window.event.keyCode; if (window.event) keynum = window.event.keyCode;
else if (e.which) keynum = e.which; else if (e.which) keynum = e.which;
var returnValue = true;
var keysym = getKeySymFromCharCode(keynum); var keysym = getKeySymFromCharCode(keynum);
if (keysym && keydownChar[keynum] != keysym) { if (keysym && keydownChar[keynum] != keysym) {
@@ -316,13 +327,14 @@ Guacamole.Keyboard = function(element) {
stopRepeat(); stopRepeat();
// Send key event // Send key event
sendKeyPressed(keysym); returnValue = sendKeyPressed(keysym);
// Start repeating (if not a modifier key) after a short delay // Start repeating (if not a modifier key) after a short delay
repeatKeyTimeoutId = setTimeout(function() { startRepeat(keysym); }, 500); repeatKeyTimeoutId = setTimeout(function() { startRepeat(keysym); }, 500);
} }
return false; return returnValue;
}; };
// When key released // When key released
@@ -352,9 +364,8 @@ Guacamole.Keyboard = function(element) {
keydownChar[keynum] = null; keydownChar[keynum] = null;
// Send release event // Send release event
sendKeyReleased(lastKeyDownChar); return sendKeyReleased(lastKeyDownChar);
return false;
}; };
// When focus is lost, clear modifiers. // When focus is lost, clear modifiers.