From ed6ac63747174d2142c093498529199f94c59604 Mon Sep 17 00:00:00 2001 From: xianshenglu Date: Sun, 12 Nov 2023 00:01:04 +0800 Subject: [PATCH] fix: fix paste not working bug --- core/clipboard.js | 20 ++++++++++++++------ core/input/keyboard.js | 4 ++++ core/rfb.js | 10 +++++++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/core/clipboard.js b/core/clipboard.js index a4a68132..92182591 100644 --- a/core/clipboard.js +++ b/core/clipboard.js @@ -19,27 +19,35 @@ export default class Clipboard { navigator.clipboard.writeText(e.clipboardData.getData('text/plain')).catch(() => {/* Do nothing */}); } } - + /** + * @param {ClipboardEvent} e + */ _handlePaste(e) { - if (navigator.clipboard.readText) { - navigator.clipboard.readText().then(this.onpaste).catch(() => {/* Do nothing */}); - } else if (e.clipboardData) { + if(!this._isVncEvent()){ + return; + } + if(e.clipboardData){ this.onpaste(e.clipboardData.getData('text/plain')); } } + _isVncEvent(){ + const isTargetFocused = document.activeElement === this._target; + return isTargetFocused; + } // ===== PUBLIC METHODS ===== grab() { if (!Clipboard.isSupported) return; this._target.addEventListener('copy', this._eventHandlers.copy); - this._target.addEventListener('paste', this._eventHandlers.paste); + // _target can not listen the paste event. + document.body.addEventListener('paste', this._eventHandlers.paste); } ungrab() { if (!Clipboard.isSupported) return; this._target.removeEventListener('copy', this._eventHandlers.copy); - this._target.removeEventListener('paste', this._eventHandlers.paste); + document.body.removeEventListener('paste', this._eventHandlers.paste); } } diff --git a/core/input/keyboard.js b/core/input/keyboard.js index 9068e9e9..d96f477f 100644 --- a/core/input/keyboard.js +++ b/core/input/keyboard.js @@ -85,6 +85,10 @@ export default class Keyboard { _handleKeyDown(e) { const code = this._getKeyCode(e); + const isCtrlVEvent = code === "KeyV" && e.ctrlKey; + if(isCtrlVEvent){ + return; + } let keysym = KeyboardUtil.getKeysym(e); let numlock = e.getModifierState('NumLock'); let capslock = e.getModifierState('CapsLock'); diff --git a/core/rfb.js b/core/rfb.js index 0bf6ff24..efae90e3 100644 --- a/core/rfb.js +++ b/core/rfb.js @@ -261,7 +261,7 @@ export default class RFB extends EventTargetMixin { } this._clipboard = new Clipboard(this._canvas); - this._clipboard.onpaste = this.clipboardPasteFrom.bind(this); + this._clipboard.onpaste = this._handlePasteEvent.bind(this); this._keyboard = new Keyboard(this._canvas); this._keyboard.onkeyevent = this._handleKeyEvent.bind(this); @@ -500,6 +500,14 @@ export default class RFB extends EventTargetMixin { this._canvas.blur(); } + _handlePasteEvent(text){ + this.clipboardPasteFrom(text); + this.sendKey(KeyTable.XK_Control_L, "ControlLeft", true) + this.sendKey(KeyTable.XK_V, "KeyV", true) + this.sendKey(KeyTable.XK_V, "KeyV", false) + this.sendKey(KeyTable.XK_Control_L, "ControlLeft", false) + } + clipboardPasteFrom(text) { if (this._rfbConnectionState !== 'connected' || this._viewOnly) { return; }