]> git.proxmox.com Git - mirror_xterm.js.git/commitdiff
Prevent keyboard modifier events from scrolling down
authorDaniel Imms <daimms@microsoft.com>
Mon, 21 Nov 2016 21:59:30 +0000 (13:59 -0800)
committerDaniel Imms <daimms@microsoft.com>
Mon, 21 Nov 2016 22:01:20 +0000 (14:01 -0800)
Fixes #363

package.json
src/test/test.js
src/utils/Keyboard.test.ts [new file with mode: 0644]
src/utils/Keyboard.ts [new file with mode: 0644]
src/xterm.js

index 93ed9a7b867d09ac5a2b04ca495e0b314548c0dc..519b7400b9a51b07ba514e48c7c9d2c88c2a1fdd 100644 (file)
@@ -10,6 +10,8 @@
   "repository": "https://github.com/sourcelair/xterm.js",
   "license": "MIT",
   "devDependencies": {
+    "@types/chai": "^3.4.34",
+    "@types/mocha": "^2.2.33",
     "@types/node": "^6.0.41",
     "browserify": "^13.1.0",
     "chai": "3.5.0",
index 08593e12a62c4d5f6b2c413cbccb1666a8b061a5..6f700002d1ab62b11e224be453956f56966b3ed0 100644 (file)
@@ -194,7 +194,7 @@ describe('xterm.js', function() {
         terminal.ydisp = 0;
         terminal.ybase = 40;
 
-        terminal.keyDown();
+        terminal.keyDown({ keyCode: 0 });
 
         // Ensure that now the terminal is scrolled to bottom
         assert.equal(terminal.ydisp, terminal.ybase);
diff --git a/src/utils/Keyboard.test.ts b/src/utils/Keyboard.test.ts
new file mode 100644 (file)
index 0000000..618d333
--- /dev/null
@@ -0,0 +1,21 @@
+import { assert } from 'chai';
+import * as Keyboard from './Keyboard';
+
+describe('Keyboard', () => {
+  describe('isModifierOnlyKeyboardEvent', () => {
+    it('should return true when only modifier keys are used', () => {
+      // Note that KeyboardEvent.keyCode is deprecated but we're using it to improve browser
+      // compatibility. This helper returns the `any` type because KeyboardEvent doesn't exist under
+      // NodeJS.
+      function createEvent(keyCode: number): any {
+        return { keyCode };
+      }
+      assert.isTrue(Keyboard.isModifierOnlyKeyboardEvent(createEvent(16)));
+      assert.isTrue(Keyboard.isModifierOnlyKeyboardEvent(createEvent(17)));
+      assert.isTrue(Keyboard.isModifierOnlyKeyboardEvent(createEvent(18)));
+      assert.isTrue(Keyboard.isModifierOnlyKeyboardEvent(createEvent(91)));
+      assert.isFalse(Keyboard.isModifierOnlyKeyboardEvent(createEvent(19)));
+      assert.isFalse(Keyboard.isModifierOnlyKeyboardEvent(createEvent(90)));
+    });
+  });
+});
diff --git a/src/utils/Keyboard.ts b/src/utils/Keyboard.ts
new file mode 100644 (file)
index 0000000..e43cba4
--- /dev/null
@@ -0,0 +1,22 @@
+/**
+ * xterm.js: xterm, in the browser
+ * Copyright (c) 2016, SourceLair Private Company <www.sourcelair.com> (MIT License)
+ */
+
+/**
+ * Keyboard utilities module. This module contains utilities for dealing with keyboard interaction.
+ * @module xterm/utils/Keyboard
+ */
+
+/**
+ * Gets whether a KeyboardEvent is made up entirely of modifier keys.
+ *
+ * @param event The event to check.
+ * @return Whether the KeyboardEvent is made up entirely of modifier keys.
+ */
+export function isModifierOnlyKeyboardEvent(event: KeyboardEvent): boolean {
+  return event.keyCode === 16 || // Shift
+      event.keyCode === 17 || // Control
+      event.keyCode === 18 || // Alt
+      event.keyCode === 91; // Meta
+}
index 85efc22307f621cfb5d2759cf0c6dda32aa0fc2a..5924b79650c9c16ab1051e7de607fe8cd5455f96 100644 (file)
@@ -36,6 +36,7 @@ import { EventEmitter } from './EventEmitter.js';
 import { Viewport } from './Viewport.js';
 import { rightClickHandler, pasteHandler, copyHandler } from './handlers/Clipboard.js';
 import * as Browser from './utils/Browser';
+import * as Keyboard from './utils/Keyboard';
 
 /**
  * Terminal Emulation References:
@@ -2426,7 +2427,7 @@ Terminal.prototype.attachCustomKeydownHandler = function(customKeydownHandler) {
  */
 Terminal.prototype.keyDown = function(ev) {
   // Scroll down to prompt, whenever the user presses a key.
-  if (this.ybase !== this.ydisp) {
+  if (!Keyboard.isModifierOnlyKeyboardEvent(ev) && this.ybase !== this.ydisp) {
     this.scrollToBottom();
   }