mirror of
https://github.com/wavetermdev/xterm.js.git
synced 2026-08-05 13:43:48 -07:00
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 { removeTerminalFromCache } from './renderer/atlas/CharAtlasCache';
|
||||
import { DomRenderer } from './renderer/dom/DomRenderer';
|
||||
import { IKeyEvent } from './base/Types';
|
||||
|
||||
// reg + shift key mappings for digits and special chars
|
||||
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
|
||||
* Key Resources:
|
||||
* - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
|
||||
* @param {KeyboardEvent} ev The keydown event to be handled.
|
||||
*/
|
||||
protected _keyDown(ev: KeyboardEvent): boolean {
|
||||
if (this._customKeyEventHandler && this._customKeyEventHandler(ev) === false) {
|
||||
protected _keyDown(domEvent: KeyboardEvent): boolean {
|
||||
if (this._customKeyEventHandler && this._customKeyEventHandler(domEvent) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this._compositionHelper.keydown(ev)) {
|
||||
if (!this._compositionHelper.keydown(domEvent)) {
|
||||
if (this.buffer.ybase !== this.buffer.ydisp) {
|
||||
this.scrollToBottom();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const result = this._evaluateKeyEscapeSequence(ev);
|
||||
const event = this._convertDomKeyEvent(domEvent);
|
||||
|
||||
const result = this._evaluateKeyEscapeSequence(event);
|
||||
|
||||
// if (result.key === C0.DC3) { // XOFF
|
||||
// this._writeStopped = true;
|
||||
@@ -1458,31 +1473,31 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
|
||||
if (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;
|
||||
}
|
||||
|
||||
if (result.cancel) {
|
||||
// The event is canceled at the end already, is this necessary?
|
||||
this.cancel(ev, true);
|
||||
this.cancel(domEvent, true);
|
||||
}
|
||||
|
||||
if (!result.key) {
|
||||
return true;
|
||||
}
|
||||
|
||||
this.emit('keydown', ev);
|
||||
this.emit('key', result.key, ev);
|
||||
this.emit('keydown', domEvent);
|
||||
this.emit('key', result.key, domEvent);
|
||||
this.showCursor();
|
||||
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 =
|
||||
(browser.isMac && !this.options.macOptionIsMeta && 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
|
||||
* @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} = {
|
||||
// Whether to cancel event propogation (NOTE: this may not be needed since the event is
|
||||
// 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