term: bring back touch simulation of special keys on mobile

and also fixed it. The original version did not work because it tried
to change read-only fields of the event, which is not allowed.

The last commit removed support of touch simulation of Ctrl and Alt
by removing the `virtual_input.coffee` file. This commit brings it back
with a better implementation.
This commit is contained in:
Peter Cai
2018-06-03 20:10:24 +08:00
parent 4d87059872
commit 866b56b682
6 changed files with 118 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
(function() {
var Popup, Selection, _set_theme_href, _theme, cancel, clean_ansi, copy, escape, histSize, linkify, maybePack, nextLeaf, packSize, popup, previousLeaf, selection, setAlarm, tags, tid, walk,
var Popup, Selection, _set_theme_href, _theme, alt, cancel, clean_ansi, copy, ctrl, escape, histSize, linkify, maybePack, nextLeaf, packSize, popup, previousLeaf, selection, setAlarm, tags, tid, walk,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
clean_ansi = function(data) {
@@ -232,6 +232,46 @@
});
});
ctrl = false;
alt = false;
addEventListener('touchstart', function(e) {
if (e.touches.length === 2) {
return ctrl = true;
} else if (e.touches.length === 3) {
ctrl = false;
return alt = true;
} else if (e.touches.length === 4) {
ctrl = true;
return alt = true;
}
});
window.mobileKeydown = function(e) {
var _altKey, _ctrlKey, _keyCode;
if (ctrl || alt) {
_ctrlKey = ctrl;
_altKey = alt;
_keyCode = e.keyCode;
if (e.keyCode >= 97 && e.keyCode <= 122) {
_keyCode -= 32;
}
e = new KeyboardEvent('keydown', {
ctrlKey: _ctrlKey,
altKey: _altKey,
keyCode: _keyCode
});
ctrl = alt = false;
setTimeout(function() {
return window.dispatchEvent(e);
}, 0);
return true;
} else {
return false;
}
};
document.addEventListener('keydown', function(e) {
if (!(e.altKey && e.keyCode === 79)) {
return true;

File diff suppressed because one or more lines are too long

View File

@@ -1507,7 +1507,17 @@
ev.stopPropagation();
setTimeout((function(_this) {
return function() {
var char, e;
if (!(_this.inComposition || _this.inputHelper.value.length > 1)) {
char = _this.inputHelper.value.toUpperCase().charCodeAt(0);
if ((65 <= char && char <= 90)) {
e = new KeyboardEvent('keydown', {
keyCode: char
});
if (window.mobileKeydown(e)) {
return;
}
}
return _this.send(_this.inputHelper.value);
}
};
@@ -1517,6 +1527,9 @@
if (ev.keyCode > 15 && ev.keyCode < 19) {
return true;
}
if (window.mobileKeydown(ev)) {
return true;
}
if (ev.keyCode === 19) {
this.body.classList.add('stopped');
this.out('\x03');

File diff suppressed because one or more lines are too long

52
coffees/ext/mobile.coffee Normal file
View File

@@ -0,0 +1,52 @@
# *-* coding: utf-8 *-*
# This file is part of butterfly
#
# butterfly Copyright(C) 2015-2017 Florian Mounier
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
ctrl = false
alt = false
addEventListener 'touchstart', (e) ->
if e.touches.length == 2
ctrl = true
else if e.touches.length == 3
ctrl = false
alt = true
else if e.touches.length == 4
ctrl = true
alt = true
# Dispatch a new event if the current event need to
# be modified with ctrlKey and altKey from touch events
# If so, this function will return true and dispatch the new event.
# The caller should return immediately upon receiving true.
window.mobileKeydown = (e) ->
if ctrl or alt
_ctrlKey = ctrl
_altKey = alt
_keyCode = e.keyCode
if e.keyCode >= 97 && e.keyCode <= 122
_keyCode -= 32
e = new KeyboardEvent 'keydown',
ctrlKey: _ctrlKey,
altKey: _altKey,
keyCode: _keyCode
ctrl = alt = false
setTimeout ->
window.dispatchEvent e
, 0
return true
else
return false

View File

@@ -1374,6 +1374,14 @@ class Terminal
# in which case we just fetch it from the text area
setTimeout =>
unless @inComposition || @inputHelper.value.length > 1
char = @inputHelper.value.toUpperCase().charCodeAt(0)
if 65 <= char <= 90
# If the character sent here is a letter
# allow it to be overridden on mobile
# Combinations like "Ctrl+C" won't work without this
# on Chrome for Android
e = new KeyboardEvent 'keydown', keyCode: char
return if window.mobileKeydown e
@send @inputHelper.value
, 0
return false
@@ -1382,6 +1390,7 @@ class Terminal
# https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
# Don't handle modifiers alone
return true if ev.keyCode > 15 and ev.keyCode < 19
return true if window.mobileKeydown ev
if ev.keyCode is 19 # Pause break
@body.classList.add 'stopped'