pikvm/pikvm#375: fixed AltGr handling

This commit is contained in:
Maxim Devaev
2022-08-22 18:41:02 +03:00
parent 7bd690b4db
commit 766e515178
4 changed files with 55 additions and 8 deletions

View File

@@ -26,7 +26,7 @@
import {tools, $$$} from "./tools.js"; import {tools, $$$} from "./tools.js";
export function Keypad(__keys_parent, __sendKey, __fix_mac_cmd=false) { export function Keypad(__keys_parent, __sendKey, __apply_fixes) {
var self = this; var self = this;
/************************************************************************/ /************************************************************************/
@@ -35,7 +35,22 @@ export function Keypad(__keys_parent, __sendKey, __fix_mac_cmd=false) {
var __keys = {}; var __keys = {};
var __modifiers = {}; var __modifiers = {};
var __fix_mac_cmd = false;
var __fix_win_altgr = false;
var __altgr_ctrl_timer = null;
var __init__ = function() { var __init__ = function() {
if (__apply_fixes) {
__fix_mac_cmd = tools.browser.is_mac;
if (__fix_mac_cmd) {
tools.info(`Keymap at ${__keys_parent}: enabled Fix-Mac-CMD`);
}
__fix_win_altgr = tools.browser.is_win;
if (__fix_win_altgr) {
tools.info(`Keymap at ${__keys_parent}: enabled Fix-Win-AltGr`);
}
}
for (let el_key of $$$(`${__keys_parent} div.key`)) { for (let el_key of $$$(`${__keys_parent} div.key`)) {
let code = el_key.getAttribute("data-code"); let code = el_key.getAttribute("data-code");
@@ -81,6 +96,11 @@ export function Keypad(__keys_parent, __sendKey, __fix_mac_cmd=false) {
self.emit = function(code, state, apply_fixes=true) { self.emit = function(code, state, apply_fixes=true) {
if (code in __merged) { if (code in __merged) {
if (__fix_win_altgr && apply_fixes) {
if (!__fixWinAltgr(code, state)) {
return;
}
}
__commonHandler(__merged[code][0], state, false); __commonHandler(__merged[code][0], state, false);
if (__fix_mac_cmd && apply_fixes) { if (__fix_mac_cmd && apply_fixes) {
__fixMacCmd(); __fixMacCmd();
@@ -103,6 +123,34 @@ export function Keypad(__keys_parent, __sendKey, __fix_mac_cmd=false) {
} }
}; };
var __fixWinAltgr = function(code, state) {
// https://github.com/pikvm/pikvm/issues/375
// https://github.com/novnc/noVNC/blob/84f102d6/core/input/keyboard.js
if (state) {
if (__altgr_ctrl_timer) {
clearTimeout(__altgr_ctrl_timer);
__altgr_ctrl_timer = null;
if (code !== "AltRight") {
self.emit("ControlLeft", true, false);
}
}
if (code === "ControlLeft" && !__isActive(__modifiers["ControlLeft"][0])) {
__altgr_ctrl_timer = setTimeout(function() {
__altgr_ctrl_timer = null;
self.emit("ControlLeft", true, false);
}, 50);
return false; // Stop handling
}
} else {
if (__altgr_ctrl_timer) {
clearTimeout(__altgr_ctrl_timer);
__altgr_ctrl_timer = null;
self.emit("ControlLeft", true, false);
}
}
return true; // Continue handling
};
var __clickHandler = function(el_key, state) { var __clickHandler = function(el_key, state) {
__commonHandler(el_key, state, false); __commonHandler(el_key, state, false);
__unholdModifiers(); __unholdModifiers();

View File

@@ -35,12 +35,7 @@ export function Keyboard(__recordWsEvent) {
var __keypad = null; var __keypad = null;
var __init__ = function() { var __init__ = function() {
let fix_mac_cmd = tools.browser.is_mac; __keypad = new Keypad("div#keyboard-window", __sendKey, true);
if (fix_mac_cmd) {
tools.info("Keyboard: enabled Fix-Mac-CMD");
}
__keypad = new Keypad("div#keyboard-window", __sendKey, fix_mac_cmd);
$("hid-keyboard-led").title = "Keyboard free"; $("hid-keyboard-led").title = "Keyboard free";

View File

@@ -48,7 +48,7 @@ export function Mouse(__getGeometry, __recordWsEvent) {
var __stream_hovered = false; var __stream_hovered = false;
var __init__ = function() { var __init__ = function() {
__keypad = new Keypad("div#stream-mouse-buttons", __sendButton); __keypad = new Keypad("div#stream-mouse-buttons", __sendButton, false);
$("hid-mouse-led").title = "Mouse free"; $("hid-mouse-led").title = "Mouse free";

View File

@@ -388,6 +388,9 @@ export var tools = new function() {
|| "Unknown" || "Unknown"
).indexOf("Mac") !== -1); ).indexOf("Mac") !== -1);
// Any Windows
let is_win = (navigator && !!(/win/i).exec(navigator.platform));
return { return {
"is_opera": is_opera, "is_opera": is_opera,
"is_firefox": is_firefox, "is_firefox": is_firefox,
@@ -396,6 +399,7 @@ export var tools = new function() {
"is_blink": is_blink, "is_blink": is_blink,
"is_ios": is_ios, "is_ios": is_ios,
"is_mac": is_mac, "is_mac": is_mac,
"is_win": is_win,
}; };
}; };
self.info("Browser:", self.browser); self.info("Browser:", self.browser);