Introduce IKeyEvent
This commit is contained in:
+27
-12
@@ -50,6 +50,7 @@ import { ScreenDprMonitor } from './utils/ScreenDprMonitor';
|
|||||||
import { ITheme, ILocalizableStrings, IMarker, IDisposable } from 'xterm';
|
import { ITheme, ILocalizableStrings, IMarker, IDisposable } from 'xterm';
|
||||||
import { removeTerminalFromCache } from './renderer/atlas/CharAtlasCache';
|
import { removeTerminalFromCache } from './renderer/atlas/CharAtlasCache';
|
||||||
import { DomRenderer } from './renderer/dom/DomRenderer';
|
import { DomRenderer } from './renderer/dom/DomRenderer';
|
||||||
|
import { IKeyEvent } from './base/Types';
|
||||||
|
|
||||||
// reg + shift key mappings for digits and special chars
|
// reg + shift key mappings for digits and special chars
|
||||||
const KEYCODE_KEY_MAPPINGS: { [key: number]: [string, string]} = {
|
const KEYCODE_KEY_MAPPINGS: { [key: number]: [string, string]} = {
|
||||||
@@ -1430,25 +1431,39 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _convertDomKeyEvent(event: KeyboardEvent): IKeyEvent {
|
||||||
|
return {
|
||||||
|
altKey: event.altKey,
|
||||||
|
ctrlKey: event.ctrlKey,
|
||||||
|
shiftKey: event.shiftKey,
|
||||||
|
metaKey: event.metaKey,
|
||||||
|
keyCode: event.keyCode,
|
||||||
|
key: event.key,
|
||||||
|
type: event.type
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle a keydown event
|
* Handle a keydown event
|
||||||
* Key Resources:
|
* Key Resources:
|
||||||
* - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
|
* - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
|
||||||
* @param {KeyboardEvent} ev The keydown event to be handled.
|
* @param {KeyboardEvent} ev The keydown event to be handled.
|
||||||
*/
|
*/
|
||||||
protected _keyDown(ev: KeyboardEvent): boolean {
|
protected _keyDown(domEvent: KeyboardEvent): boolean {
|
||||||
if (this._customKeyEventHandler && this._customKeyEventHandler(ev) === false) {
|
if (this._customKeyEventHandler && this._customKeyEventHandler(domEvent) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this._compositionHelper.keydown(ev)) {
|
if (!this._compositionHelper.keydown(domEvent)) {
|
||||||
if (this.buffer.ybase !== this.buffer.ydisp) {
|
if (this.buffer.ybase !== this.buffer.ydisp) {
|
||||||
this.scrollToBottom();
|
this.scrollToBottom();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = this._evaluateKeyEscapeSequence(ev);
|
const event = this._convertDomKeyEvent(domEvent);
|
||||||
|
|
||||||
|
const result = this._evaluateKeyEscapeSequence(event);
|
||||||
|
|
||||||
// if (result.key === C0.DC3) { // XOFF
|
// if (result.key === C0.DC3) { // XOFF
|
||||||
// this._writeStopped = true;
|
// this._writeStopped = true;
|
||||||
@@ -1458,31 +1473,31 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
|||||||
|
|
||||||
if (result.scrollLines) {
|
if (result.scrollLines) {
|
||||||
this.scrollLines(result.scrollLines);
|
this.scrollLines(result.scrollLines);
|
||||||
return this.cancel(ev, true);
|
return this.cancel(domEvent, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._isThirdLevelShift(this.browser, ev)) {
|
if (this._isThirdLevelShift(this.browser, event)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.cancel) {
|
if (result.cancel) {
|
||||||
// The event is canceled at the end already, is this necessary?
|
// The event is canceled at the end already, is this necessary?
|
||||||
this.cancel(ev, true);
|
this.cancel(domEvent, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result.key) {
|
if (!result.key) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('keydown', ev);
|
this.emit('keydown', domEvent);
|
||||||
this.emit('key', result.key, ev);
|
this.emit('key', result.key, domEvent);
|
||||||
this.showCursor();
|
this.showCursor();
|
||||||
this.handler(result.key);
|
this.handler(result.key);
|
||||||
|
|
||||||
return this.cancel(ev, true);
|
return this.cancel(domEvent, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _isThirdLevelShift(browser: IBrowser, ev: KeyboardEvent): boolean {
|
private _isThirdLevelShift(browser: IBrowser, ev: IKeyEvent): boolean {
|
||||||
const thirdLevelKey =
|
const thirdLevelKey =
|
||||||
(browser.isMac && !this.options.macOptionIsMeta && ev.altKey && !ev.ctrlKey && !ev.metaKey) ||
|
(browser.isMac && !this.options.macOptionIsMeta && ev.altKey && !ev.ctrlKey && !ev.metaKey) ||
|
||||||
(browser.isMSWindows && ev.altKey && ev.ctrlKey && !ev.metaKey);
|
(browser.isMSWindows && ev.altKey && ev.ctrlKey && !ev.metaKey);
|
||||||
@@ -1502,7 +1517,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
|||||||
* Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
|
* Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
|
||||||
* @param ev The keyboard event to be translated to key escape sequence.
|
* @param ev The keyboard event to be translated to key escape sequence.
|
||||||
*/
|
*/
|
||||||
protected _evaluateKeyEscapeSequence(ev: KeyboardEvent): {cancel: boolean, key: string, scrollLines: number} {
|
protected _evaluateKeyEscapeSequence(ev: IKeyEvent): {cancel: boolean, key: string, scrollLines: number} {
|
||||||
const result: {cancel: boolean, key: string, scrollLines: number} = {
|
const result: {cancel: boolean, key: string, scrollLines: number} = {
|
||||||
// Whether to cancel event propogation (NOTE: this may not be needed since the event is
|
// Whether to cancel event propogation (NOTE: this may not be needed since the event is
|
||||||
// canceled at the end of keyDown
|
// canceled at the end of keyDown
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2018 The xterm.js authors. All rights reserved.
|
||||||
|
* @license MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface IKeyEvent {
|
||||||
|
altKey: boolean;
|
||||||
|
ctrlKey: boolean;
|
||||||
|
shiftKey: boolean;
|
||||||
|
metaKey: boolean;
|
||||||
|
keyCode: number;
|
||||||
|
key: string;
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user